1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 | #pragma once
#include <cassert><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iterator><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility>
#include <type_traits><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <concepts><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <bit><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "internal/dev_env.hpp"
#include "internal/types.hpp"
#include "internal/concepts.hpp"
#include "internal/range_reference.hpp"
#include "internal/unconstructible.hpp"
#include "numeric/bit.hpp"
#include "algebraic/internal/concepts.hpp"
#include "action/base.hpp"
namespace uni {
namespace internal {
namespace disjoint_sparse_table_impl {
// Thanks to: https://noshi91.hatenablog.com/entry/2018/05/08/183946
template<algebraic::internal::semigroup Operand>
struct core {
using size_type = internal::size_t;
using operand = Operand;
using iterator = std::vector<std::vector<Operand>>;
size_type _n = 0, _depth = 0;
bool _built = false;
protected:
std::vector<std::vector<operand>> _table = {};
public:
explicit core(const size_type n = 0) noexcept(NO_EXCEPT) : _n(n) {
this->_depth = std::bit_width<std::make_unsigned_t<size_type>>(n);
this->_table.resize(this->_depth+1, std::vector<operand>(n));
}
template<std::input_iterator I, std::sized_sentinel_for<I> S>
core(I first, S last) noexcept(NO_EXCEPT)
: core(static_cast<size_type>(std::ranges::distance(first, last)))
{
std::ranges::copy(first, last, this->_table.begin()->begin());
}
template<bool FORCE = false>
inline auto& build() noexcept(NO_EXCEPT) {
if(!FORCE and this->_built) return *this;
FOR(i, 2, this->_depth) {
const size_type len = 1 << i;
for(size_type l = 0, m = (len >> 1); m < this->_n; l += len, m = l + (len >> 1)) {
this->_table[i - 1][m - 1] = this->_table.front()[m - 1];
REPD(j, l, m-1) {
this->_table[i - 1][j] = this->_table.front()[j] + this->_table[i - 1][j + 1];
}
this->_table[i - 1][m] = this->_table.front()[m];
REP(j, m + 1, std::min(l + len, this->_n)) {
this->_table[i - 1][j] = this->_table[i - 1][j - 1] + this->_table.front()[j];
}
}
}
this->_built = true;
return *this;
}
inline auto& raw() noexcept(NO_EXCEPT) {
this->_built = false;
return this->_table.front();
}
inline const auto& raw() const noexcept(NO_EXCEPT) { return this->_table.front(); }
inline auto& data() noexcept(NO_EXCEPT) { return this->_table; }
inline const auto& data() const noexcept(NO_EXCEPT) { return this->_table; }
size_type size() const noexcept(NO_EXCEPT) { return this->_n; }
operand fold(const size_type l, size_type r) {
if(l == r) return operand{};
if(l == --r) return this->_table.front()[l];
this->build();
const size_type p = highest_bit_pos<std::make_unsigned_t<size_type>>(l ^ r);
return this->_table[p][l] + this->_table[p][r];
}
};
} // namespace disjoint_sparse_table_impl
} // namespace internal
template<class> struct disjoint_sparse_table : internal::unconstructible {};
template<algebraic::internal::semigroup Semigroup>
struct disjoint_sparse_table<Semigroup> {
private:
using core = internal::disjoint_sparse_table_impl::core<Semigroup>;
using iterator = core::iterator;
core _impl;
public:
using value_type = Semigroup;
using size_type = core::size_type;
protected:
inline auto _positivize_index(const size_type p) const noexcept(NO_EXCEPT) {
return p < 0 ? this->_impl.size() + p : p;
}
public:
explicit disjoint_sparse_table(const size_type n, const value_type& val = value_type()) noexcept(NO_EXCEPT) : _impl(n) {
this->_impl.data().begin()->assign(n, val);
}
template<std::input_iterator I, std::sized_sentinel_for<I> S>
disjoint_sparse_table(I first, S last) noexcept(NO_EXCEPT) : _impl(first, last) {}
template<std::ranges::input_range R>
explicit disjoint_sparse_table(R&& range) noexcept(NO_EXCEPT)
: _impl(std::ranges::begin(range), std::ranges::end(range))
{}
inline auto& raw() noexcept(NO_EXCEPT) { return this->_impl.raw(); }
inline const auto& raw() const noexcept(NO_EXCEPT) { return this->_impl.raw(); }
inline const auto& data() const noexcept(NO_EXCEPT) { return this->impl.data(); }
inline auto size() const noexcept(NO_EXCEPT) { return this->_impl.size(); }
friend internal::range_reference<disjoint_sparse_table>;
struct range_reference : internal::range_reference<disjoint_sparse_table> {
range_reference(disjoint_sparse_table *const super, const size_type l, const size_type r) noexcept(NO_EXCEPT)
: internal::range_reference<disjoint_sparse_table>(super, super->_positivize_index(l), super->_positivize_index(r))
{
assert(0 <= this->_begin && this->_begin <= this->_end && this->_end <= this->_super->size());
}
inline auto fold() noexcept(NO_EXCEPT) {
return this->_super->fold(this->_begin, this->_end);
}
};
inline auto fold(size_type l, size_type r) noexcept(NO_EXCEPT) {
l = this->_positivize_index(l), r = this->_positivize_index(r);
assert(0 <= l && l <= r && r <= this->size());
return this->_impl.fold(l, r);
}
inline auto fold() noexcept(NO_EXCEPT) { return this->fold(0, this->size()); }
inline auto operator[](const size_type index) const noexcept(NO_EXCEPT) { return this->_impl.data().front()[index]; }
inline auto operator()(const size_type l, const size_type r) noexcept(NO_EXCEPT) { return range_reference(this, l, r); }
inline auto begin() const noexcept(NO_EXCEPT) { return this->_impl.data().begin()->begin(); }
inline auto end() const noexcept(NO_EXCEPT) { return this->_impl.data().begin()->end(); }
inline auto rbegin() const noexcept(NO_EXCEPT) { return this->_impl.data().begin()->rbegin(); }
inline auto rend() const noexcept(NO_EXCEPT) { return this->_impl.data().begin()->rend(); }
};
template<actions::internal::operatable_action Action>
struct disjoint_sparse_table<Action> : disjoint_sparse_table<typename Action::operand> {
using disjoint_sparse_table<typename Action::operand>::disjoint_sparse_table;
};
} // namespace uni
|