openshot-audio  0.1.5
juce_Range.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_RANGE_H_INCLUDED
30 #define JUCE_RANGE_H_INCLUDED
31 
32 
33 //==============================================================================
43 template <typename ValueType>
44 class Range
45 {
46 public:
47  //==============================================================================
49  Range() noexcept : start(), end()
50  {
51  }
52 
54  Range (const ValueType startValue, const ValueType endValue) noexcept
55  : start (startValue), end (jmax (startValue, endValue))
56  {
57  }
58 
60  Range (const Range& other) noexcept
61  : start (other.start), end (other.end)
62  {
63  }
64 
67  {
68  start = other.start;
69  end = other.end;
70  return *this;
71  }
72 
74  static Range between (const ValueType position1, const ValueType position2) noexcept
75  {
76  return position1 < position2 ? Range (position1, position2)
77  : Range (position2, position1);
78  }
79 
81  static Range withStartAndLength (const ValueType startValue, const ValueType length) noexcept
82  {
83  jassert (length >= ValueType());
84  return Range (startValue, startValue + length);
85  }
86 
88  static Range emptyRange (const ValueType start) noexcept
89  {
90  return Range (start, start);
91  }
92 
93  //==============================================================================
95  inline ValueType getStart() const noexcept { return start; }
96 
98  inline ValueType getLength() const noexcept { return end - start; }
99 
101  inline ValueType getEnd() const noexcept { return end; }
102 
104  inline bool isEmpty() const noexcept { return start == end; }
105 
106  //==============================================================================
111  void setStart (const ValueType newStart) noexcept
112  {
113  start = newStart;
114  if (end < newStart)
115  end = newStart;
116  }
117 
122  Range withStart (const ValueType newStart) const noexcept
123  {
124  return Range (newStart, jmax (newStart, end));
125  }
126 
128  Range movedToStartAt (const ValueType newStart) const noexcept
129  {
130  return Range (newStart, end + (newStart - start));
131  }
132 
137  void setEnd (const ValueType newEnd) noexcept
138  {
139  end = newEnd;
140  if (newEnd < start)
141  start = newEnd;
142  }
143 
148  Range withEnd (const ValueType newEnd) const noexcept
149  {
150  return Range (jmin (start, newEnd), newEnd);
151  }
152 
154  Range movedToEndAt (const ValueType newEnd) const noexcept
155  {
156  return Range (start + (newEnd - end), newEnd);
157  }
158 
162  void setLength (const ValueType newLength) noexcept
163  {
164  end = start + jmax (ValueType(), newLength);
165  }
166 
170  Range withLength (const ValueType newLength) const noexcept
171  {
172  return Range (start, start + newLength);
173  }
174 
175  //==============================================================================
177  inline Range operator+= (const ValueType amountToAdd) noexcept
178  {
179  start += amountToAdd;
180  end += amountToAdd;
181  return *this;
182  }
183 
185  inline Range operator-= (const ValueType amountToSubtract) noexcept
186  {
187  start -= amountToSubtract;
188  end -= amountToSubtract;
189  return *this;
190  }
191 
195  Range operator+ (const ValueType amountToAdd) const noexcept
196  {
197  return Range (start + amountToAdd, end + amountToAdd);
198  }
199 
202  Range operator- (const ValueType amountToSubtract) const noexcept
203  {
204  return Range (start - amountToSubtract, end - amountToSubtract);
205  }
206 
207  bool operator== (Range other) const noexcept { return start == other.start && end == other.end; }
208  bool operator!= (Range other) const noexcept { return start != other.start || end != other.end; }
209 
210  //==============================================================================
212  bool contains (const ValueType position) const noexcept
213  {
214  return start <= position && position < end;
215  }
216 
218  ValueType clipValue (const ValueType value) const noexcept
219  {
220  return jlimit (start, end, value);
221  }
222 
227  bool contains (Range other) const noexcept
228  {
229  return start <= other.start && end >= other.end;
230  }
231 
233  bool intersects (Range other) const noexcept
234  {
235  return other.start < end && start < other.end;
236  }
237 
241  {
242  return Range (jmax (start, other.start),
243  jmin (end, other.end));
244  }
245 
248  {
249  return Range (jmin (start, other.start),
250  jmax (end, other.end));
251  }
252 
254  Range getUnionWith (const ValueType valueToInclude) const noexcept
255  {
256  return Range (jmin (valueToInclude, start),
257  jmax (valueToInclude, end));
258  }
259 
270  Range constrainRange (Range rangeToConstrain) const noexcept
271  {
272  const ValueType otherLen = rangeToConstrain.getLength();
273  return getLength() <= otherLen
274  ? *this
275  : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
276  }
277 
279  static Range findMinAndMax (const ValueType* values, int 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 
304 #endif // JUCE_RANGE_H_INCLUDED
Range getIntersectionWith(Range other) const noexcept
Definition: juce_Range.h:240
Range movedToStartAt(const ValueType newStart) const noexcept
Definition: juce_Range.h:128
Range movedToEndAt(const ValueType newEnd) const noexcept
Definition: juce_Range.h:154
Range operator-(const ValueType amountToSubtract) const noexcept
Definition: juce_Range.h:202
#define noexcept
Definition: juce_CompilerSupport.h:141
static Range between(const ValueType position1, const ValueType position2) noexcept
Definition: juce_Range.h:74
Range withEnd(const ValueType newEnd) const noexcept
Definition: juce_Range.h:148
Type jmin(const Type a, const Type b)
Definition: juce_core.h:113
void setEnd(const ValueType newEnd) noexcept
Definition: juce_Range.h:137
Range getUnionWith(const ValueType valueToInclude) const noexcept
Definition: juce_Range.h:254
Definition: juce_Range.h:44
bool operator==(Range other) const noexcept
Definition: juce_Range.h:207
static Range findMinAndMax(const ValueType *values, int numValues) noexcept
Definition: juce_Range.h:279
ValueType clipValue(const ValueType value) const noexcept
Definition: juce_Range.h:218
ValueType getLength() const noexcept
Definition: juce_Range.h:98
Range operator-=(const ValueType amountToSubtract) noexcept
Definition: juce_Range.h:185
void setLength(const ValueType newLength) noexcept
Definition: juce_Range.h:162
Type jmax(const Type a, const Type b)
Definition: juce_core.h:101
Range constrainRange(Range rangeToConstrain) const noexcept
Definition: juce_Range.h:270
static Range emptyRange(const ValueType start) noexcept
Definition: juce_Range.h:88
bool contains(const ValueType position) const noexcept
Definition: juce_Range.h:212
bool contains(Range other) const noexcept
Definition: juce_Range.h:227
Range withLength(const ValueType newLength) const noexcept
Definition: juce_Range.h:170
bool intersects(Range other) const noexcept
Definition: juce_Range.h:233
void setStart(const ValueType newStart) noexcept
Definition: juce_Range.h:111
Range getUnionWith(Range other) const noexcept
Definition: juce_Range.h:247
Type jlimit(const Type lowerLimit, const Type upperLimit, const Type valueToConstrain) noexcept
Definition: juce_MathsFunctions.h:220
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Definition: juce_Range.h:81
Range(const ValueType startValue, const ValueType endValue) noexcept
Definition: juce_Range.h:54
#define jassert(a)
Definition: juce_PlatformDefs.h:146
Range operator+=(const ValueType amountToAdd) noexcept
Definition: juce_Range.h:177
Range & operator=(Range other) noexcept
Definition: juce_Range.h:66
Range(const Range &other) noexcept
Definition: juce_Range.h:60
Range withStart(const ValueType newStart) const noexcept
Definition: juce_Range.h:122
bool operator!=(Range other) const noexcept
Definition: juce_Range.h:208
ValueType getStart() const noexcept
Definition: juce_Range.h:95
bool isEmpty() const noexcept
Definition: juce_Range.h:104
Range() noexcept
Definition: juce_Range.h:49
Range operator+(const ValueType amountToAdd) const noexcept
Definition: juce_Range.h:195
ValueType getEnd() const noexcept
Definition: juce_Range.h:101