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
11namespace stdex
12{
16 template <class T>
17 struct interval
18 {
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
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<T> operator+(_In_ const interval<T>& other) const
89 {
90 return interval<T>(start + other.start, end + other.end);
91 }
92
100 interval<T> operator+(_In_ const T x) const
101 {
102 return interval<T>(start + x, end + x);
103 }
104
111 {
112 ++start;
113 ++end;
114 return *this;
115 }
116
122 interval<T> operator++(int) // Postfix increment operator.
123 {
124 interval<T> r = *this;
125 ++start;
126 ++end;
127 return r;
128 }
129
137 interval<T> operator-(_In_ const interval<T>& other) const
138 {
139 return interval<T>(start - other.start, end - other.end);
140 }
141
149 interval<T> operator-(_In_ const T x) const
150 {
151 return interval<T>(start - x, end - x);
152 }
153
160 {
161 --start;
162 --end;
163 return *this;
164 }
165
171 interval<T> operator--(int) // Postfix decrement operator.
172 {
173 interval<T> r = *this;
174 --start;
175 --end;
176 return r;
177 }
178
186 bool operator==(_In_ const interval<T>& other) const
187 {
188 return start == other.start && end == other.end;
189 }
190
198 bool operator!=(_In_ const interval<T>& other) const
199 {
200 return !operator ==(other);
201 }
202 };
203
204 template <class T, class AX = std::allocator<interval<T>>>
205 using interval_vector = std::vector<interval<T>, AX>;
206}
Numerical interval.
Definition interval.hpp:18
interval(T x) noexcept
Constructs a zero-size interval.
Definition interval.hpp:32
interval< T > operator-(const T x) const
Moves interval towards the beginning by a number.
Definition interval.hpp:149
bool contains(T x) const
Is value in interval?
Definition interval.hpp:79
interval< T > operator++(int)
Moves interval towards the end by one.
Definition interval.hpp:122
bool empty() const
Is interval empty?
Definition interval.hpp:54
interval(T _start, T _end) noexcept
Constructs an interval.
Definition interval.hpp:40
interval< T > operator+(const T x) const
Moves interval towards the end by a number.
Definition interval.hpp:100
interval< T > operator+(const interval< T > &other) const
Adds two intervals by components.
Definition interval.hpp:88
T size() const
Returns interval size.
Definition interval.hpp:47
T end
interval end
Definition interval.hpp:20
bool operator==(const interval< T > &other) const
Are intervals identical?
Definition interval.hpp:186
interval< T > operator++()
Moves interval towards the end by one.
Definition interval.hpp:110
interval() noexcept
Constructs an invalid interval.
Definition interval.hpp:25
void invalidate()
Invalidates interval.
Definition interval.hpp:59
interval< T > operator--()
Moves interval towards the begginning by one.
Definition interval.hpp:159
bool operator!=(const interval< T > &other) const
Are intervals different?
Definition interval.hpp:198
T start
interval start
Definition interval.hpp:19
interval< T > operator-(const interval< T > &other) const
Subtracts two intervals by components.
Definition interval.hpp:137
interval< T > operator--(int)
Moves interval towards the begginning by one.
Definition interval.hpp:171