|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Disjoint Sparse Table over an arbitrary associative binary operation. More...
#include <tpl_disjoint_sparse_table.H>
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 |
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.
| T | element type. |
| Op | associative binary functor. |
Definition at line 152 of file tpl_disjoint_sparse_table.H.
| 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.
|
inline |
Construct a disjoint sparse table with num elements, all equal to init_val.
| num | number of elements. |
| init_val | value for every position. |
| oper | associative 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().
|
inline |
Construct from an initializer list in O(n log n) time.
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().
|
inline |
Construct from an Array<T> in O(n log n) time.
| values | source array with the initial element values. |
| oper | associative 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().
|
inline |
Construct from a std::vector<T> in O(n log n) time.
| values | source vector with the initial element values. |
| oper | associative 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().
|
inline |
Construct from a DynList<T> in O(n log n) time.
| values | source list with the initial element values. |
| oper | associative 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().
|
default |
|
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.
| [in,out] | other | table to move from. |
| what | moving the operation throws; then other is left empty. |
Definition at line 336 of file tpl_disjoint_sparse_table.H.
|
inlineprivate |
Access table[k][i] (0-based row k, 0-based column i).
Definition at line 162 of file tpl_disjoint_sparse_table.H.
References k, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::table.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query().
|
inlineprivate |
Definition at line 163 of file tpl_disjoint_sparse_table.H.
References k, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::table.
|
inlineprivate |
Build the disjoint sparse table from the data array.
For each level k (block size 2^(k+1), half = 2^k):
Definition at line 185 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::at(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, k, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::levels, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::op.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table().
|
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.
|
inlineprivate |
Fill data array from a 0-based indexed getter.
Definition at line 219 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table().
|
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().
|
inline |
Retrieve the value a[i] in O(1).
Equivalent to query(i, i).
| i | 0-based index. |
| std::out_of_range | if i >= size(). |
Definition at line 391 of file tpl_disjoint_sparse_table.H.
References ah_out_of_range_error_if, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by test_boundary_queries_no_throw(), test_exception_get_out_of_range(), test_get_all_elements(), test_get_equals_point_query(), test_prefix_sum_consistency(), test_self_copy_assignment(), test_single_element_sum(), test_stress_point_queries(), test_string_concatenation(), and Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::use().
|
inlineconstexprnoexcept |
True if the table contains no elements.
Definition at line 404 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::is_empty(), test_single_element_sum(), and Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::use().
|
inlineconstexprnoexcept |
Number of precomputed levels.
Definition at line 407 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::levels.
Referenced by Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::is_empty(), scenario_parlay_betting(), scenario_sales_analytics(), scenario_xor_checksum(), test_num_levels(), and test_single_element_sum().
|
default |
|
inlinenoexcept |
Exchange this table with another, which is left with the previous elements of this one.
| [in,out] | other | table to move from. |
Definition at line 351 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap().
|
inline |
Range query over [l, r] in O(1).
Returns Op(a[l], a[l+1], ..., a[r]).
Op to be associative. Idempotency is not required.| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
Op over the range. | std::out_of_range | if 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().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 401 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by scenario_parlay_betting(), scenario_sales_analytics(), scenario_xor_checksum(), test_copy_assignment(), test_copy_constructor(), test_move_assignment(), test_move_constructor(), test_non_power_of_two_sizes(), test_perf_build(), test_perf_build_large(), test_power_of_two_sizes(), test_self_copy_assignment(), test_self_swap(), test_single_element_sum(), test_swap(), and Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::use().
|
inlinenoexcept |
Swap this table with other in O(1).
| [in,out] | other | table to exchange with. |
| what | swapping 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().
|
inline |
Reconstruct all original values into an Array.
O(n).
Definition at line 416 of file tpl_disjoint_sparse_table.H.
References Aleph::Array< T >::create(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::Gen_Disjoint_Sparse_Table(), scenario_construction(), test_values_reconstruct(), and Moved_From_Case< Sum_Disjoint_Sparse_Table< int > >::use().
|
private |
Definition at line 154 of file tpl_disjoint_sparse_table.H.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data_from_aleph_it(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::get(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::values().
|
private |
Definition at line 157 of file tpl_disjoint_sparse_table.H.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::num_levels(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap().
|
private |
Definition at line 156 of file tpl_disjoint_sparse_table.H.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::at(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::at(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::get(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::is_empty(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::size(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::values().
|
private |
Definition at line 159 of file tpl_disjoint_sparse_table.H.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap().
|
private |
Definition at line 155 of file tpl_disjoint_sparse_table.H.
Referenced by Aleph::Gen_Disjoint_Sparse_Table< T, Op >::at(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::at(), and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::swap().