7namespace CppClingo::Util {
40 if (idx_ == std::get<1>(*it_)) {
60 using interval_iterator = std::vector<std::tuple<size_t, size_t, T>>::const_iterator;
62 iterator(interval_iterator it,
size_t idx) : it_{it}, idx_{idx} {}
64 interval_iterator it_;
70 if (values_.empty()) {
71 values_.emplace_back(0, 1, value);
73 auto &[l, r, y] = values_.back();
74 if (
static_cast<T
>(r) + y == value) {
77 values_.emplace_back(
size(),
size() + 1, value -
static_cast<T
>(
size()));
86 values_.emplace_back(0,
static_cast<size_t>(r - l), l);
92 assert(index <
size() && last_ < values_.size());
93 auto const &[l, r, y] = values_[last_];
95 auto ib = index < l ? values_.begin() : values_.begin() + last_;
96 auto ie = index < r ? values_.begin() + last_ + 1 : values_.end();
97 auto it = std::upper_bound(ib, ie, index, [](
auto const &a,
auto const &b) {
return a < std::get<1>(b); });
98 last_ =
static_cast<size_t>(std::distance(values_.begin(), it));
99 return static_cast<T
>(index) + std::get<2>(*it);
102 [[nodiscard]]
auto find(T value)
const ->
size_t {
106 for (
auto const &[l, r, y] : values_) {
107 auto i =
static_cast<size_t>(value - y);
108 if (y <= value && l <= i && i < r) {
115 [[nodiscard]]
auto size() const ->
size_t {
return empty() ? 0 : std::get<1>(values_.back()); }
117 [[nodiscard]]
auto empty() const ->
bool {
return values_.empty(); }
119 [[nodiscard]]
auto begin() const ->
iterator {
return {values_.cbegin(), 0}; }
124 std::vector<std::tuple<size_t, size_t, T>> values_;
125 size_t mutable last_ = 0;
Const iterator for index sequence.
Definition index_sequence.hh:18
auto operator!=(const iterator &other) const -> bool
Compare two iterators.
Definition index_sequence.hh:57
void pointer
The pointer type.
Definition index_sequence.hh:29
auto operator*() const -> reference
Get the current value.
Definition index_sequence.hh:35
std::forward_iterator_tag iterator_category
The iterator category.
Definition index_sequence.hh:21
iterator()=default
Construct an end iterator.
T value_type
The value type.
Definition index_sequence.hh:23
auto operator++() -> iterator &
Increment the iterator.
Definition index_sequence.hh:38
auto operator++(int) -> iterator
Post increment the iterator.
Definition index_sequence.hh:47
T reference
The reference type.
Definition index_sequence.hh:27
auto operator==(const iterator &other) const -> bool
Compare two iterators.
Definition index_sequence.hh:54
std::ptrdiff_t difference_type
The difference type.
Definition index_sequence.hh:25
Container to store integer sequences.
Definition index_sequence.hh:15
auto operator[](size_t index) const -> T
Get the i-th integer in the sequence.
Definition index_sequence.hh:91
auto end() const -> iterator
Get an iterator to the end of the sequence.
Definition index_sequence.hh:121
auto empty() const -> bool
Check whether the sequence is empty.
Definition index_sequence.hh:117
void assign(T l, T r)
Set the sequence to the interval [l,r-1].
Definition index_sequence.hh:83
void add(T value)
Add an integer to the sequence.
Definition index_sequence.hh:69
auto begin() const -> iterator
Get an iterator to the beginning of the sequence.
Definition index_sequence.hh:119
auto size() const -> size_t
Get the number of indices in the sequence.
Definition index_sequence.hh:115
auto find(T value) const -> size_t
Check if the sequence contains an element.
Definition index_sequence.hh:102