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.

304 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. constexpr Range() = default;
  35. /** Constructs a range with given start and end values. */
  36. constexpr 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. constexpr Range (const Range&) = default;
  42. /** Copies another range object. */
  43. Range& operator= (const Range&) = default;
  44. /** Returns the range that lies between two positions (in either order). */
  45. constexpr static Range between (const ValueType position1, const ValueType position2) noexcept
  46. {
  47. return position1 < position2 ? Range (position1, position2)
  48. : Range (position2, position1);
  49. }
  50. /** Returns a range with a given start and length. */
  51. [[nodiscard]] static Range withStartAndLength (const ValueType startValue, const ValueType length) noexcept
  52. {
  53. jassert (length >= ValueType());
  54. return Range (startValue, startValue + length);
  55. }
  56. /** Returns a range with the specified start position and a length of zero. */
  57. [[nodiscard]] constexpr static Range emptyRange (const ValueType start) noexcept
  58. {
  59. return Range (start, start);
  60. }
  61. //==============================================================================
  62. /** Returns the start of the range. */
  63. constexpr inline ValueType getStart() const noexcept { return start; }
  64. /** Returns the length of the range. */
  65. constexpr inline ValueType getLength() const noexcept { return end - start; }
  66. /** Returns the end of the range. */
  67. constexpr inline ValueType getEnd() const noexcept { return end; }
  68. /** Returns true if the range has a length of zero. */
  69. constexpr inline bool isEmpty() const noexcept { return exactlyEqual (start, end); }
  70. //==============================================================================
  71. /** Changes the start position of the range, leaving the end position unchanged.
  72. If the new start position is higher than the current end of the range, the end point
  73. will be pushed along to equal it, leaving an empty range at the new position.
  74. */
  75. void setStart (const ValueType newStart) noexcept
  76. {
  77. start = newStart;
  78. if (end < newStart)
  79. end = newStart;
  80. }
  81. /** Returns a range with the same end as this one, but a different start.
  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, returning an empty range at the new position.
  84. */
  85. [[nodiscard]] constexpr Range withStart (const ValueType newStart) const noexcept
  86. {
  87. return Range (newStart, jmax (newStart, end));
  88. }
  89. /** Returns a range with the same length as this one, but moved to have the given start position. */
  90. [[nodiscard]] constexpr Range movedToStartAt (const ValueType newStart) const noexcept
  91. {
  92. return Range (newStart, end + (newStart - start));
  93. }
  94. /** Changes the end position of the range, leaving the start unchanged.
  95. If the new end position is below the current start of the range, the start point
  96. will be pushed back to equal the new end point.
  97. */
  98. void setEnd (const ValueType newEnd) noexcept
  99. {
  100. end = newEnd;
  101. if (newEnd < start)
  102. start = newEnd;
  103. }
  104. /** Returns a range with the same start position as this one, but a different end.
  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. [[nodiscard]] constexpr Range withEnd (const ValueType newEnd) const noexcept
  109. {
  110. return Range (jmin (start, newEnd), newEnd);
  111. }
  112. /** Returns a range with the same length as this one, but moved to have the given end position. */
  113. [[nodiscard]] constexpr Range movedToEndAt (const ValueType newEnd) const noexcept
  114. {
  115. return Range (start + (newEnd - end), newEnd);
  116. }
  117. /** Changes the length of the range.
  118. Lengths less than zero are treated as zero.
  119. */
  120. void setLength (const ValueType newLength) noexcept
  121. {
  122. end = start + jmax (ValueType(), newLength);
  123. }
  124. /** Returns a range with the same start as this one, but a different length.
  125. Lengths less than zero are treated as zero.
  126. */
  127. [[nodiscard]] constexpr Range withLength (const ValueType newLength) const noexcept
  128. {
  129. return Range (start, start + newLength);
  130. }
  131. /** Returns a range which has its start moved down and its end moved up by the
  132. given amount.
  133. @returns The returned range will be (start - amount, end + amount)
  134. */
  135. [[nodiscard]] constexpr Range expanded (ValueType amount) const noexcept
  136. {
  137. return Range (start - amount, end + amount);
  138. }
  139. //==============================================================================
  140. /** Adds an amount to the start and end of the range. */
  141. inline Range operator+= (const ValueType amountToAdd) noexcept
  142. {
  143. start += amountToAdd;
  144. end += amountToAdd;
  145. return *this;
  146. }
  147. /** Subtracts an amount from the start and end of the range. */
  148. inline Range operator-= (const ValueType amountToSubtract) noexcept
  149. {
  150. start -= amountToSubtract;
  151. end -= amountToSubtract;
  152. return *this;
  153. }
  154. /** Returns a range that is equal to this one with an amount added to its
  155. start and end.
  156. */
  157. constexpr Range operator+ (const ValueType amountToAdd) const noexcept
  158. {
  159. return Range (start + amountToAdd, end + amountToAdd);
  160. }
  161. /** Returns a range that is equal to this one with the specified amount
  162. subtracted from its start and end. */
  163. constexpr Range operator- (const ValueType amountToSubtract) const noexcept
  164. {
  165. return Range (start - amountToSubtract, end - amountToSubtract);
  166. }
  167. constexpr bool operator== (Range other) const noexcept
  168. {
  169. const auto tie = [] (const Range& r) { return std::tie (r.start, r.end); };
  170. return tie (*this) == tie (other);
  171. }
  172. constexpr bool operator!= (Range other) const noexcept { return ! operator== (other); }
  173. //==============================================================================
  174. /** Returns true if the given position lies inside this range.
  175. When making this comparison, the start value is considered to be inclusive,
  176. and the end of the range exclusive.
  177. */
  178. constexpr bool contains (const ValueType position) const noexcept
  179. {
  180. return start <= position && position < end;
  181. }
  182. /** Returns the nearest value to the one supplied, which lies within the range. */
  183. ValueType clipValue (const ValueType value) const noexcept
  184. {
  185. return jlimit (start, end, value);
  186. }
  187. /** Returns true if the given range lies entirely inside this range. */
  188. constexpr bool contains (Range other) const noexcept
  189. {
  190. return start <= other.start && end >= other.end;
  191. }
  192. /** Returns true if the given range intersects this one. */
  193. constexpr bool intersects (Range other) const noexcept
  194. {
  195. return other.start < end && start < other.end;
  196. }
  197. /** Returns the range that is the intersection of the two ranges, or an empty range
  198. with an undefined start position if they don't overlap. */
  199. [[nodiscard]] constexpr Range getIntersectionWith (Range other) const noexcept
  200. {
  201. return Range (jmax (start, other.start),
  202. jmin (end, other.end));
  203. }
  204. /** Returns the smallest range that contains both this one and the other one. */
  205. [[nodiscard]] constexpr Range getUnionWith (Range other) const noexcept
  206. {
  207. return Range (jmin (start, other.start),
  208. jmax (end, other.end));
  209. }
  210. /** Returns the smallest range that contains both this one and the given value. */
  211. [[nodiscard]] constexpr Range getUnionWith (const ValueType valueToInclude) const noexcept
  212. {
  213. return Range (jmin (valueToInclude, start),
  214. jmax (valueToInclude, end));
  215. }
  216. /** Returns a given range, after moving it forwards or backwards to fit it
  217. within this range.
  218. If the supplied range has a greater length than this one, the return value
  219. will be this range.
  220. Otherwise, if the supplied range is smaller than this one, the return value
  221. will be the new range, shifted forwards or backwards so that it doesn't extend
  222. beyond this one, but keeping its original length.
  223. */
  224. Range constrainRange (Range rangeToConstrain) const noexcept
  225. {
  226. const ValueType otherLen = rangeToConstrain.getLength();
  227. return getLength() <= otherLen
  228. ? *this
  229. : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
  230. }
  231. /** Scans an array of values for its min and max, and returns these as a Range. */
  232. template <typename Integral, std::enable_if_t<std::is_integral_v<Integral>, int> = 0>
  233. static Range findMinAndMax (const ValueType* values, Integral numValues) noexcept
  234. {
  235. if (numValues <= 0)
  236. return Range();
  237. const ValueType first (*values++);
  238. Range r (first, first);
  239. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  240. {
  241. const ValueType v (*values++);
  242. if (r.end < v) r.end = v;
  243. if (v < r.start) r.start = v;
  244. }
  245. return r;
  246. }
  247. private:
  248. //==============================================================================
  249. ValueType start{}, end{};
  250. };
  251. } // namespace juce