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.

262 lines
9.7KB

  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 : start(), end()
  33. {
  34. }
  35. /** Constructs a range with given start and end values. */
  36. Range (const ValueType startValue, const ValueType endValue) noexcept
  37. : start (startValue), end (jmax (startValue, endValue))
  38. {
  39. }
  40. /** Constructs a copy of another range. */
  41. Range (const Range& other) noexcept
  42. : start (other.start), end (other.end)
  43. {
  44. }
  45. /** Copies another range object. */
  46. Range& operator= (Range other) noexcept
  47. {
  48. start = other.start;
  49. end = other.end;
  50. return *this;
  51. }
  52. /** Returns the range that lies between two positions (in either order). */
  53. static Range between (const ValueType position1, const ValueType position2) noexcept
  54. {
  55. return position1 < position2 ? Range (position1, position2)
  56. : Range (position2, position1);
  57. }
  58. /** Returns a range with the specified start position and a length of zero. */
  59. static Range emptyRange (const ValueType start) noexcept
  60. {
  61. return Range (start, start);
  62. }
  63. //==============================================================================
  64. /** Returns the start of the range. */
  65. inline ValueType getStart() const noexcept { return start; }
  66. /** Returns the length of the range. */
  67. inline ValueType getLength() const noexcept { return end - start; }
  68. /** Returns the end of the range. */
  69. inline ValueType getEnd() const noexcept { return end; }
  70. /** Returns true if the range has a length of zero. */
  71. inline bool isEmpty() const noexcept { return start == end; }
  72. //==============================================================================
  73. /** Changes the start position of the range, leaving the end position unchanged.
  74. If the new start position is higher than the current end of the range, the end point
  75. will be pushed along to equal it, leaving an empty range at the new position.
  76. */
  77. void setStart (const ValueType newStart) noexcept
  78. {
  79. start = newStart;
  80. if (end < newStart)
  81. end = newStart;
  82. }
  83. /** Returns a range with the same end as this one, but a different start.
  84. If the new start position is higher than the current end of the range, the end point
  85. will be pushed along to equal it, returning an empty range at the new position.
  86. */
  87. Range withStart (const ValueType newStart) const noexcept
  88. {
  89. return Range (newStart, jmax (newStart, end));
  90. }
  91. /** Returns a range with the same length as this one, but moved to have the given start position. */
  92. Range movedToStartAt (const ValueType newStart) const noexcept
  93. {
  94. return Range (newStart, end + (newStart - start));
  95. }
  96. /** Changes the end position of the range, leaving the start unchanged.
  97. If the new end position is below the current start of the range, the start point
  98. will be pushed back to equal the new end point.
  99. */
  100. void setEnd (const ValueType newEnd) noexcept
  101. {
  102. end = newEnd;
  103. if (newEnd < start)
  104. start = newEnd;
  105. }
  106. /** Returns a range with the same start position as this one, but a different end.
  107. If the new end position is below the current start of the range, the start point
  108. will be pushed back to equal the new end point.
  109. */
  110. Range withEnd (const ValueType newEnd) const noexcept
  111. {
  112. return Range (jmin (start, newEnd), newEnd);
  113. }
  114. /** Returns a range with the same length as this one, but moved to have the given end position. */
  115. Range movedToEndAt (const ValueType newEnd) const noexcept
  116. {
  117. return Range (start + (newEnd - end), newEnd);
  118. }
  119. /** Changes the length of the range.
  120. Lengths less than zero are treated as zero.
  121. */
  122. void setLength (const ValueType newLength) noexcept
  123. {
  124. end = start + jmax (ValueType(), newLength);
  125. }
  126. /** Returns a range with the same start as this one, but a different length.
  127. Lengths less than zero are treated as zero.
  128. */
  129. Range withLength (const ValueType newLength) const noexcept
  130. {
  131. return Range (start, start + newLength);
  132. }
  133. //==============================================================================
  134. /** Adds an amount to the start and end of the range. */
  135. inline Range operator+= (const ValueType amountToAdd) noexcept
  136. {
  137. start += amountToAdd;
  138. end += amountToAdd;
  139. return *this;
  140. }
  141. /** Subtracts an amount from the start and end of the range. */
  142. inline Range operator-= (const ValueType amountToSubtract) noexcept
  143. {
  144. start -= amountToSubtract;
  145. end -= amountToSubtract;
  146. return *this;
  147. }
  148. /** Returns a range that is equal to this one with an amount added to its
  149. start and end.
  150. */
  151. Range operator+ (const ValueType amountToAdd) const noexcept
  152. {
  153. return Range (start + amountToAdd, end + amountToAdd);
  154. }
  155. /** Returns a range that is equal to this one with the specified amount
  156. subtracted from its start and end. */
  157. Range operator- (const ValueType amountToSubtract) const noexcept
  158. {
  159. return Range (start - amountToSubtract, end - amountToSubtract);
  160. }
  161. bool operator== (Range other) const noexcept { return start == other.start && end == other.end; }
  162. bool operator!= (Range other) const noexcept { return start != other.start || end != other.end; }
  163. //==============================================================================
  164. /** Returns true if the given position lies inside this range. */
  165. bool contains (const ValueType position) const noexcept
  166. {
  167. return start <= position && position < end;
  168. }
  169. /** Returns the nearest value to the one supplied, which lies within the range. */
  170. ValueType clipValue (const ValueType value) const noexcept
  171. {
  172. return jlimit (start, end, value);
  173. }
  174. /** Returns true if the given range lies entirely inside this range. */
  175. bool contains (Range other) const noexcept
  176. {
  177. return start <= other.start && end >= other.end;
  178. }
  179. /** Returns true if the given range intersects this one. */
  180. bool intersects (Range other) const noexcept
  181. {
  182. return other.start < end && start < other.end;
  183. }
  184. /** Returns the range that is the intersection of the two ranges, or an empty range
  185. with an undefined start position if they don't overlap. */
  186. Range getIntersectionWith (Range other) const noexcept
  187. {
  188. return Range (jmax (start, other.start),
  189. jmin (end, other.end));
  190. }
  191. /** Returns the smallest range that contains both this one and the other one. */
  192. Range getUnionWith (Range other) const noexcept
  193. {
  194. return Range (jmin (start, other.start),
  195. jmax (end, other.end));
  196. }
  197. /** Returns a given range, after moving it forwards or backwards to fit it
  198. within this range.
  199. If the supplied range has a greater length than this one, the return value
  200. will be this range.
  201. Otherwise, if the supplied range is smaller than this one, the return value
  202. will be the new range, shifted forwards or backwards so that it doesn't extend
  203. beyond this one, but keeping its original length.
  204. */
  205. Range constrainRange (Range rangeToConstrain) const noexcept
  206. {
  207. const ValueType otherLen = rangeToConstrain.getLength();
  208. return getLength() <= otherLen
  209. ? *this
  210. : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
  211. }
  212. private:
  213. //==============================================================================
  214. ValueType start, end;
  215. };
  216. #endif // __JUCE_RANGE_JUCEHEADER__