stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
interval.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 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 inline interval() noexcept : start(static_cast<T>(1)), end(static_cast<T>(0)) {}
26
32 inline interval(_In_ T x) noexcept : start(x), end(x) {}
33
40 inline interval(_In_ T _start, _In_ T _end) noexcept : start(_start), end(_end) {}
41
47 inline T size() const { return start <= end ? end - start : 0; }
48
54 inline bool empty() const { return start >= end; }
55
61 inline operator bool() const { return start <= end; }
62
70 inline bool contains(_In_ T x) const { return start <= x && x < end; }
71
79 inline interval<T> operator+(_In_ const interval<T>& other) const
80 {
81 return interval<T>(start + other.start, end + other.end);i.
82 }
83
91 inline interval<T> operator+(_In_ const T x) const
92 {
93 return interval<T>(start + x, end + x);
94 }
95
102 {
103 ++start;
104 ++end;
105 return *this;
106 }
107
113 inline interval<T> operator++(int) // Postfix increment operator.
114 {
115 interval<T> r = *this;
116 ++start;
117 ++end;
118 return r;
119 }
120
128 inline interval<T> operator-(_In_ const interval<T>& other) const
129 {
130 return interval<T>(start - other.start, end - other.end);
131 }
132
140 inline interval<T> operator-(_In_ const T x) const
141 {
142 return interval<T>(start - x, end - x);
143 }
144
151 {
152 --start;
153 --end;
154 return *this;
155 }
156
162 inline interval<T> operator--(int) // Postfix decrement operator.
163 {
164 interval<T> r = *this;
165 --start;
166 --end;
167 return r;
168 }
169
177 inline bool operator==(_In_ const interval<T>& other) const
178 {
179 return start == other.start && end == other.end;
180 }
181
189 inline bool operator!=(_In_ const interval<T>& other) const
190 {
191 return !operator ==(other);
192 }
193 };
194
195 template <class T, class _Alloc = std::allocator<interval<T>>>
196 using interval_vector = std::vector<interval<T>, _Alloc>;
197}
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:140
bool contains(T x) const
Is value in interval?
Definition interval.hpp:70
interval< T > operator++(int)
Moves interval towards the end by one.
Definition interval.hpp:113
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:91
interval< T > operator+(const interval< T > &other) const
Adds two intervals by components.
Definition interval.hpp:79
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:177
interval< T > operator++()
Moves interval towards the end by one.
Definition interval.hpp:101
interval() noexcept
Constructs an invalid interval.
Definition interval.hpp:25
interval< T > operator--()
Moves interval towards the begginning by one.
Definition interval.hpp:150
bool operator!=(const interval< T > &other) const
Are intervals different?
Definition interval.hpp:189
T start
interval start
Definition interval.hpp:19
interval< T > operator-(const interval< T > &other) const
Subtracts two intervals by components.
Definition interval.hpp:128
interval< T > operator--(int)
Moves interval towards the begginning by one.
Definition interval.hpp:162