Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Sparse_Table< T, Op > Class Template Reference

Sparse Table over an arbitrary associative and idempotent binary operation. More...

#include <tpl_sparse_table.H>

Inheritance diagram for Aleph::Gen_Sparse_Table< T, Op >:
[legend]
Collaboration diagram for Aleph::Gen_Sparse_Table< T, Op >:
[legend]

Public Types

using Item_Type = T
 The type of the element stored in the table.
 

Public Member Functions

 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 (std::initializer_list< T > il, Op oper=Op())
 Construct from an initializer list in O(n log n) time.
 
 Gen_Sparse_Table (const Array< T > &values, Op oper=Op())
 Construct from an Array<T> 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.
 
 Gen_Sparse_Table (const DynList< T > &values, Op oper=Op())
 Construct from a DynList<T> in O(n log n) time.
 
 Gen_Sparse_Table (const Gen_Sparse_Table &)=default
 
 Gen_Sparse_Table (Gen_Sparse_Table &&other) noexcept(std::is_nothrow_move_constructible_v< Array< T > > &&std::is_nothrow_move_constructible_v< Op >)
 Take the elements of another table, which is left empty.
 
Gen_Sparse_Table & operator= (const Gen_Sparse_Table &)=default
 
Gen_Sparse_Table & operator= (Gen_Sparse_Table &&other) noexcept(noexcept(swap(other)))
 Exchange this table with another, which is left with the previous elements of this one.
 
T query (const size_t l, const size_t r) const
 Range query over [l, r] in O(1).
 
T get (const size_t i) const
 Retrieve the value a[i] in O(1).
 
constexpr size_t size () const noexcept
 Number of logical elements.
 
constexpr bool is_empty () const noexcept
 True if the table contains no elements.
 
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 swap (Gen_Sparse_Table &other) noexcept(std::is_nothrow_swappable_v< Op >)
 Swap this table with other in O(1).
 

Private Member Functions

T & at (const size_t k, const size_t i)
 Access table[k][i] (0-based row k, 0-based column i).
 
const T & at (const size_t k, const size_t i) const
 
void build_log_table ()
 Precompute the log lookup table for lengths [0..n].
 
void build_upper_levels ()
 Build levels k >= 1 from already-populated level 0.
 
template<class Getter >
void fill_and_build (Getter getter)
 Fill level 0 from a 0-based indexed getter and build higher levels.
 
template<class AlephIt >
void fill_from_aleph_it (AlephIt it)
 Fill level 0 from an Aleph-style iterator and build higher levels.
 

Static Private Member Functions

static constexpr size_t compute_levels (const size_t nn) noexcept
 Compute levels = floor(log2(n)) + 1; 0 if n == 0.
 
static constexpr size_t compute_cells (const size_t nn) noexcept
 Number of flattened cells needed to store all levels.
 

Private Attributes

Array< T > table
 
Array< size_t > log_tbl
 
size_t n = 0
 
size_t levels = 0
 
Op op
 

Detailed Description

template<typename T, class Op>
requires SparseTableOp<Op, T>
class Aleph::Gen_Sparse_Table< T, Op >

Sparse Table over an arbitrary associative and idempotent binary operation.

Maintains a precomputed table table[k][i] storing the result of applying Op over the subarray a[i .. i + 2^k - 1]. Since the operation is associative and idempotent, overlapping sub-ranges can be combined in O(1):

\[ \text{query}(l, r) = \text{Op}\bigl( \text{table}[k][l],\; \text{table}[k][r - 2^k + 1] \bigr), \quad k = \lfloor \log_2(r - l + 1) \rfloor \]

Template Parameters
Telement type.
Opassociative and idempotent binary functor.
Example
// Range-GCD sparse table
struct Gcd {
int operator()(int a, int b) const { return std::gcd(a, b); }
};
Gen_Sparse_Table<int, Gcd> st = {12, 6, 9, 3, 15};
int g = st.query(1, 3); // gcd(6, 9, 3) = 3

Definition at line 234 of file tpl_sparse_table.H.

Member Typedef Documentation

◆ Item_Type

template<typename T , class Op >
using Aleph::Gen_Sparse_Table< T, Op >::Item_Type = T

The type of the element stored in the table.

Definition at line 309 of file tpl_sparse_table.H.

Constructor & Destructor Documentation

◆ Gen_Sparse_Table() [1/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( const size_t  num,
const T &  init_val,
Op  oper = Op() 
)
inline

Construct a sparse table with num elements, all equal to init_val.

Parameters
numnumber of elements.
init_valvalue for every position.
operassociative and idempotent binary functor.

Definition at line 318 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), and Aleph::Gen_Sparse_Table< T, Op >::n.

