Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
range.hpp
Go to the documentation of this file.
1#pragma once
2
3// Templated Range<T> — represents a [start, end) interval with set operations.
4
5#include <algorithm>
6#include <cmath>
7#include <optional>
8#include <type_traits>
9
10namespace pulp::runtime {
11
12template<typename T>
13struct Range {
14 T start{};
15 T end{};
16
17 constexpr Range() = default;
18 constexpr Range(T s, T e) : start(s), end(e) {}
19
21 constexpr T length() const { return end - start; }
22
24 constexpr bool empty() const { return start >= end; }
25
27 constexpr bool contains(T value) const { return value >= start && value < end; }
28
30 constexpr bool contains(const Range& other) const {
31 return other.start >= start && other.end <= end;
32 }
33
35 constexpr bool intersects(const Range& other) const {
36 return start < other.end && other.start < end;
37 }
38
40 constexpr Range intersection(const Range& other) const {
41 T s = std::max(start, other.start);
42 T e = std::min(end, other.end);
43 return (s < e) ? Range(s, e) : Range{};
44 }
45
47 constexpr Range enclosing_union(const Range& other) const {
48 if (empty()) return other;
49 if (other.empty()) return *this;
50 return Range(std::min(start, other.start), std::max(end, other.end));
51 }
52
54 constexpr T constrain(T value) const {
55 if (end <= start) return start;
56 if constexpr (std::is_integral_v<T>) {
57 return std::clamp(value, start, end - T(1));
58 } else {
59 return std::clamp(value, start, std::nextafter(end, start));
60 }
61 }
62
64 constexpr Range expanded(T value) const {
65 if (empty()) return Range(value, value + T(1));
66 return Range(std::min(start, value), std::max(end, value + T(1)));
67 }
68
69 constexpr bool operator==(const Range& other) const {
70 return start == other.start && end == other.end;
71 }
72 constexpr bool operator!=(const Range& other) const { return !(*this == other); }
73
75 static constexpr Range from_start_length(T s, T len) { return Range(s, s + len); }
76};
77
78// Common type aliases
83
84} // namespace pulp::runtime
Definition generated_event_source.hpp:33
Range< double > DoubleRange
Definition range.hpp:81
Range< float > FloatRange
Definition range.hpp:80
Range< size_t > SizeRange
Definition range.hpp:82
Range< int > IntRange
Definition range.hpp:79
@ other
Definition udev_monitor.hpp:26
Definition range.hpp:13
constexpr bool empty() const
Whether the range is empty (start >= end).
Definition range.hpp:24
constexpr Range(T s, T e)
Definition range.hpp:18
int end
Definition range.hpp:15
constexpr T length() const
Length of the range.
Definition range.hpp:21
int start
Definition range.hpp:14
constexpr Range enclosing_union(const Range &other) const
Union of two ranges (bounding box — may include a gap).
Definition range.hpp:47
constexpr Range expanded(T value) const
Expand range to include a value.
Definition range.hpp:64
constexpr bool contains(const Range &other) const
Whether this range fully contains another range.
Definition range.hpp:30
constexpr Range intersection(const Range &other) const
Intersection of two ranges (empty if no overlap).
Definition range.hpp:40
constexpr T constrain(T value) const
Constrain a value to this range [start, end).
Definition range.hpp:54
constexpr bool contains(T value) const
Whether this range contains a value.
Definition range.hpp:27
constexpr Range()=default
static constexpr Range from_start_length(T s, T len)
Create a range from start + length.
Definition range.hpp:75
constexpr bool intersects(const Range &other) const
Whether this range intersects another range.
Definition range.hpp:35
constexpr bool operator==(const Range &other) const
Definition range.hpp:69
constexpr bool operator!=(const Range &other) const
Definition range.hpp:72