From 9dcdea59a7ec6feca2a9ab645c8d4a46ef143373 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 17 Mar 2014 21:53:56 +0000 Subject: [PATCH] Added a couple of methods to Range. --- modules/juce_core/maths/juce_Range.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/modules/juce_core/maths/juce_Range.h b/modules/juce_core/maths/juce_Range.h index 56678c1b96..d58a062466 100644 --- a/modules/juce_core/maths/juce_Range.h +++ b/modules/juce_core/maths/juce_Range.h @@ -237,6 +237,13 @@ public: jmax (end, other.end)); } + /** Returns the smallest range that contains both this one and the given value. */ + Range getUnionWith (const ValueType valueToInclude) const noexcept + { + return Range (jmin (valueToInclude, start), + jmax (valueToInclude, end)); + } + /** Returns a given range, after moving it forwards or backwards to fit it within this range. @@ -255,6 +262,26 @@ public: : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart())); } + /** Scans an array of values for its min and max, and returns these as a Range. */ + static Range findMinAndMax (const ValueType* values, int numValues) noexcept + { + if (numValues <= 0) + return Range(); + + const ValueType first (*values++); + Range r (first, first); + + while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample) + { + const ValueType v (*values++); + + if (r.end < v) r.end = v; + if (v < r.start) r.start = v; + } + + return r; + } + private: //============================================================================== ValueType start, end;