juce_Range.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_RANGE_H_INCLUDED
  22. #define JUCE_RANGE_H_INCLUDED
  23. //==============================================================================
  24. /** A general-purpose range object, that simply represents any linear range with
  25. a start and end point.
  26. Note that when checking whether values fall within the range, the start value is
  27. considered to be inclusive, and the end of the range exclusive.
  28. The templated parameter is expected to be a primitive integer or floating point
  29. type, though class types could also be used if they behave in a number-like way.
  30. */
  31. template <typename ValueType>
  32. class Range
  33. {
  34. public:
  35. //==============================================================================
  36. /** Constructs an empty range. */
  37. Range() noexcept : start(), end()
  38. {
  39. }
  40. /** Constructs a range with given start and end values. */
  41. Range (const ValueType startValue, const ValueType endValue) noexcept
  42. : start (startValue), end (jmax (startValue, endValue))
  43. {
  44. }
  45. /** Constructs a copy of another range. */
  46. Range (const Range& other) noexcept
  47. : start (other.start), end (other.end)
  48. {
  49. }
  50. /** Copies another range object. */
  51. Range& operator= (Range other) noexcept
  52. {
  53. start = other.start;
  54. end = other.end;
  55. return *this;
  56. }
  57. /** Returns the range that lies between two positions (in either order). */
  58. static Range between (const ValueType position1, const ValueType position2) noexcept
  59. {
  60. return position1 < position2 ? Range (position1, position2)
  61. : Range (position2, position1);
  62. }
  63. /** Returns a range with a given start and length. */
  64. static Range withStartAndLength (const ValueType startValue, const ValueType length) noexcept
  65. {
  66. jassert (length >= ValueType());
  67. return Range (startValue, startValue + length);
  68. }
  69. /** Returns a range with the specified start position and a length of zero. */
  70. static Range emptyRange (const ValueType start) noexcept
  71. {
  72. return Range (start, start);
  73. }
  74. //==============================================================================
  75. /** Returns the start of the range. */
  76. inline ValueType getStart() const noexcept { return start; }
  77. /** Returns the length of the range. */
  78. inline ValueType getLength() const noexcept { return end - start; }
  79. /** Returns the end of the range. */
  80. inline ValueType getEnd() const noexcept { return end; }
  81. /** Returns true if the range has a length of zero. */
  82. inline bool isEmpty() const noexcept { return start == end; }
  83. //==============================================================================
  84. /** Changes the start position of the range, leaving the end position unchanged.
  85. If the new start position is higher than the current end of the range, the end point
  86. will be pushed along to equal it, leaving an empty range at the new position.
  87. */
  88. void setStart (const ValueType newStart) noexcept
  89. {
  90. start = newStart;
  91. if (end < newStart)
  92. end = newStart;
  93. }
  94. /** Returns a range with the same end as this one, but a different start.
  95. If the new start position is higher than the current end of the range, the end point
  96. will be pushed along to equal it, returning an empty range at the new position.
  97. */
  98. Range withStart (const ValueType newStart) const noexcept
  99. {
  100. return Range (newStart, jmax (newStart, end));
  101. }
  102. /** Returns a range with the same length as this one, but moved to have the given start position. */
  103. Range movedToStartAt (const ValueType newStart) const noexcept
  104. {
  105. return Range (newStart, end + (newStart - start));
  106. }
  107. /** Changes the end position of the range, leaving the start unchanged.
  108. If the new end position is below the current start of the range, the start point
  109. will be pushed back to equal the new end point.
  110. */
  111. void setEnd (const ValueType newEnd) noexcept
  112. {
  113. end = newEnd;
  114. if (newEnd < start)
  115. start = newEnd;
  116. }
  117. /** Returns a range with the same start position as this one, but a different end.
  118. If the new end position is below the current start of the range, the start point
  119. will be pushed back to equal the new end point.
  120. */
  121. Range withEnd (const ValueType newEnd) const noexcept
  122. {
  123. return Range (jmin (start, newEnd), newEnd);
  124. }
  125. /** Returns a range with the same length as this one, but moved to have the given end position. */
  126. Range movedToEndAt (const ValueType newEnd) const noexcept
  127. {
  128. return Range (start + (newEnd - end), newEnd);
  129. }
  130. /** Changes the length of the range.
  131. Lengths less than zero are treated as zero.
  132. */
  133. void setLength (const ValueType newLength) noexcept
  134. {
  135. end = start + jmax (ValueType(), newLength);
  136. }
  137. /** Returns a range with the same start as this one, but a different length.
  138. Lengths less than zero are treated as zero.
  139. */
  140. Range withLength (const ValueType newLength) const noexcept
  141. {
  142. return Range (start, start + newLength);
  143. }
  144. //==============================================================================
  145. /** Adds an amount to the start and end of the range. */
  146. inline Range operator+= (const ValueType amountToAdd) noexcept
  147. {
  148. start += amountToAdd;
  149. end += amountToAdd;
  150. return *this;
  151. }
  152. /** Subtracts an amount from the start and end of the range. */
  153. inline Range operator-= (const ValueType amountToSubtract) noexcept
  154. {
  155. start -= amountToSubtract;
  156. end -= amountToSubtract;
  157. return *this;
  158. }
  159. /** Returns a range that is equal to this one with an amount added to its
  160. start and end.
  161. */
  162. Range operator+ (const ValueType amountToAdd) const noexcept
  163. {
  164. return Range (start + amountToAdd, end + amountToAdd);
  165. }
  166. /** Returns a range that is equal to this one with the specified amount
  167. subtracted from its start and end. */
  168. Range operator- (const ValueType amountToSubtract) const noexcept
  169. {
  170. return Range (start - amountToSubtract, end - amountToSubtract);
  171. }
  172. bool operator== (Range other) const noexcept { return start == other.start && end == other.end; }
  173. bool operator!= (Range other) const noexcept { return start != other.start || end != other.end; }
  174. //==============================================================================
  175. /** Returns true if the given position lies inside this range.
  176. When making this comparison, the start value is considered to be inclusive,
  177. and the end of the range exclusive.
  178. */
  179. bool contains (const ValueType position) const noexcept
  180. {
  181. return start <= position && position < end;
  182. }
  183. /** Returns the nearest value to the one supplied, which lies within the range. */
  184. ValueType clipValue (const ValueType value) const noexcept
  185. {
  186. return jlimit (start, end, value);
  187. }
  188. /** Returns true if the given range lies entirely inside this range. */
  189. bool contains (Range other) const noexcept
  190. {
  191. return start <= other.start && end >= other.end;
  192. }
  193. /** Returns true if the given range intersects this one. */
  194. bool intersects (Range other) const noexcept
  195. {
  196. return other.start < end && start < other.end;
  197. }
  198. /** Returns the range that is the intersection of the two ranges, or an empty range
  199. with an undefined start position if they don't overlap. */
  200. Range getIntersectionWith (Range other) const noexcept
  201. {
  202. return Range (jmax (start, other.start),
  203. jmin (end, other.end));
  204. }
  205. /** Returns the smallest range that contains both this one and the other one. */
  206. Range getUnionWith (Range other) const noexcept
  207. {
  208. return Range (jmin (start, other.start),
  209. jmax (end, other.end));
  210. }
  211. /** Returns the smallest range that contains both this one and the given value. */
  212. Range getUnionWith (const ValueType valueToInclude) const noexcept
  213. {
  214. return Range (jmin (valueToInclude, start),
  215. jmax (valueToInclude, end));
  216. }
  217. /** Returns a given range, after moving it forwards or backwards to fit it
  218. within this range.
  219. If the supplied range has a greater length than this one, the return value
  220. will be this range.
  221. Otherwise, if the supplied range is smaller than this one, the return value
  222. will be the new range, shifted forwards or backwards so that it doesn't extend
  223. beyond this one, but keeping its original length.
  224. */
  225. Range constrainRange (Range rangeToConstrain) const noexcept
  226. {
  227. const ValueType otherLen = rangeToConstrain.getLength();
  228. return getLength() <= otherLen
  229. ? *this
  230. : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
  231. }
  232. /** Scans an array of values for its min and max, and returns these as a Range. */
  233. static Range findMinAndMax (const ValueType* values, int numValues) noexcept
  234. {
  235. if (numValues <= 0)
  236. return Range();
  237. const ValueType first (*values++);
  238. Range r (first, first);
  239. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  240. {
  241. const ValueType v (*values++);
  242. if (r.end < v) r.end = v;
  243. if (v < r.start) r.start = v;
  244. }
  245. return r;
  246. }
  247. private:
  248. //==============================================================================
  249. ValueType start, end;
  250. };
  251. #endif // JUCE_RANGE_H_INCLUDED