87# ifndef TPL_DISJOINT_SPARSE_TABLE_H
88# define TPL_DISJOINT_SPARSE_TABLE_H
94# include <initializer_list>
95# include <type_traits>
115 template <
typename F,
typename T>
150 template <
typename T,
class Op>
162 T &
at(
const size_t k,
const size_t i) {
return table(
k *
n + i); }
163 const T &
at(
const size_t k,
const size_t i)
const
175 if (nn <= 1)
return 0;
176 return static_cast<size_t>(std::bit_width(nn - 1));
192 const size_t half =
size_t{1} <<
k;
193 const size_t block_sz = half << 1;
195 for (
size_t b = 0; b <
n; b += block_sz)
197 const size_t mid = b + half;
201 at(
k, mid - 1) =
data(mid - 1);
202 for (
size_t i = mid - 1; i > b; --i)
209 const size_t right_end = std::min(b + block_sz,
n);
210 for (
size_t i = mid + 1; i < right_end; ++i)
218 template <
class Getter>
221 for (
size_t i = 0; i <
n; ++i)
226 template <
class AlephIt>
230 for (; it.has_curr(); it.next_ne())
231 data(i++) = it.get_curr();
252 fill_data([&init_val](
size_t) {
return init_val; });
270 auto it = il.begin();
271 fill_data([&it](
size_t) {
return *it++; });
337 noexcept(std::is_nothrow_move_constructible_v<Op>)
352 noexcept(
noexcept(
swap(other)))
373 <<
"Gen_Disjoint_Sparse_Table::query: r=" <<
r <<
" >= n=" <<
n;
375 <<
"Gen_Disjoint_Sparse_Table::query: l=" <<
l <<
" > r=" <<
r;
380 const size_t k =
static_cast<size_t>(std::bit_width(
l ^
r)) - 1;
394 <<
"Gen_Disjoint_Sparse_Table::get: index " << i
401 [[nodiscard]]
constexpr size_t size() const noexcept {
return n; }
404 [[nodiscard]]
constexpr bool is_empty() const noexcept {
return n == 0; }
419 for (
size_t i = 0; i <
n; ++i)
438 data.swap(other.data);
439 table.swap(other.table);
440 std::swap(
n, other.n);
441 std::swap(
levels, other.levels);
463 template <
typename T>
488 template <
typename T>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Standard functor implementations and comparison objects.
Simple dynamic array with automatic resizing and functional operations.
static Array create(const size_t n)
Create an array with n logical elements.
Doubly-linked list (defined in tpl_dynList.H).
Disjoint Sparse Table over an arbitrary associative binary operation.
Gen_Disjoint_Sparse_Table(const size_t num, const T &init_val, Op oper=Op())
Construct a disjoint sparse table with num elements, all equal to init_val.
Gen_Disjoint_Sparse_Table & operator=(const Gen_Disjoint_Sparse_Table &)=default
constexpr bool is_empty() const noexcept
True if the table contains no elements.
void fill_data_from_aleph_it(AlephIt it)
Fill data array from an Aleph-style iterator.
Gen_Disjoint_Sparse_Table(const DynList< T > &values, Op oper=Op())
Construct from a DynList<T> in O(n log n) time.
Gen_Disjoint_Sparse_Table & operator=(Gen_Disjoint_Sparse_Table &&other) noexcept(noexcept(swap(other)))
Exchange this table with another, which is left with the previous elements of this one.
Gen_Disjoint_Sparse_Table(Gen_Disjoint_Sparse_Table &&other) noexcept(std::is_nothrow_move_constructible_v< Op >)
Take the elements of another table, which is left empty.
T & at(const size_t k, const size_t i)
Access table[k][i] (0-based row k, 0-based column i).
Array< T > values() const
Reconstruct all original values into an Array.
void swap(Gen_Disjoint_Sparse_Table &other) noexcept(std::is_nothrow_swappable_v< Op >)
Swap this table with other in O(1).
const T & at(const size_t k, const size_t i) const
T get(const size_t i) const
Retrieve the value a[i] in O(1).
constexpr size_t num_levels() const noexcept
Number of precomputed levels.
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).
Gen_Disjoint_Sparse_Table(const Array< T > &values, Op oper=Op())
Construct from an Array<T> in O(n log n) time.
void fill_data(Getter getter)
Fill data array from a 0-based indexed getter.
Gen_Disjoint_Sparse_Table(std::initializer_list< T > il, Op oper=Op())
Construct from an initializer list in O(n log n) time.
Gen_Disjoint_Sparse_Table(const std::vector< T > &values, Op oper=Op())
Construct from a std::vector<T> in O(n log n) time.
void build()
Build the disjoint sparse table from the data array.
constexpr size_t size() const noexcept
Number of logical elements.
Gen_Disjoint_Sparse_Table(const Gen_Disjoint_Sparse_Table &)=default
T Item_Type
The type of the element stored in the table.
static constexpr size_t compute_levels(const size_t nn) noexcept
Compute the number of levels for n elements.
A binary functor closed over T.
Binary operation compatible with Disjoint Sparse Table queries.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Disjoint Sparse Table for range product queries.
Disjoint Sparse Table for range sum queries.
Dynamic array container with automatic resizing.
Alias for htlist.H (DynList implementation).