juce_Range.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 the specified start position and a length of zero. */
  64. static Range emptyRange (const ValueType start) noexcept
  65. {
  66. return Range (start, start);
  67. }
  68. //==============================================================================
  69. /** Returns the start of the range. */
  70. inline ValueType getStart() const noexcept { return start; }
  71. /** Returns the length of the range. */
  72. inline ValueType getLength() const noexcept { return end - start; }
  73. /** Returns the end of the range. */
  74. inline ValueType getEnd() const noexcept { return end; }
  75. /** Returns true if the range has a length of zero. */
  76. inline bool isEmpty() const noexcept { return start == end; }
  77. //==============================================================================
  78. /** Changes the start position of the range, leaving the end position unchanged.
  79. If the new start position is higher than the current end of the range, the end point
  80. will be pushed along to equal it, leaving an empty range at the new position.
  81. */
  82. void setStart (const ValueType newStart) noexcept
  83. {
  84. start = newStart;
  85. if (end < newStart)
  86. end = newStart;
  87. }
  88. /** Returns a range with the same end as this one, but a different start.
  89. If the new start position is higher than the current end of the range, the end point
  90. will be pushed along to equal it, returning an empty range at the new position.
  91. */
  92. Range withStart (const ValueType newStart) const noexcept
  93. {
  94. return Range (newStart, jmax (newStart, end));
  95. }
  96. /** Returns a range with the same length as this one, but moved to have the given start position. */
  97. Range movedToStartAt (const ValueType newStart) const noexcept
  98. {
  99. return Range (newStart, end + (newStart - start));
  100. }
  101. /** Changes the end position of the range, leaving the start unchanged.
  102. If the new end position is below the current start of the range, the start point
  103. will be pushed back to equal the new end point.
  104. */
  105. void setEnd (const ValueType newEnd) noexcept
  106. {
  107. end = newEnd;
  108. if (newEnd < start)
  109. start = newEnd;
  110. }
  111. /** Returns a range with the same start position as this one, but a different end.
  112. If the new end position is below the current start of the range, the start point
  113. will be pushed back to equal the new end point.
  114. */
  115. Range withEnd (const ValueType newEnd) const noexcept
  116. {
  117. return Range (jmin (start, newEnd), newEnd);
  118. }
  119. /** Returns a range with the same length as this one, but moved to have the given end position. */
  120. Range movedToEndAt (const ValueType newEnd) const noexcept
  121. {
  122. return Range (start + (newEnd - end), newEnd);
  123. }
  124. /** Changes the length of the range.
  125. Lengths less than zero are treated as zero.
  126. */
  127. void setLength (const ValueType newLength) noexcept
  128. {
  129. end = start + jmax (ValueType(), newLength);
  130. }
  131. /** Returns a range with the same start as this one, but a different length.
  132. Lengths less than zero are treated as zero.
  133. */
  134. Range withLength (const ValueType newLength) const noexcept
  135. {
  136. return Range (start, start + newLength);
  137. }
  138. //==============================================================================
  139. /** Adds an amount to the start and end of the range. */
  140. inline Range operator+= (const ValueType amountToAdd) noexcept
  141. {
  142. start += amountToAdd;
  143. end += amountToAdd;
  144. return *this;
  145. }
  146. /** Subtracts an amount from the start and end of the range. */
  147. inline Range operator-= (const ValueType amountToSubtract) noexcept
  148. {
  149. start -= amountToSubtract;
  150. end -= amountToSubtract;
  151. return *this;
  152. }
  153. /** Returns a range that is equal to this one with an amount added to its
  154. start and end.
  155. */
  156. Range operator+ (const ValueType amountToAdd) const noexcept
  157. {
  158. return Range (start + amountToAdd, end + amountToAdd);
  159. }
  160. /** Returns a range that is equal to this one with the specified amount
  161. subtracted from its start and end. */
  162. Range operator- (const ValueType amountToSubtract) const noexcept
  163. {
  164. return Range (start - amountToSubtract, end - amountToSubtract);
  165. }
  166. bool operator== (Range other) const noexcept { return start == other.start && end == other.end; }
  167. bool operator!= (Range other) const noexcept { return start != other.start || end != other.end; }
  168. //==============================================================================
  169. /** Returns true if the given position lies inside this range. */
  170. bool contains (const ValueType position) const noexcept
  171. {
  172. return start <= position && position < end;
  173. }
  174. /** Returns the nearest value to the one supplied, which lies within the range. */
  175. ValueType clipValue (const ValueType value) const noexcept
  176. {
  177. return jlimit (start, end, value);
  178. }
  179. /** Returns true if the given range lies entirely inside this range.
  180. When making this comparison, the start value is considered to be inclusive,
  181. and the end of the range exclusive.
  182. */
  183. bool contains (Range other) const noexcept
  184. {
  185. return start <= other.start && end >= other.end;
  186. }
  187. /** Returns true if the given range intersects this one. */
  188. bool intersects (Range other) const noexcept
  189. {
  190. return other.start < end && start < other.end;
  191. }
  192. /** Returns the range that is the intersection of the two ranges, or an empty range
  193. with an undefined start position if they don't overlap. */
  194. Range getIntersectionWith (Range other) const noexcept
  195. {
  196. return Range (jmax (start, other.start),
  197. jmin (end, other.end));
  198. }
  199. /** Returns the smallest range that contains both this one and the other one. */
  200. Range getUnionWith (Range other) const noexcept
  201. {
  202. return Range (jmin (start, other.start),
  203. jmax (end, other.end));
  204. }
  205. /** Returns the smallest range that contains both this one and the given value. */
  206. Range getUnionWith (const ValueType valueToInclude) const noexcept
  207. {
  208. return Range (jmin (valueToInclude, start),
  209. jmax (valueToInclude, end));
  210. }
  211. /** Returns a given range, after moving it forwards or backwards to fit it
  212. within this range.
  213. If the supplied range has a greater length than this one, the return value
  214. will be this range.
  215. Otherwise, if the supplied range is smaller than this one, the return value
  216. will be the new range, shifted forwards or backwards so that it doesn't extend
  217. beyond this one, but keeping its original length.
  218. */
  219. Range constrainRange (Range rangeToConstrain) const noexcept
  220. {
  221. const ValueType otherLen = rangeToConstrain.getLength();
  222. return getLength() <= otherLen
  223. ? *this
  224. : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
  225. }
  226. /** Scans an array of values for its min and max, and returns these as a Range. */
  227. static Range findMinAndMax (const ValueType* values, int numValues) noexcept
  228. {
  229. if (numValues <= 0)
  230. return Range();
  231. const ValueType first (*values++);
  232. Range r (first, first);
  233. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  234. {
  235. const ValueType v (*values++);
  236. if (r.end < v) r.end = v;
  237. if (v < r.start) r.start = v;
  238. }
  239. return r;
  240. }
  241. private:
  242. //==============================================================================
  243. ValueType start, end;
  244. };
  245. #endif // JUCE_RANGE_H_INCLUDED