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.

268 lines
9.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_RANGE_JUCEHEADER__
  19. #define __JUCE_RANGE_JUCEHEADER__
  20. //==============================================================================
  21. /** A general-purpose range object, that simply represents any linear range with
  22. a start and end point.
  23. The templated parameter is expected to be a primitive integer or floating point
  24. type, though class types could also be used if they behave in a number-like way.
  25. */
  26. template <typename ValueType>
  27. class Range
  28. {
  29. public:
  30. //==============================================================================
  31. /** Constructs an empty range. */
  32. Range() noexcept
  33. : start (ValueType()), end (ValueType())
  34. {
  35. }
  36. /** Constructs a range with given start and end values. */
  37. Range (const ValueType start_, const ValueType end_) noexcept
  38. : start (start_), end (jmax (start_, end_))
  39. {
  40. }
  41. /** Constructs a copy of another range. */
  42. Range (const Range& other) noexcept
  43. : start (other.start), end (other.end)
  44. {
  45. }
  46. /** Copies another range object. */
  47. Range& operator= (const Range& other) noexcept
  48. {
  49. start = other.start;
  50. end = other.end;
  51. return *this;
  52. }
  53. /** Destructor. */
  54. ~Range() noexcept
  55. {
  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 start 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 const 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 const 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== (const Range& other) const noexcept { return start == other.start && end == other.end; }
  167. bool operator!= (const 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. bool contains (const Range& other) const noexcept
  181. {
  182. return start <= other.start && end >= other.end;
  183. }
  184. /** Returns true if the given range intersects this one. */
  185. bool intersects (const Range& other) const noexcept
  186. {
  187. return other.start < end && start < other.end;
  188. }
  189. /** Returns the range that is the intersection of the two ranges, or an empty range
  190. with an undefined start position if they don't overlap. */
  191. Range getIntersectionWith (const Range& other) const noexcept
  192. {
  193. return Range (jmax (start, other.start),
  194. jmin (end, other.end));
  195. }
  196. /** Returns the smallest range that contains both this one and the other one. */
  197. Range getUnionWith (const Range& other) const noexcept
  198. {
  199. return Range (jmin (start, other.start),
  200. jmax (end, other.end));
  201. }
  202. /** Returns a given range, after moving it forwards or backwards to fit it
  203. within this range.
  204. If the supplied range has a greater length than this one, the return value
  205. will be this range.
  206. Otherwise, if the supplied range is smaller than this one, the return value
  207. will be the new range, shifted forwards or backwards so that it doesn't extend
  208. beyond this one, but keeping its original length.
  209. */
  210. Range constrainRange (const Range& rangeToConstrain) const noexcept
  211. {
  212. const ValueType otherLen = rangeToConstrain.getLength();
  213. return getLength() <= otherLen
  214. ? *this
  215. : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
  216. }
  217. private:
  218. //==============================================================================
  219. ValueType start, end;
  220. };
  221. #endif // __JUCE_RANGE_JUCEHEADER__