4#include <clingo/util/small_vector.hh>
6namespace CppClingo::Util {
71 return x.bound < y.bound || (x.bound == y.bound && x.inclusive && !y.inclusive);
75 return x.bound < y.bound || (x.bound == y.bound && (x.inclusive || !y.inclusive));
86 return x.bound < y.bound || (x.bound == y.bound && x.inclusive && y.inclusive);
91 return x.bound < y.bound || (y.bound == x.bound && !x.inclusive && y.inclusive);
95 return x.bound < y.bound || (y.bound == x.bound && (!x.inclusive || y.inclusive));
106 return x.bound < y.bound || (y.bound == x.bound && !x.inclusive && !y.inclusive);
113 return x < y.left.bound || (y.left.bound == x && !y.left.inclusive);
117 return x.right.bound < y || (y == x.right.bound && !x.right.inclusive);
137 for (
auto &x : list) {
158 auto it = std::lower_bound(vec_.
begin(), vec_.
end(), x);
159 if (it == vec_.
end()) {
162 auto jt = std::upper_bound(it, vec_.
end(), x);
166 it->left = std::min(x.left, it->left);
167 it->right = std::max(x.right, (jt - 1)->right);
168 vec_.
erase(it + 1, jt);
181 auto it = std::lower_bound(vec_.
begin(), vec_.
end(), x);
182 if (it != vec_.
end()) {
183 auto jt = std::upper_bound(it, vec_.
end(), x);
193 }
else if (!r.empty()) {
196 }
else if (it != jt) {
199 vec_.
erase(it + !it->empty(), jt - !(jt - 1)->
empty());
214 for (
auto &y : vec_) {
215 if (x.right <= y.right) {
216 return y.left <= x.left;
226 [[nodiscard]]
auto contains(T
const &x)
const ->
bool {
227 for (
auto &y : vec_) {
241 for (
auto &y : vec_) {
242 if (x.left < y.right) {
243 return y.left < x.right;
251 [[nodiscard]]
auto empty() const ->
bool {
return vec_.
empty(); }
254 [[nodiscard]]
auto size() const ->
size_t {
return vec_.
size(); }
273 auto it = vec_.
begin();
275 for (
auto &x : set.vec_) {
276 for (; it != vec_.
end() && it->right < x.left; ++it) {
278 for (; it != vec_.
end() && it->right <= x.right; ++it) {
281 if (it != vec_.
end() && it->left < x.right) {
290 auto it = set.vec_.begin();
292 for (
auto &x : vec_) {
294 for (; it != set.vec_.end() && it->right < current.
left; ++it) {
296 for (; it != set.vec_.end() && it->right <= current.
right; ++it) {
297 if (current.
left < it->left) {
303 if (it != set.vec_.end() && it->left < current.
right) {
A set of intervals.
Definition interval_set.hh:18
friend auto operator<=(right_bound const &x, right_bound const &y) -> bool
Right bound x is smaller than or equal to right bound y.
Definition interval_set.hh:94
friend auto operator<(left_bound const &x, left_bound const &y) -> bool
Left bound x is smaller than left bound y.
Definition interval_set.hh:70
auto size() const -> size_t
Get the size of the set.
Definition interval_set.hh:254
auto operator=(interval_set const &other) -> interval_set &=default
The default copy assignment.
friend auto operator<(left_bound const &x, right_bound const &y) -> bool
There is a gap between x and y.
Definition interval_set.hh:85
auto front() const -> interval const &
Get the first interval in the set.
Definition interval_set.hh:257
auto difference(interval_set const &set) const -> interval_set
Compute the difference between two interval sets.
Definition interval_set.hh:289
auto release() -> interval_vector
Releases the underlying vector.
Definition interval_set.hh:150
friend auto operator<(right_bound const &x, right_bound const &y) -> bool
Right bound x is smaller than right bound y.
Definition interval_set.hh:90
auto remove(interval const &x) -> interval_set &
Subtract the given interval from the set.
Definition interval_set.hh:179
auto contains(T const &x) const -> bool
Check if the set contains the given value.
Definition interval_set.hh:226
friend auto operator<(interval const &x, value_type const &y) -> bool
Interval x is before value y.
Definition interval_set.hh:116
friend auto operator<(right_bound const &x, left_bound const &y) -> bool
There is a gap between x and y.
Definition interval_set.hh:105
interval_set(interval_set const &other)=default
The default copy constructor.
auto back() const -> interval const &
Get the last interval in the set.
Definition interval_set.hh:260
auto end() const -> iterator
Get an iterator to the end of the set.
Definition interval_set.hh:266
auto intersect(interval_set const &set) const -> interval_set
Compute the intersection of two interval sets.
Definition interval_set.hh:272
auto contains(interval const &x) const -> bool
Check if the set contains the given interval.
Definition interval_set.hh:210
interval_set(interval_set &&other) noexcept=default
The default move constructor.
friend auto operator<(interval const &x, interval const &y) -> bool
Interval x is before interval y.
Definition interval_set.hh:110
friend auto operator<(value_type const &x, interval const &y) -> bool
Value x is before interval y.
Definition interval_set.hh:112
typename interval_vector::const_iterator iterator
An iterator over the intervals in the set.
Definition interval_set.hh:67
interval_set()=default
Construct an empty interval set.
auto intersects(interval const &x) const -> bool
Check if the set intersects the given interval.
Definition interval_set.hh:239
void clear()
Clear the set.
Definition interval_set.hh:269
~interval_set()=default
Destroy the interval set.
auto empty() const -> bool
Check if the set is empty.
Definition interval_set.hh:251
auto add(interval const &x) -> interval_set &
Add the given interval to the set.
Definition interval_set.hh:156
auto operator=(interval_set &&other) noexcept -> interval_set &=default
The default copy assignment.
auto begin() const -> iterator
Get an iterator to the beginning of the set.
Definition interval_set.hh:263
void reserve(size_t n)
Reserve space for at least n elements.
Definition interval_set.hh:147
small_vector< interval > interval_vector
The vector used to store intervals.
Definition interval_set.hh:65
interval_set(std::initializer_list< interval > list)
Construct an interval set containing the given intervals.
Definition interval_set.hh:136
T value_type
The value type used as bounds.
Definition interval_set.hh:21
friend auto operator<=(left_bound const &x, left_bound const &y) -> bool
Left bound x is smaller than or equal to left bound y.
Definition interval_set.hh:74
void reserve(size_t n)
Reserve space for at least n elements.
Definition small_vector.hh:185
auto front() -> reference
Get the first element in the vector.
Definition small_vector.hh:202
void emplace(const_iterator loc, U &&...args)
Emplace an element before the given iterator.
Definition small_vector.hh:253
interval const * const_iterator
The const iterator type.
Definition small_vector.hh:33
auto begin() -> iterator
Get an iterator to the beginning of the vector.
Definition small_vector.hh:135
void emplace_back(U &&...args)
Emplace an element after the last element.
Definition small_vector.hh:271
auto end() -> iterator
Get an iterator to the end of the vector.
Definition small_vector.hh:147
auto back() -> reference
Get the last element in the vector.
Definition small_vector.hh:214
auto erase(iterator it) -> iterator
Erase the given element.
Definition small_vector.hh:226
auto empty() const -> bool
Check if the vector is empty.
Definition small_vector.hh:124
auto size() const -> size_t
Get the size of the vector.
Definition small_vector.hh:116
void clear() noexcept
Clear the vector.
Definition small_vector.hh:298
An interval determined by a left and a right bound.
Definition interval_set.hh:51
constexpr interval(left_bound left, right_bound right)
Construct the bound.
Definition interval_set.hh:53
auto empty() const -> bool
Whether the interval is empty.
Definition interval_set.hh:57
right_bound right
The right bound of the interval.
Definition interval_set.hh:62
left_bound left
The left bound of the interval.
Definition interval_set.hh:60
auto contains(value_type const &x) const -> bool
Whether the interval contains the given value.
Definition interval_set.hh:55
A left bound of an interval.
Definition interval_set.hh:25
value_type bound
The value of the bound.
Definition interval_set.hh:32
left_bound(right_bound const &x)
Construct the bound from a right bound.
Definition interval_set.hh:29
bool inclusive
Whether the bound is open (!inclusive) or closed (inclusive).
Definition interval_set.hh:34
constexpr left_bound(value_type bound, bool inclusive)
Construct the bound.
Definition interval_set.hh:27
A right bound of an interval.
Definition interval_set.hh:38
constexpr right_bound(value_type bound, bool inclusive)
Construct the bound.
Definition interval_set.hh:40
bool inclusive
Whether the bound is open (!inclusive) or closed (inclusive).
Definition interval_set.hh:47
value_type bound
The value of the bound.
Definition interval_set.hh:45
right_bound(left_bound const &x)
Construct the bound from a left bound.
Definition interval_set.hh:42