OpenShot Audio Library | OpenShotAudio  0.3.1
juce_Range.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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  JUCE_CONSTEXPR Range() = default;
45 
47  JUCE_CONSTEXPR Range (const ValueType startValue, const ValueType endValue) noexcept
48  : start (startValue), end (jmax (startValue, endValue))
49  {
50  }
51 
53  JUCE_CONSTEXPR Range (const Range&) = default;
54 
56  Range& operator= (const Range&) = default;
57 
59  JUCE_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  static Range withStartAndLength (const ValueType startValue, const ValueType length) noexcept
67  {
68  jassert (length >= ValueType());
69  return Range (startValue, startValue + length);
70  }
71 
73  JUCE_CONSTEXPR static Range emptyRange (const ValueType start) noexcept
74  {
75  return Range (start, start);
76  }
77 
78  //==============================================================================
80  JUCE_CONSTEXPR inline ValueType getStart() const noexcept { return start; }
81 
83  JUCE_CONSTEXPR inline ValueType getLength() const noexcept { return end - start; }
84 
86  JUCE_CONSTEXPR inline ValueType getEnd() const noexcept { return end; }
87 
89  JUCE_CONSTEXPR inline bool isEmpty() const noexcept { return 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  JUCE_CONSTEXPR Range withStart (const ValueType newStart) const noexcept
108  {
109  return Range (newStart, jmax (newStart, end));
110  }
111 
113  JUCE_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  JUCE_CONSTEXPR Range withEnd (const ValueType newEnd) const noexcept
134  {
135  return Range (jmin (start, newEnd), newEnd);
136  }
137 
139  JUCE_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  JUCE_CONSTEXPR Range withLength (const ValueType newLength) const noexcept
156  {
157  return Range (start, start + newLength);
158  }
159 
164  JUCE_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  JUCE_CONSTEXPR Range operator+ (const ValueType amountToAdd) const noexcept
190  {
191  return Range (start + amountToAdd, end + amountToAdd);
192  }
193 
196  JUCE_CONSTEXPR Range operator- (const ValueType amountToSubtract) const noexcept
197  {
198  return Range (start - amountToSubtract, end - amountToSubtract);
199  }
200 
201  JUCE_CONSTEXPR bool operator== (Range other) const noexcept { return start == other.start && end == other.end; }
202  JUCE_CONSTEXPR bool operator!= (Range other) const noexcept { return start != other.start || end != other.end; }
203 
204  //==============================================================================
209  JUCE_CONSTEXPR bool contains (const ValueType position) const noexcept
210  {
211  return start <= position && position < end;
212  }
213 
215  ValueType clipValue (const ValueType value) const noexcept
216  {
217  return jlimit (start, end, value);
218  }
219 
221  JUCE_CONSTEXPR bool contains (Range other) const noexcept
222  {
223  return start <= other.start && end >= other.end;
224  }
225 
227  JUCE_CONSTEXPR bool intersects (Range other) const noexcept
228  {
229  return other.start < end && start < other.end;
230  }
231 
234  JUCE_CONSTEXPR Range getIntersectionWith (Range other) const noexcept
235  {
236  return Range (jmax (start, other.start),
237  jmin (end, other.end));
238  }
239 
241  JUCE_CONSTEXPR Range getUnionWith (Range other) const noexcept
242  {
243  return Range (jmin (start, other.start),
244  jmax (end, other.end));
245  }
246 
248  JUCE_CONSTEXPR Range getUnionWith (const ValueType valueToInclude) const noexcept
249  {
250  return Range (jmin (valueToInclude, start),
251  jmax (valueToInclude, end));
252  }
253 
264  Range constrainRange (Range rangeToConstrain) const noexcept
265  {
266  const ValueType otherLen = rangeToConstrain.getLength();
267  return getLength() <= otherLen
268  ? *this
269  : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
270  }
271 
273  static Range findMinAndMax (const ValueType* values, int numValues) noexcept
274  {
275  if (numValues <= 0)
276  return Range();
277 
278  const ValueType first (*values++);
279  Range r (first, first);
280 
281  while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
282  {
283  const ValueType v (*values++);
284 
285  if (r.end < v) r.end = v;
286  if (v < r.start) r.start = v;
287  }
288 
289  return r;
290  }
291 
292 private:
293  //==============================================================================
294  ValueType start{}, end{};
295 };
296 
297 } // namespace juce
JUCE_CONSTEXPR Range operator+(const ValueType amountToAdd) const noexcept
Definition: juce_Range.h:189
void setEnd(const ValueType newEnd) noexcept
Definition: juce_Range.h:122
JUCE_CONSTEXPR Range getUnionWith(const ValueType valueToInclude) const noexcept
Definition: juce_Range.h:248
Range constrainRange(Range rangeToConstrain) const noexcept
Definition: juce_Range.h:264
JUCE_CONSTEXPR Range movedToEndAt(const ValueType newEnd) const noexcept
Definition: juce_Range.h:139
void setLength(const ValueType newLength) noexcept
Definition: juce_Range.h:147
JUCE_CONSTEXPR Range getUnionWith(Range other) const noexcept
Definition: juce_Range.h:241
Range operator+=(const ValueType amountToAdd) noexcept
Definition: juce_Range.h:171
JUCE_CONSTEXPR Range operator-(const ValueType amountToSubtract) const noexcept
Definition: juce_Range.h:196
JUCE_CONSTEXPR bool contains(const ValueType position) const noexcept
Definition: juce_Range.h:209
JUCE_CONSTEXPR Range withLength(const ValueType newLength) const noexcept
Definition: juce_Range.h:155
Range & operator=(const Range &)=default
JUCE_CONSTEXPR Range movedToStartAt(const ValueType newStart) const noexcept
Definition: juce_Range.h:113
JUCE_CONSTEXPR bool intersects(Range other) const noexcept
Definition: juce_Range.h:227
JUCE_CONSTEXPR Range()=default
JUCE_CONSTEXPR ValueType getEnd() const noexcept
Definition: juce_Range.h:86
JUCE_CONSTEXPR Range withStart(const ValueType newStart) const noexcept
Definition: juce_Range.h:107
ValueType clipValue(const ValueType value) const noexcept
Definition: juce_Range.h:215
static JUCE_CONSTEXPR Range between(const ValueType position1, const ValueType position2) noexcept
Definition: juce_Range.h:59
JUCE_CONSTEXPR Range expanded(ValueType amount) const noexcept
Definition: juce_Range.h:164
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Definition: juce_Range.h:66
static Range findMinAndMax(const ValueType *values, int numValues) noexcept
Definition: juce_Range.h:273
Range operator-=(const ValueType amountToSubtract) noexcept
Definition: juce_Range.h:179
JUCE_CONSTEXPR ValueType getLength() const noexcept
Definition: juce_Range.h:83
JUCE_CONSTEXPR bool isEmpty() const noexcept
Definition: juce_Range.h:89
void setStart(const ValueType newStart) noexcept
Definition: juce_Range.h:96
JUCE_CONSTEXPR Range withEnd(const ValueType newEnd) const noexcept
Definition: juce_Range.h:133
JUCE_CONSTEXPR ValueType getStart() const noexcept
Definition: juce_Range.h:80
JUCE_CONSTEXPR Range(const ValueType startValue, const ValueType endValue) noexcept
Definition: juce_Range.h:47
JUCE_CONSTEXPR bool contains(Range other) const noexcept
Definition: juce_Range.h:221
static JUCE_CONSTEXPR Range emptyRange(const ValueType start) noexcept
Definition: juce_Range.h:73
JUCE_CONSTEXPR Range getIntersectionWith(Range other) const noexcept
Definition: juce_Range.h:234