◆ Gen_Sparse_Table() [2/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( std::initializer_list< T >  il,
Op  oper = Op() 
)
inline

Construct from an initializer list in O(n log n) time.

Example
Gen_Sparse_Table<int, Min_Op<int>> st = {3, 1, 4, 1, 5, 9};
assert(st.query(0, 3) == 1); // min(3, 1, 4, 1)
Sparse Table over an arbitrary associative and idempotent binary operation.
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).

Definition at line 336 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), and Aleph::Gen_Sparse_Table< T, Op >::n.

◆ Gen_Sparse_Table() [3/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( const Array< T > &  values,
Op  oper = Op() 
)
inline

Construct from an Array<T> in O(n log n) time.

Parameters
valuessource array with the initial element values.
operassociative and idempotent binary functor.

Definition at line 353 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), Aleph::Gen_Sparse_Table< T, Op >::n, and Aleph::Gen_Sparse_Table< T, Op >::values().

◆ Gen_Sparse_Table() [4/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( const std::vector< T > &  values,
Op  oper = Op() 
)
inline

Construct from a std::vector<T> in O(n log n) time.

Parameters
valuessource vector with the initial element values.
operassociative and idempotent binary functor.

Definition at line 367 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), Aleph::Gen_Sparse_Table< T, Op >::n, and Aleph::Gen_Sparse_Table< T, Op >::values().

