OpenShot Audio Library | OpenShotAudio 0.3.2
Loading...
Searching...
No Matches
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
23namespace juce
24{
25
26//==============================================================================
38template <typename ValueType>
39class Range
40{
41public:
42 //==============================================================================
44 JUCE_CONSTEXPR Range() = default;
45
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
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
108 {
109 return Range (newStart, jmax (newStart, end));
110 }
111
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
134 {
135 return Range (jmin (start, newEnd), newEnd);
136 }
137
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
156 {
157 return Range (start, start + newLength);
158 }
159
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
180 {
181 start -= amountToSubtract;
182 end -= amountToSubtract;
183 return *this;
184 }
185
190 {
191 return Range (start + amountToAdd, end + amountToAdd);
192 }
193
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
235 {
236 return Range (jmax (start, other.start),
237 jmin (end, other.end));
238 }
239
242 {
243 return Range (jmin (start, other.start),
244 jmax (end, other.end));
245 }
246
249 {
250 return Range (jmin (valueToInclude, start),
251 jmax (valueToInclude, end));
252 }
253
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
292private:
293 //==============================================================================
294 ValueType start{}, end{};
295};
296
297} // namespace juce
ElementType * end() noexcept
Definition juce_Array.h:344
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
Range constrainRange(Range rangeToConstrain) const noexcept
Definition juce_Range.h:264
JUCE_CONSTEXPR Range withEnd(const ValueType newEnd) const noexcept
Definition juce_Range.h:133
JUCE_CONSTEXPR Range getIntersectionWith(Range other) const noexcept
Definition juce_Range.h:234
JUCE_CONSTEXPR Range expanded(ValueType amount) const noexcept
Definition juce_Range.h:164
JUCE_CONSTEXPR Range withStart(const ValueType newStart) const noexcept
Definition juce_Range.h:107
JUCE_CONSTEXPR ValueType getStart() const noexcept
Definition juce_Range.h:80
JUCE_CONSTEXPR Range operator+(const ValueType amountToAdd) const noexcept
Definition juce_Range.h:189
JUCE_CONSTEXPR Range movedToEndAt(const ValueType newEnd) const noexcept
Definition juce_Range.h:139
JUCE_CONSTEXPR bool contains(const ValueType position) const noexcept
Definition juce_Range.h:209
JUCE_CONSTEXPR Range operator-(const ValueType amountToSubtract) const noexcept
Definition juce_Range.h:196
JUCE_CONSTEXPR Range withLength(const ValueType newLength) const noexcept
Definition juce_Range.h:155
JUCE_CONSTEXPR bool isEmpty() const noexcept
Definition juce_Range.h:89
JUCE_CONSTEXPR Range()=default
ValueType clipValue(const ValueType value) const noexcept
Definition juce_Range.h:215
JUCE_CONSTEXPR ValueType getLength() const noexcept
Definition juce_Range.h:83
static Range findMinAndMax(const ValueType *values, int numValues) noexcept
Definition juce_Range.h:273
Range operator+=(const ValueType amountToAdd) noexcept
Definition juce_Range.h:171
JUCE_CONSTEXPR Range(const ValueType startValue, const ValueType endValue) noexcept
Definition juce_Range.h:47
JUCE_CONSTEXPR bool intersects(Range other) const noexcept
Definition juce_Range.h:227
static JUCE_CONSTEXPR Range between(const ValueType position1, const ValueType position2) noexcept
Definition juce_Range.h:59
void setLength(const ValueType newLength) noexcept
Definition juce_Range.h:147
void setStart(const ValueType newStart) noexcept
Definition juce_Range.h:96
Range & operator=(const Range &)=default
JUCE_CONSTEXPR Range(const Range &)=default
JUCE_CONSTEXPR bool contains(Range other) const noexcept
Definition juce_Range.h:221
Range operator-=(const ValueType amountToSubtract) noexcept
Definition juce_Range.h:179
JUCE_CONSTEXPR Range getUnionWith(Range other) const noexcept
Definition juce_Range.h:241
JUCE_CONSTEXPR Range getUnionWith(const ValueType valueToInclude) const noexcept
Definition juce_Range.h:248
static JUCE_CONSTEXPR Range emptyRange(const ValueType start) noexcept
Definition juce_Range.h:73
JUCE_CONSTEXPR ValueType getEnd() const noexcept
Definition juce_Range.h:86
JUCE_CONSTEXPR Range movedToStartAt(const ValueType newStart) const noexcept
Definition juce_Range.h:113