Loading...
Searching...
No Matches
juce_Range.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
template <typename ValueType>
class Range
{
public:
//==============================================================================
constexpr Range() = default;
constexpr Range (const ValueType startValue, const ValueType endValue) noexcept
: start (startValue), end (jmax (startValue, endValue))
{
}
constexpr Range (const Range&) = default;
Range& operator= (const Range&) = default;
constexpr static Range between (const ValueType position1, const ValueType position2) noexcept
{
return position1 < position2 ? Range (position1, position2)
: Range (position2, position1);
}
[[nodiscard]] static Range withStartAndLength (const ValueType startValue, const ValueType length) noexcept
{
jassert (length >= ValueType());
return Range (startValue, startValue + length);
}
[[nodiscard]] constexpr static Range emptyRange (const ValueType start) noexcept
{
return Range (start, start);
}
//==============================================================================
constexpr inline ValueType getStart() const noexcept { return start; }
constexpr inline ValueType getLength() const noexcept { return end - start; }
constexpr inline ValueType getEnd() const noexcept { return end; }
constexpr inline bool isEmpty() const noexcept { return exactlyEqual (start, end); }
//==============================================================================
void setStart (const ValueType newStart) noexcept
{
start = newStart;
if (end < newStart)
end = newStart;
}
[[nodiscard]] constexpr Range withStart (const ValueType newStart) const noexcept
{
return Range (newStart, jmax (newStart, end));
}
[[nodiscard]] constexpr Range movedToStartAt (const ValueType newStart) const noexcept
{
return Range (newStart, end + (newStart - start));
}
void setEnd (const ValueType newEnd) noexcept
{
end = newEnd;
if (newEnd < start)
start = newEnd;
}
[[nodiscard]] constexpr Range withEnd (const ValueType newEnd) const noexcept
{
return Range (jmin (start, newEnd), newEnd);
}
[[nodiscard]] constexpr Range movedToEndAt (const ValueType newEnd) const noexcept
{
return Range (start + (newEnd - end), newEnd);
}
void setLength (const ValueType newLength) noexcept
{
end = start + jmax (ValueType(), newLength);
}
[[nodiscard]] constexpr Range withLength (const ValueType newLength) const noexcept
{
return Range (start, start + newLength);
}
[[nodiscard]] constexpr Range expanded (ValueType amount) const noexcept
{
return Range (start - amount, end + amount);
}
//==============================================================================
inline Range operator+= (const ValueType amountToAdd) noexcept
{
start += amountToAdd;
end += amountToAdd;
return *this;
}
inline Range operator-= (const ValueType amountToSubtract) noexcept
{
start -= amountToSubtract;
end -= amountToSubtract;
return *this;
}
constexpr Range operator+ (const ValueType amountToAdd) const noexcept
{
return Range (start + amountToAdd, end + amountToAdd);
}
constexpr Range operator- (const ValueType amountToSubtract) const noexcept
{
return Range (start - amountToSubtract, end - amountToSubtract);
}
constexpr bool operator== (Range other) const noexcept
{
const auto tie = [] (const Range& r) { return std::tie (r.start, r.end); };
return tie (*this) == tie (other);
}
constexpr bool operator!= (Range other) const noexcept { return ! operator== (other); }
//==============================================================================
constexpr bool contains (const ValueType position) const noexcept
{
return start <= position && position < end;
}
ValueType clipValue (const ValueType value) const noexcept
{
return jlimit (start, end, value);
}
constexpr bool contains (Range other) const noexcept
{
return start <= other.start && end >= other.end;
}
constexpr bool intersects (Range other) const noexcept
{
return other.start < end && start < other.end;
}
[[nodiscard]] constexpr Range getIntersectionWith (Range other) const noexcept
{
return Range (jmax (start, other.start),
jmin (end, other.end));
}
[[nodiscard]] constexpr Range getUnionWith (Range other) const noexcept
{
return Range (jmin (start, other.start),
jmax (end, other.end));
}
[[nodiscard]] constexpr Range getUnionWith (const ValueType valueToInclude) const noexcept
{
return Range (jmin (valueToInclude, start),
jmax (valueToInclude, end));
}
Range constrainRange (Range rangeToConstrain) const noexcept
{
const ValueType otherLen = rangeToConstrain.getLength();
return getLength() <= otherLen
? *this
: rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart()));
}
template <typename Integral, std::enable_if_t<std::is_integral_v<Integral>, int> = 0>
static Range findMinAndMax (const ValueType* values, Integral numValues) noexcept
{
if (numValues <= 0)
return Range();
const ValueType first (*values++);
Range r (first, first);
while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
{
const ValueType v (*values++);
if (r.end < v) r.end = v;
if (v < r.start) r.start = v;
}
return r;
}
private:
//==============================================================================
ValueType start{}, end{};
};
} // namespace juce
juce::Range
Definition juce_Range.h:40
juce::Range::withStartAndLength
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Definition juce_Range.h:66
juce::Range::setEnd
void setEnd(const ValueType newEnd) noexcept
Definition juce_Range.h:122
juce::Range::expanded
constexpr Range expanded(ValueType amount) const noexcept
Definition juce_Range.h:164
juce::Range::constrainRange
Range constrainRange(Range rangeToConstrain) const noexcept
Definition juce_Range.h:269
juce::Range::getStart
constexpr ValueType getStart() const noexcept
Definition juce_Range.h:80
juce::Range::operator-
constexpr Range operator-(const ValueType amountToSubtract) const noexcept
Definition juce_Range.h:196
juce::Range::contains
constexpr bool contains(Range other) const noexcept
Definition juce_Range.h:226
juce::Range::isEmpty
constexpr bool isEmpty() const noexcept
Definition juce_Range.h:89
juce::Range::getIntersectionWith
constexpr Range getIntersectionWith(Range other) const noexcept
Definition juce_Range.h:239
juce::Range::emptyRange
static constexpr Range emptyRange(const ValueType start) noexcept
Definition juce_Range.h:73
juce::Range::intersects
constexpr bool intersects(Range other) const noexcept
Definition juce_Range.h:232
juce::Range::getEnd
constexpr ValueType getEnd() const noexcept
Definition juce_Range.h:86
juce::Range::operator+
constexpr Range operator+(const ValueType amountToAdd) const noexcept
Definition juce_Range.h:189
juce::Range::withEnd
constexpr Range withEnd(const ValueType newEnd) const noexcept
Definition juce_Range.h:133
juce::Range::Range
constexpr Range(const ValueType startValue, const ValueType endValue) noexcept
Definition juce_Range.h:47
juce::Range::Range
constexpr Range(const Range &)=default
juce::Range::getUnionWith
constexpr Range getUnionWith(Range other) const noexcept
Definition juce_Range.h:246
juce::Range::findMinAndMax
static Range findMinAndMax(const ValueType *values, Integral numValues) noexcept
Definition juce_Range.h:279
juce::Range::clipValue
ValueType clipValue(const ValueType value) const noexcept
Definition juce_Range.h:220
juce::Range::between
static constexpr Range between(const ValueType position1, const ValueType position2) noexcept
Definition juce_Range.h:59
juce::Range::movedToStartAt
constexpr Range movedToStartAt(const ValueType newStart) const noexcept
Definition juce_Range.h:113
juce::Range::withLength
constexpr Range withLength(const ValueType newLength) const noexcept
Definition juce_Range.h:155
juce::Range::getUnionWith
constexpr Range getUnionWith(const ValueType valueToInclude) const noexcept
Definition juce_Range.h:253
juce::Range::operator+=
Range operator+=(const ValueType amountToAdd) noexcept
Definition juce_Range.h:171
juce::Range::setLength
void setLength(const ValueType newLength) noexcept
Definition juce_Range.h:147
juce::Range::setStart
void setStart(const ValueType newStart) noexcept
Definition juce_Range.h:96
juce::Range::getLength
constexpr ValueType getLength() const noexcept
Definition juce_Range.h:83
juce::Range::operator=
Range & operator=(const Range &)=default
juce::Range::contains
constexpr bool contains(const ValueType position) const noexcept
Definition juce_Range.h:214
juce::Range::operator-=
Range operator-=(const ValueType amountToSubtract) noexcept
Definition juce_Range.h:179
juce::Range::Range
constexpr Range()=default
juce::Range::withStart
constexpr Range withStart(const ValueType newStart) const noexcept
Definition juce_Range.h:107
juce::Range::movedToEndAt
constexpr Range movedToEndAt(const ValueType newEnd) const noexcept
Definition juce_Range.h:139