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>
117 requires(
const F& f,
const T& a,
const T& b)
119 { f(a, b) } -> std::convertible_to<T>;
154 template <
typename T,
class Op>
166 T &
at(
const size_t k,
const size_t i) {
return table(
k *
n + i); }
167 const T &
at(
const size_t k,
const size_t i)
const
179 if (
nn <= 1)
return 0;
180 return static_cast<size_t>(std::bit_width(
nn - 1));
196 const size_t half =
size_t{1} <<
k;
206 for (
size_t i =
mid - 1; i > b; --i)
222 template <
class Getter>
225 for (
size_t i = 0; i <
n; ++i)
230 template <
class AlephIt>
234 for (; it.has_curr(); it.next_ne())
235 data(i++) = it.get_curr();
275 fill_data([&it](
size_t) {
return *it++; });
354 <<
"Gen_Disjoint_Sparse_Table::query: r=" << r <<
" >= n=" <<
n;
356 <<
"Gen_Disjoint_Sparse_Table::query: l=" <<
l <<
" > r=" << r;
361 const size_t k =
static_cast<size_t>(std::bit_width(
l ^ r)) - 1;
375 <<
"Gen_Disjoint_Sparse_Table::get: index " << i
400 for (
size_t i = 0; i <
n; ++i)
434 template <
typename T>
459 template <
typename T>
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(size_t n)
Create an array with n logical elements.
Dynamic singly linked list with functional programming support.
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.
void swap(Gen_Disjoint_Sparse_Table &other) noexcept
Swap this table with other in O(1).
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.
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.
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
Gen_Disjoint_Sparse_Table(Gen_Disjoint_Sparse_Table &&) noexcept=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.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
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).