The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
11KB

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