◆ Gen_Sparse_Table() [5/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( const DynList< T > &  values,
Op  oper = Op() 
)
inline

Construct from a DynList<T> in O(n log n) time.

Parameters
valuessource list with the initial element values.
operassociative and idempotent binary functor.

Definition at line 381 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::fill_from_aleph_it(), Aleph::Gen_Sparse_Table< T, Op >::n, and Aleph::Gen_Sparse_Table< T, Op >::values().

◆ Gen_Sparse_Table() [6/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( const Gen_Sparse_Table< T, Op > &  )
default

◆ Gen_Sparse_Table() [7/7]

template<typename T , class Op >
Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table ( Gen_Sparse_Table< T, Op > &&  other)
inlinenoexcept

Take the elements of another table, which is left empty.

The implicit move took the arrays but copied the size and the number of levels: the moved-from table then indexed arrays that it no longer had.

Parameters
[in,out]othertable to move from.

Definition at line 400 of file tpl_sparse_table.H.

Member Function Documentation

◆ at() [1/2]

◆ at() [2/2]

template<typename T , class Op >
const T & Aleph::Gen_Sparse_Table< T, Op >::at ( const size_t  k,
const size_t  i 
) const
inlineprivate

◆ build_log_table()

◆ build_upper_levels()

template<typename T , class Op >
void Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels ( )
inlineprivate

◆ compute_cells()

template<typename T , class Op >
static constexpr size_t Aleph::Gen_Sparse_Table< T, Op >::compute_cells ( const size_t  nn)
inlinestaticconstexprprivatenoexcept

Number of flattened cells needed to store all levels.

Definition at line 254 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::compute_levels().

◆ compute_levels()

template<typename T , class Op >
static constexpr size_t Aleph::Gen_Sparse_Table< T, Op >::compute_levels ( const size_t  nn)
inlinestaticconstexprprivatenoexcept

Compute levels = floor(log2(n)) + 1; 0 if n == 0.

Definition at line 248 of file tpl_sparse_table.H.

Referenced by Aleph::Gen_Sparse_Table< T, Op >::compute_cells().

◆ fill_and_build()

template<typename T , class Op >
template<class Getter >
void Aleph::Gen_Sparse_Table< T, Op >::fill_and_build ( Getter  getter)
inlineprivate

◆ fill_from_aleph_it()

template<typename T , class Op >
template<class AlephIt >
void Aleph::Gen_Sparse_Table< T, Op >::fill_from_aleph_it ( AlephIt  it)
inlineprivate

Fill level 0 from an Aleph-style iterator and build higher levels.

Definition at line 298 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::at(), and Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels().

Referenced by Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table().

◆ get()

template<typename T , class Op >
T Aleph::Gen_Sparse_Table< T, Op >::get ( const size_t  i) const
inline

◆ is_empty()

template<typename T , class Op >
constexpr bool Aleph::Gen_Sparse_Table< T, Op >::is_empty ( ) const
inlineconstexprnoexcept

True if the table contains no elements.

Definition at line 462 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::n.

Referenced by TEST(), and test_single_element().

◆ num_levels()

template<typename T , class Op >
constexpr size_t Aleph::Gen_Sparse_Table< T, Op >::num_levels ( ) const
inlineconstexprnoexcept

Number of precomputed levels (floor(log2(n)) + 1).

Definition at line 465 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::levels.

Referenced by scenario_range_gcd(), scenario_sensor_monitoring(), test_num_levels_correctness(), and test_single_element().

◆ operator=() [1/2]

template<typename T , class Op >
Gen_Sparse_Table & Aleph::Gen_Sparse_Table< T, Op >::operator= ( const Gen_Sparse_Table< T, Op > &  )
default

◆ operator=() [2/2]

template<typename T , class Op >
Gen_Sparse_Table & Aleph::Gen_Sparse_Table< T, Op >::operator= ( Gen_Sparse_Table< T, Op > &&  other)
inlinenoexcept

Exchange this table with another, which is left with the previous elements of this one.

Parameters
[in,out]othertable to move from.
Returns
this table.

Definition at line 415 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::swap().

◆ query()

template<typename T , class Op >
T Aleph::Gen_Sparse_Table< T, Op >::query ( const size_t  l,
const size_t  r 
) const
inline

Range query over [l, r] in O(1).

Returns Op(a[l], a[l+1], ..., a[r]).

Note
Correctness requires Op to be associative and idempotent.
Parameters
l0-based left index (inclusive).
r0-based right index (inclusive).
Returns
result of applying Op over the range.
Exceptions
std::out_of_rangeif l > r or r >= size().

Definition at line 432 of file tpl_sparse_table.H.

References ah_out_of_range_error_if, Aleph::Gen_Sparse_Table< T, Op >::at(), k, l, Aleph::Gen_Sparse_Table< T, Op >::log_tbl, Aleph::Gen_Sparse_Table< T, Op >::n, Aleph::Gen_Sparse_Table< T, Op >::op, and r.

Referenced by scenario_3(), scenario_4(), scenario_4_rmq_comparison(), scenario_construction(), scenario_range_gcd(), scenario_sensor_monitoring(), scenario_sports_leaderboard(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), test_adversarial_plateau_with_dip(), test_adversarial_single_outlier(), test_adversarial_zigzag(), test_all_constructors_agree(), test_all_equal(), test_bitwise_and_stress(), test_bitwise_or_stress(), test_boundary_queries_valid(), test_construct_from_array(), test_construct_from_dynlist(), test_construct_from_initlist(), test_construct_from_vector(), test_construct_uniform_value(), test_copy_assignment(), test_copy_constructor(), test_disjoint_split_min(), test_double_stress(), test_double_values(), test_gcd_known(), test_gcd_stress(), test_get_equals_point_query(), test_idempotent_overlap_split(), test_int_extremes(), test_known_max_array(), test_known_min_array(), test_long_long_values(), test_max_cross_validation(), test_min_cross_validation(), test_move_assignment(), test_move_constructor(), test_negative_values(), test_non_power_of_two_sizes(), test_overlapping_ranges_idempotent(), test_performance_build_large(), test_performance_queries(), test_power_of_two_sizes(), test_query_l_greater_than_r(), test_query_r_out_of_range(), test_self_copy_assignment(), test_self_swap(), test_single_element(), test_single_element_max(), test_sliding_window(), test_sorted_ascending(), test_sorted_descending(), test_stress_all_pairs_small(), test_stress_all_point_queries(), test_stress_max_small(), test_stress_min_medium(), test_stress_min_small(), test_swap(), and test_two_elements().

◆ size()

◆ swap()

template<typename T , class Op >
void Aleph::Gen_Sparse_Table< T, Op >::swap ( Gen_Sparse_Table< T, Op > &  other)
inlinenoexcept

Swap this table with other in O(1).

Parameters
[in,out]othertable to exchange with.
Exceptions
whatswapping the operations throws. They are exchanged before the arrays, so that, if that swap throws, nothing else is exchanged, and both tables are left as they were if it left its arguments as they were.

Definition at line 489 of file tpl_sparse_table.H.

References Aleph::Gen_Sparse_Table< T, Op >::levels, Aleph::Gen_Sparse_Table< T, Op >::log_tbl, Aleph::Gen_Sparse_Table< T, Op >::n, Aleph::Gen_Sparse_Table< T, Op >::op, Aleph::Array< T >::swap(), Aleph::Gen_Sparse_Table< T, Op >::swap(), and Aleph::Gen_Sparse_Table< T, Op >::table.

Referenced by Aleph::Gen_Sparse_Table< T, Op >::operator=(), Aleph::Gen_Sparse_Table< T, Op >::swap(), TEST(), test_self_swap(), and test_swap().

◆ values()

Member Data Documentation

◆ levels

◆ log_tbl

template<typename T , class Op >
Array<size_t> Aleph::Gen_Sparse_Table< T, Op >::log_tbl
private

◆ n

◆ op

◆ table

template<typename T , class Op >
Array<T> Aleph::Gen_Sparse_Table< T, Op >::table
private

The documentation for this class was generated from the following file: