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.

312 lines
12KB

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