|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Sparse Table over an arbitrary associative and idempotent binary operation. More...
#include <tpl_sparse_table.H>
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 |
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 \]
| T | element type. |
| Op | associative and idempotent binary functor. |
Definition at line 234 of file tpl_sparse_table.H.
| 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.
|
inline |
Construct a sparse table with num elements, all equal to init_val.
| num | number of elements. |
| init_val | value for every position. |
| oper | associative 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.
|
inline |
Construct from an initializer list in O(n log n) time.
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.
|
inline |
Construct from an Array<T> in O(n log n) time.
| values | source array with the initial element values. |
| oper | associative 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().
|
inline |
Construct from a std::vector<T> in O(n log n) time.
| values | source vector with the initial element values. |
| oper | associative 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().
|
inline |
Construct from a DynList<T> in O(n log n) time.
| values | source list with the initial element values. |
| oper | associative 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().
|
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. |
Definition at line 400 of file tpl_sparse_table.H.
|
inlineprivate |
Access table[k][i] (0-based row k, 0-based column i).
Definition at line 244 of file tpl_sparse_table.H.
References k, Aleph::Gen_Sparse_Table< T, Op >::n, and Aleph::Gen_Sparse_Table< T, Op >::table.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels(), Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), Aleph::Gen_Sparse_Table< T, Op >::fill_from_aleph_it(), Aleph::Gen_Sparse_Table< T, Op >::get(), Aleph::Gen_Sparse_Table< T, Op >::query(), and Aleph::Gen_Sparse_Table< T, Op >::values().
|
inlineprivate |
Definition at line 245 of file tpl_sparse_table.H.
References k, Aleph::Gen_Sparse_Table< T, Op >::n, and Aleph::Gen_Sparse_Table< T, Op >::table.
|
inlineprivate |
Precompute the log lookup table for lengths [0..n].
Definition at line 260 of file tpl_sparse_table.H.
References Aleph::Array< T >::create(), Aleph::Gen_Sparse_Table< T, Op >::log_tbl, and Aleph::Gen_Sparse_Table< T, Op >::n.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), and Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table().
|
inlineprivate |
Build levels k >= 1 from already-populated level 0.
Definition at line 273 of file tpl_sparse_table.H.
References Aleph::Gen_Sparse_Table< T, Op >::at(), k, Aleph::Gen_Sparse_Table< T, Op >::levels, Aleph::Gen_Sparse_Table< T, Op >::n, and Aleph::Gen_Sparse_Table< T, Op >::op.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), and Aleph::Gen_Sparse_Table< T, Op >::fill_from_aleph_it().
|
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().
|
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().
|
inlineprivate |
Fill level 0 from a 0-based indexed getter and build higher levels.
Definition at line 287 of file tpl_sparse_table.H.
References Aleph::Gen_Sparse_Table< T, Op >::at(), Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels(), and Aleph::Gen_Sparse_Table< T, Op >::n.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), and Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table().
|
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().
|
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 450 of file tpl_sparse_table.H.
References ah_out_of_range_error_if, Aleph::Gen_Sparse_Table< T, Op >::at(), and Aleph::Gen_Sparse_Table< T, Op >::n.
Referenced by scenario_range_gcd(), scenario_sensor_monitoring(), scenario_sports_leaderboard(), TEST(), TEST(), test_boundary_queries_valid(), test_construct_from_array(), test_get_all_elements(), test_get_equals_point_query(), test_get_out_of_range(), test_self_copy_assignment(), test_single_element(), and test_stress_all_point_queries().
|
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().
|
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().
|
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 415 of file tpl_sparse_table.H.
References Aleph::Gen_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 and idempotent.| 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 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().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 459 of file tpl_sparse_table.H.
References Aleph::Gen_Sparse_Table< T, Op >::n.
Referenced by scenario_range_gcd(), scenario_sensor_monitoring(), scenario_sports_leaderboard(), TEST(), TEST(), 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_move_assignment(), test_move_constructor(), test_non_power_of_two_sizes(), test_performance_build(), test_performance_build_large(), test_power_of_two_sizes(), test_self_copy_assignment(), test_self_swap(), test_single_element(), and test_swap().
|
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 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().
|
inline |
Reconstruct all original values into an Array.
O(n).
Definition at line 474 of file tpl_sparse_table.H.
References Aleph::Gen_Sparse_Table< T, Op >::at(), Aleph::Array< T >::create(), and Aleph::Gen_Sparse_Table< T, Op >::n.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), scenario_construction(), TEST(), and test_values_reconstruction().
|
private |
Definition at line 239 of file tpl_sparse_table.H.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels(), Aleph::Gen_Sparse_Table< T, Op >::num_levels(), and Aleph::Gen_Sparse_Table< T, Op >::swap().
|
private |
Definition at line 237 of file tpl_sparse_table.H.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::query(), and Aleph::Gen_Sparse_Table< T, Op >::swap().
|
private |
Definition at line 238 of file tpl_sparse_table.H.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::Gen_Sparse_Table(), Aleph::Gen_Sparse_Table< T, Op >::at(), Aleph::Gen_Sparse_Table< T, Op >::at(), Aleph::Gen_Sparse_Table< T, Op >::build_log_table(), Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels(), Aleph::Gen_Sparse_Table< T, Op >::fill_and_build(), Aleph::Gen_Sparse_Table< T, Op >::get(), Aleph::Gen_Sparse_Table< T, Op >::is_empty(), Aleph::Gen_Sparse_Table< T, Op >::query(), Aleph::Gen_Sparse_Table< T, Op >::size(), Aleph::Gen_Sparse_Table< T, Op >::swap(), and Aleph::Gen_Sparse_Table< T, Op >::values().
|
private |
Definition at line 241 of file tpl_sparse_table.H.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::build_upper_levels(), Aleph::Gen_Sparse_Table< T, Op >::query(), and Aleph::Gen_Sparse_Table< T, Op >::swap().
|
private |
Definition at line 236 of file tpl_sparse_table.H.
Referenced by Aleph::Gen_Sparse_Table< T, Op >::at(), Aleph::Gen_Sparse_Table< T, Op >::at(), and Aleph::Gen_Sparse_Table< T, Op >::swap().