69# ifndef TPL_SPARSE_TABLE_H
70# define TPL_SPARSE_TABLE_H
76# include <initializer_list>
77# include <type_traits>
97 template <
typename F,
typename T>
99 requires(
const F& f,
const T& a,
const T& b)
101 { f(a, b) } -> std::convertible_to<T>;
111 template <std::totally_ordered T>
116 return a <= b ? a : b;
127 template <std::totally_ordered T>
132 return a >= b ? a : b;
166 template <
typename T,
class Op>
178 T &
at(
const size_t k,
const size_t i) {
return table(
k *
n + i); }
179 const T &
at(
const size_t k,
const size_t i)
const {
return table(
k *
n + i); }
184 return nn == 0 ? 0 :
static_cast<size_t>(std::bit_width(
nn));
202 for (
size_t i = 2; i <=
n; ++i)
212 const size_t half =
size_t{1} << (
k - 1);
213 const size_t limit =
n - (
size_t{1} <<
k) + 1;
214 for (
size_t i = 0; i <
limit; ++i)
220 template <
class Getter>
224 for (
size_t i = 0; i <
n; ++i)
231 template <
class AlephIt>
235 for (; it.has_curr(); it.next_ne())
236 at(0, i++) = it.get_curr();
350 <<
"Gen_Sparse_Table::query: r=" << r <<
" >= n=" <<
n;
352 <<
"Gen_Sparse_Table::query: l=" <<
l <<
" > r=" << r;
355 return op(
at(
k,
l),
at(
k, r - (
size_t{1} <<
k) + 1));
368 <<
"Gen_Sparse_Table::get: index " << i <<
" >= size " <<
n;
392 for (
size_t i = 0; i <
n; ++i)
426 template <std::totally_ordered T>
451 template <std::totally_ordered 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.
void swap(Array &s) noexcept
Swap this with s
Dynamic singly linked list with functional programming support.
Sparse Table over an arbitrary associative and idempotent binary operation.
void build_upper_levels()
Build levels k >= 1 from already-populated level 0.
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 (floor(log2(n)) + 1).
Array< T > values() const
Reconstruct all original values into an Array.
void fill_and_build(Getter getter)
Fill level 0 from a 0-based indexed getter and build higher levels.
constexpr size_t size() const noexcept
Number of logical elements.
Gen_Sparse_Table(std::initializer_list< T > il, Op oper=Op())
Construct from an initializer list in O(n log n) time.
Gen_Sparse_Table(const std::vector< T > &values, Op oper=Op())
Construct from a std::vector<T> in O(n log n) time.
static constexpr size_t compute_cells(const size_t nn) noexcept
Number of flattened cells needed to store all levels.
T & at(const size_t k, const size_t i)
Access table[k][i] (0-based row k, 0-based column i).
void fill_from_aleph_it(AlephIt it)
Fill level 0 from an Aleph-style iterator and build higher levels.
Gen_Sparse_Table(const size_t num, const T &init_val, Op oper=Op())
Construct a sparse table with num elements, all equal to init_val.
Gen_Sparse_Table(const Gen_Sparse_Table &)=default
const T & at(const size_t k, const size_t i) const
static constexpr size_t compute_levels(const size_t nn) noexcept
Compute levels = floor(log2(n)) + 1; 0 if n == 0.
constexpr bool is_empty() const noexcept
True if the table contains no elements.
Gen_Sparse_Table(const DynList< T > &values, Op oper=Op())
Construct from a DynList<T> in O(n log n) time.
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).
T Item_Type
The type of the element stored in the table.
void swap(Gen_Sparse_Table &other) noexcept
Swap this table with other in O(1).
Gen_Sparse_Table(Gen_Sparse_Table &&) noexcept(std::is_nothrow_move_constructible_v< Array< T > > &&std::is_nothrow_move_constructible_v< Op >)=default
void build_log_table()
Precompute the log lookup table for lengths [0..n].
Gen_Sparse_Table(const Array< T > &values, Op oper=Op())
Construct from an Array<T> in O(n log n) time.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Binary operation compatible with Sparse Table queries.
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.
Functor returning the maximum of two values.
constexpr T operator()(const T &a, const T &b) const noexcept
Sparse Table for range maximum queries.
Functor returning the minimum of two values.
constexpr T operator()(const T &a, const T &b) const noexcept
Sparse Table for range minimum queries.
Dynamic array container with automatic resizing.
Alias for htlist.H (DynList implementation).