stdex Additional custom or not Standard C++ covered algorithms

Loading...
Searching...
No Matches
interval.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include <vector>
10
11 namespace stdex
12{
16 template <class T>
17 struct interval
18 {
19 T start;
20 T end;
21
25 interval() noexcept : start(static_cast<T>(1)), end(static_cast<T>(0)) {}
26
32 interval(_In_ T x) noexcept : start(x), end(x) {}
33
40 interval(_In_ T _start, _In_ T _end) noexcept : start(_start), end(_end) {}
41
47 T size() const { return start <= end ? end - start : 0; }
48
54 bool empty() const { return start >= end; }
55
59 void invalidate()
60 {
61 start = static_cast<T>(1);
62 end = static_cast<T>(0);
63 }
64
70 operator bool() const { return start <= end; }
71
79 bool contains(_In_ T x) const { return start <= x && x < end; }
80
88 interval operator+(_In_ const interval& other) const
89 {
90 return interval(start + other.start, end + other.end);
91 }
92
100 interval operator+(_In_ const T x) const
101 {
102 return interval(start + x, end + x);
103 }
104
112 interval operator+=(_In_ const T x)
113 {
114 start += x;
115 end += x;
116 return *this;
117 }
118
124 interval operator++()
125 {
126 ++start;
127 ++end;
128 return *this;
129 }
130
136 interval operator++(int) // Postfix increment operator.
137 {
138 interval r = *this;
139 ++start;
140 ++end;
141 return r;
142 }
143
151 interval operator-(_In_ const interval& other) const
152 {
153 return interval(start - other.start, end - other.end);
154 }
155
163 interval operator-(_In_ const T x) const
164 {
165 return interval(start - x, end - x);
166 }
167
175 interval operator-=(_In_ const T x)
176 {
177 start -= x;
178 end -= x;
179 return *this;
180 }
181
187 interval operator--()
188 {
189 --start;
190 --end;
191 return *this;
192 }
193
199 interval operator--(int) // Postfix decrement operator.
200 {
201 interval r = *this;
202 --start;
203 --end;
204 return r;
205 }
206
214 bool operator==(_In_ const interval& other) const
215 {
216 return start == other.start && end == other.end;
217 }
218
226 bool operator!=(_In_ const interval& other) const
227 {
228 return !operator ==(other);
229 }
230 };
231
232 template <class T, class AX = std::allocator<interval<T>>>
233 using interval_vector = std::vector<interval<T>, AX>;
234}
stdex::interval
Numerical interval.
Definition interval.hpp:18
stdex::interval::operator-=
interval operator-=(const T x)
Moves interval towards the beginning by a number.
Definition interval.hpp:175
stdex::interval::operator+
interval operator+(const interval &other) const
Adds two intervals by components.
Definition interval.hpp:88
stdex::interval::interval
interval(T x) noexcept
Constructs a zero-size interval.
Definition interval.hpp:32
stdex::interval::contains
bool contains(T x) const
Is value in interval?
Definition interval.hpp:79
stdex::interval::empty
bool empty() const
Is interval empty?
Definition interval.hpp:54
stdex::interval::operator+=
interval operator+=(const T x)
Moves interval towards the end by a number.
Definition interval.hpp:112
stdex::interval::interval
interval(T _start, T _end) noexcept
Constructs an interval.
Definition interval.hpp:40
stdex::interval::operator++
interval operator++(int)
Moves interval towards the end by one.
Definition interval.hpp:136
stdex::interval::operator++
interval operator++()
Moves interval towards the end by one.
Definition interval.hpp:124
stdex::interval::operator+
interval operator+(const T x) const
Moves interval towards the end by a number.
Definition interval.hpp:100
stdex::interval::size
T size() const
Returns interval size.
Definition interval.hpp:47
stdex::interval::end
T end
interval end
Definition interval.hpp:20
stdex::interval::interval
interval() noexcept
Constructs an invalid interval.
Definition interval.hpp:25
stdex::interval::operator-
interval operator-(const T x) const
Moves interval towards the beginning by a number.
Definition interval.hpp:163
stdex::interval::operator-
interval operator-(const interval &other) const
Subtracts two intervals by components.
Definition interval.hpp:151
stdex::interval::operator!=
bool operator!=(const interval &other) const
Are intervals different?
Definition interval.hpp:226
stdex::interval::invalidate
void invalidate()
Invalidates interval.
Definition interval.hpp:59
stdex::interval::operator--
interval operator--(int)
Moves interval towards the begginning by one.
Definition interval.hpp:199
stdex::interval::start
T start
interval start
Definition interval.hpp:19
stdex::interval::operator==
bool operator==(const interval &other) const
Are intervals identical?
Definition interval.hpp:214
stdex::interval::operator--
interval operator--()
Moves interval towards the beginning by one.
Definition interval.hpp:187