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

Disjoint Sparse Table over an arbitrary associative binary operation. More...

#include <tpl_disjoint_sparse_table.H>

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

Public Types

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

Public Member Functions

 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 (std::initializer_list< T > il, Op oper=Op())
 Construct from an initializer list in O(n log n) time.
 
 Gen_Disjoint_Sparse_Table (const Array< T > &values, Op oper=Op())
 Construct from an Array<T> 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.
 
 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 (const Gen_Disjoint_Sparse_Table &)=default
 
 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.
 
Gen_Disjoint_Sparse_Table & operator= (const Gen_Disjoint_Sparse_Table &)=default
 
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.
 
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.
 
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).
 

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 ()
 Build the disjoint sparse table from the data array.
 
template<class Getter >
void fill_data (Getter getter)
 Fill data array from a 0-based indexed getter.
 
template<class AlephIt >
void fill_data_from_aleph_it (AlephIt it)
 Fill data array from an Aleph-style iterator.
 

Static Private Member Functions

static constexpr size_t compute_levels (const size_t nn) noexcept
 Compute the number of levels for n elements.
 

Private Attributes

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

Detailed Description

template<typename T, class Op>
requires DisjointSparseTableOp<Op, T>
class Aleph::Gen_Disjoint_Sparse_Table< T, Op >

Disjoint Sparse Table over an arbitrary associative binary operation.

The structure decomposes the array into disjoint blocks at each level and stores prefix/suffix aggregates from each block's midpoint. For a query [l, r] with l != r, the answer is obtained by combining the suffix aggregate ending at l with the prefix aggregate starting at r, both relative to the block midpoint at the appropriate level:

\[ \text{query}(l, r) = \text{Op}\bigl( \text{suffix}[k][l],\; \text{prefix}[k][r] \bigr), \quad k = \lfloor \log_2(l \oplus r) \rfloor \]

Because the sub-ranges are disjoint (non-overlapping), the operation need not be idempotent — only associative.

Template Parameters
Telement type.
Opassociative binary functor.
Example
// Range-sum disjoint sparse table
int s = st.query(1, 3); // 1 + 4 + 1 = 6
Disjoint Sparse Table over an arbitrary associative binary operation.
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).

Definition at line 152 of file tpl_disjoint_sparse_table.H.

Member Typedef Documentation

◆ Item_Type

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

The type of the element stored in the table.

Definition at line 236 of file tpl_disjoint_sparse_table.H.

Constructor & Destructor Documentation

◆ Gen_Disjoint_Sparse_Table() [1/7]

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

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

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

Definition at line 245 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data().

◆ Gen_Disjoint_Sparse_Table() [2/7]

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

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

Example
assert(st.query(0, 4) == 14); // 3 + 1 + 4 + 1 + 5

Definition at line 264 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data().

◆ Gen_Disjoint_Sparse_Table() [3/7]

template<typename T , class Op >
Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_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 binary functor.

Definition at line 280 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::values().

◆ Gen_Disjoint_Sparse_Table() [4/7]

template<typename T , class Op >
Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_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 binary functor.

Definition at line 296 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::values().

◆ Gen_Disjoint_Sparse_Table() [5/7]

template<typename T , class Op >
Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_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 binary functor.

Definition at line 313 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data_from_aleph_it(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::values().

◆ Gen_Disjoint_Sparse_Table() [6/7]

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

◆ Gen_Disjoint_Sparse_Table() [7/7]

template<typename T , class Op >
Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table ( Gen_Disjoint_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.
Exceptions
whatmoving the operation throws; then other is left empty.

Definition at line 336 of file tpl_disjoint_sparse_table.H.

Member Function Documentation

◆ at() [1/2]

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

◆ at() [2/2]

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

◆ build()

◆ compute_levels()

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

Compute the number of levels for n elements.

For n <= 1 no levels are needed (single-element queries use data directly). For n >= 2, levels = bit_width(n - 1).

Definition at line 173 of file tpl_disjoint_sparse_table.H.

◆ fill_data()

◆ fill_data_from_aleph_it()

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

Fill data array from an Aleph-style iterator.

Definition at line 227 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data.

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

◆ get()

◆ is_empty()

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

◆ num_levels()

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

template<typename T , class Op >
Gen_Disjoint_Sparse_Table & Aleph::Gen_Disjoint_Sparse_Table< T, Op >::operator= ( Gen_Disjoint_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 351 of file tpl_disjoint_sparse_table.H.

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

◆ query()

template<typename T , class Op >
T Aleph::Gen_Disjoint_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. Idempotency is not required.
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 370 of file tpl_disjoint_sparse_table.H.

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

Referenced by scenario_construction(), scenario_parlay_betting(), scenario_probability(), scenario_sales_analytics(), scenario_xor_checksum(), test_adversarial_plateau_with_spike(), test_adversarial_single_outlier(), test_adversarial_zigzag(), test_all_equal(), test_boundary_queries_no_throw(), test_construct_all_identical(), test_construct_from_array(), test_construct_from_dynlist(), test_construct_from_init_list(), test_construct_from_vector(), test_construct_uniform(), test_copy_assignment(), test_copy_constructor(), test_double_product(), test_double_sum(), test_exception_l_greater_than_r(), test_exception_r_out_of_range(), test_get_equals_point_query(), test_int_limits(), test_known_product_array(), test_known_sum_array(), test_max_cross_validation(), test_min_cross_validation(), test_move_assignment(), test_move_constructor(), test_negative_values(), test_non_idempotent_correctness(), test_non_power_of_two_sizes(), test_perf_queries(), test_power_of_two_sizes(), test_prefix_sum_consistency(), test_self_copy_assignment(), test_self_swap(), test_single_element_product(), test_single_element_sum(), test_sliding_window(), test_sorted_ascending(), test_sorted_descending(), test_splitting_composability(), test_splitting_product(), test_stress_double_sum(), test_stress_exhaustive_small(), test_stress_point_queries(), test_stress_product_small(), test_stress_sum_large(), test_stress_sum_small(), test_string_concatenation(), test_string_stress(), test_swap(), test_two_elements(), test_xor_known(), test_xor_stress(), and Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::use().

◆ size()

◆ swap()

template<typename T , class Op >
void Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap ( Gen_Disjoint_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 431 of file tpl_disjoint_sparse_table.H.

References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::levels, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::op, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::table.

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

◆ values()

Member Data Documentation

◆ data

◆ levels

◆ n

◆ op

◆ table


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