|
Aleph-w 3.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 &&) noexcept=default | |
| Gen_Disjoint_Sparse_Table & | operator= (const Gen_Disjoint_Sparse_Table &)=default |
| Gen_Disjoint_Sparse_Table & | operator= (Gen_Disjoint_Sparse_Table &&) noexcept=default |
| 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 |
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 156 of file tpl_disjoint_sparse_table.H.
The type of the element stored in the table.
Definition at line 240 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 249 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::maps().
|
inline |
Construct from an initializer list in O(n log n) time.
Definition at line 268 of file tpl_disjoint_sparse_table.H.
References StlAlephIterator< SetName >::begin(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::build(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::fill_data(), and Aleph::maps().
|
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 284 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 300 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 317 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 |
|
defaultnoexcept |
Access table[k][i] (0-based row k, 0-based column i).
Definition at line 166 of file tpl_disjoint_sparse_table.H.
References Aleph::maps(), 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 167 of file tpl_disjoint_sparse_table.H.
References Aleph::maps(), 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 189 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::at(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::levels, Aleph::maps(), 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 177 of file tpl_disjoint_sparse_table.H.
References Aleph::maps().
|
inlineprivate |
Fill data array from a 0-based indexed getter.
Definition at line 223 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, Aleph::maps(), 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 231 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().
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 372 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(), and test_string_concatenation().
|
inlineconstexprnoexcept |
True if the table contains no elements.
Definition at line 385 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by test_single_element_sum().
|
inlineconstexprnoexcept |
Number of precomputed levels.
Definition at line 388 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::levels.
Referenced by test_single_element_sum().
|
default |
|
defaultnoexcept |
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 351 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, l, Aleph::maps(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::op.
Referenced by test_adversarial_plateau_with_spike(), test_adversarial_single_outlier(), test_adversarial_zigzag(), test_all_equal(), test_boundary_queries_no_throw(), test_construct_from_array(), test_construct_from_dynlist(), test_construct_from_init_list(), test_construct_from_vector(), test_construct_uniform(), test_copy_assignment(), 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_move_assignment(), test_negative_values(), 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(), and test_xor_stress().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 382 of file tpl_disjoint_sparse_table.H.
References Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n.
Referenced by test_copy_assignment(), test_move_assignment(), 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(), and test_swap().
|
inlinenoexcept |
Swap this table with other in O(1).
Definition at line 406 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::maps(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::n, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::op, and Aleph::Gen_Disjoint_Sparse_Table< T, Op >::table.
Referenced by test_self_swap(), and test_swap().
|
inline |
Reconstruct all original values into an Array.
O(n).
Definition at line 397 of file tpl_disjoint_sparse_table.H.
References Aleph::Array< T >::create(), Aleph::Gen_Disjoint_Sparse_Table< T, Op >::data, Aleph::maps(), 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 test_values_reconstruct().
Definition at line 158 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 161 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().
Definition at line 160 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().
Definition at line 163 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().
Definition at line 159 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().