OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Range.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
38 template <typename ValueType>
39 class Range
40 {
41 public:
42  //==============================================================================
44  constexpr Range() = default;
45 
47  constexpr Range (const ValueType startValue, const ValueType endValue) noexcept
48  : start (startValue), end (jmax (startValue, endValue))
49  {
50  }
51 
53  constexpr Range (const Range&) = default;
54 
56  Range& operator= (const Range&) = default;
57 
59  constexpr static Range between (const ValueType position1, const ValueType position2) noexcept
60  {
61  return position1 < position2 ? Range (position1, position2)
62  : Range (position2, position1);
63  }
64 
66  [[nodiscard]] static Range withStartAndLength (const ValueType startValue, const ValueType length) noexcept
67  {
68  jassert (length >= ValueType());
69  return Range (startValue, startValue + length);
70  }
71 
73  [[nodiscard]] constexpr static Range emptyRange (const ValueType start) noexcept
74  {
75  return Range (start, start);
76  }
77 
78  //==============================================================================
80  constexpr inline ValueType getStart() const noexcept { return start; }
81 
83  constexpr inline ValueType getLength() const noexcept { return end - start; }
84 
86  constexpr inline ValueType getEnd() const noexcept { return end; }
87 
89  constexpr inline bool isEmpty() const noexcept { return exactlyEqual (start, end); }
90 
91  //==============================================================================
96  void setStart (const ValueType newStart) noexcept
97  {
98  start = newStart;
99  if (end < newStart)
100  end = newStart;
101  }
102 
107  [[nodiscard]] constexpr Range withStart (const ValueType newStart) const noexcept
108  {
109  return Range (newStart, jmax (newStart, end));
110  }
111 
113  [[nodiscard]] constexpr Range movedToStartAt (const ValueType newStart) const noexcept
114  {
115  return Range (newStart, end + (newStart - start));
116  }
117 
122  void setEnd (const ValueType newEnd) noexcept
123  {
124  end = newEnd;
125  if (newEnd < start)
126  start = newEnd;
127  }
128 
133  [[nodiscard]] constexpr Range withEnd (const ValueType newEnd) const noexcept
134  {
135  return Range (jmin (start, newEnd), newEnd);
136  }
137 
139  [[nodiscard]] constexpr Range movedToEndAt (const ValueType newEnd) const noexcept
140  {
141  return Range (start + (newEnd - end), newEnd);
142  }
143 
147  void setLength (const ValueType newLength) noexcept
148  {
149  end = start + jmax (ValueType(), newLength);
150  }
151 
155  [[nodiscard]] constexpr Range withLength (const ValueType newLength) const noexcept
156  {
157  return Range (start, start + newLength);
158  }
159 
164  [[nodiscard]] constexpr Range expanded (ValueType amount) const noexcept
165  {
166  return Range (start - amount, end + amount);
167  }
168 
169  //==============================================================================
171  inline Range operator+= (const ValueType amountToAdd) noexcept
172  {
173  start += amountToAdd;
174  end += amountToAdd;
175  return *this;
176  }
177 
179  inline Range operator-= (const ValueType amountToSubtract) noexcept
180  {
181  start -= amountToSubtract;
182  end -= amountToSubtract;
183  return *this;
184  }
185 
189  constexpr Range operator+ (const ValueType amountToAdd) const noexcept
190  {
191  return Range (start + amountToAdd, end + amountToAdd);
192  }
193 
196  constexpr Range operator- (const ValueType amountToSubtract) const noexcept
197  {
198  return Range (start - amountToSubtract, end - amountToSubtract);
199  }
200 
201  constexpr bool operator== (Range other) const noexcept
202  {
203  const auto tie = [] (const Range& r) { return std::tie (r.start, r.end); };
204  return tie (*this) == tie (other);
205  }
206 
207  constexpr bool operator!= (Range other) const noexcept { return ! operator== (other); }
208 
209  //==============================================================================
214  constexpr bool contains (const ValueType position) const noexcept
215  {
216  return start <= position && position < end;
217  }
218 
220  ValueType clipValue (const ValueType value) const noexcept
221  {
222  return jlimit (start, end, value);
223  }
224 
226  constexpr bool contains (Range other) const noexcept
227  {
228  return start <= other.start && end >= other.end;
229  }
230 
232  constexpr bool intersects (Range other) const noexcept
233  {
234  return other.start < end && start < other.end;
235  }
236 
239  [[nodiscard]] constexpr Range getIntersectionWith (Range other) const noexcept
240  {
241  return Range (jmax (start, other.start),
242  jmin (end, other.end));
243  }
244 
246  [[nodiscard]] constexpr Range getUnionWith (Range other) const noexcept
247  {
248  return Range (jmin (start, other.start),
249  jmax (end, other.end));
250  }
251 
253  [[nodiscard]] constexpr Range getUnionWith (const ValueType valueToInclude) const noexcept
254  {
255  return Range (jmin (valueToInclude, start),
256  jmax (valueToInclude, end));
257  }
258 
269  Range constrainRange (Range rangeToConstrain) const noexcept
270  {
271  const ValueType otherLen = rangeToConstrain.getLength();
272  return getLength() <= otherLen
273  ? *this
274  : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
275  }
276 
278  template <typename Integral, std::enable_if_t<std::is_integral_v<Integral>, int> = 0>
279  static Range findMinAndMax (const ValueType* values, Integral numValues) noexcept
280  {
281  if (numValues <= 0)
282  return Range();
283 
284  const ValueType first (*values++);
285  Range r (first, first);
286 
287  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
288  {
289  const ValueType v (*values++);
290 
291  if (r.end < v) r.end = v;
292  if (v < r.start) r.start = v;
293  }
294 
295  return r;
296  }
297 
298 private:
299  //==============================================================================
300  ValueType start{}, end{};
301 };
302 
303 } // namespace juce
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Definition: juce_Range.h:66
void setEnd(const ValueType newEnd) noexcept
Definition: juce_Range.h:122
constexpr Range expanded(ValueType amount) const noexcept
Definition: juce_Range.h:164
Range constrainRange(Range rangeToConstrain) const noexcept
Definition: juce_Range.h:269
constexpr ValueType getStart() const noexcept
Definition: juce_Range.h:80
constexpr Range operator-(const ValueType amountToSubtract) const noexcept
Definition: juce_Range.h:196
constexpr bool contains(Range other) const noexcept
Definition: juce_Range.h:226
constexpr bool isEmpty() const noexcept
Definition: juce_Range.h:89
constexpr Range getIntersectionWith(Range other) const noexcept
Definition: juce_Range.h:239
Range & operator=(const Range &)=default
constexpr bool intersects(Range other) const noexcept
Definition: juce_Range.h:232
constexpr ValueType getEnd() const noexcept
Definition: juce_Range.h:86
constexpr Range operator+(const ValueType amountToAdd) const noexcept
Definition: juce_Range.h:189
constexpr Range withEnd(const ValueType newEnd) const noexcept
Definition: juce_Range.h:133
constexpr Range(const ValueType startValue, const ValueType endValue) noexcept
Definition: juce_Range.h:47
constexpr Range(const Range &)=default
constexpr static Range emptyRange(const ValueType start) noexcept
Definition: juce_Range.h:73
constexpr Range getUnionWith(Range other) const noexcept
Definition: juce_Range.h:246
static Range findMinAndMax(const ValueType *values, Integral numValues) noexcept
Definition: juce_Range.h:279
constexpr static Range between(const ValueType position1, const ValueType position2) noexcept
Definition: juce_Range.h:59
ValueType clipValue(const ValueType value) const noexcept
Definition: juce_Range.h:220
constexpr Range movedToStartAt(const ValueType newStart) const noexcept
Definition: juce_Range.h:113
constexpr Range withLength(const ValueType newLength) const noexcept
Definition: juce_Range.h:155
constexpr Range getUnionWith(const ValueType valueToInclude) const noexcept
Definition: juce_Range.h:253
Range operator+=(const ValueType amountToAdd) noexcept
Definition: juce_Range.h:171
void setLength(const ValueType newLength) noexcept
Definition: juce_Range.h:147
void setStart(const ValueType newStart) noexcept
Definition: juce_Range.h:96
constexpr ValueType getLength() const noexcept
Definition: juce_Range.h:83
constexpr bool contains(const ValueType position) const noexcept
Definition: juce_Range.h:214
Range operator-=(const ValueType amountToSubtract) noexcept
Definition: juce_Range.h:179
constexpr Range()=default
constexpr Range withStart(const ValueType newStart) const noexcept
Definition: juce_Range.h:107
constexpr Range movedToEndAt(const ValueType newEnd) const noexcept
Definition: juce_Range.h:139