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.

265 lines
10.0KB

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