|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Segment tree over an arbitrary associative binary operation. More...
#include <tpl_segment_tree.H>
Public Types | |
| using | Item_Type = T |
Public Member Functions | |
| Gen_Segment_Tree (const size_t num, const T &init_val, const T &id, Op oper=Op()) | |
Construct a segment tree with num elements, all equal to init_val. | |
| Gen_Segment_Tree (std::initializer_list< T > il, const T &id, Op oper=Op()) | |
| Construct from an initializer list. | |
| Gen_Segment_Tree (const Array< T > &values, const T &id, Op oper=Op()) | |
| Construct from an Array<T>. | |
| Gen_Segment_Tree (const std::vector< T > &values, const T &id, Op oper=Op()) | |
| Construct from a std::vector<T>. | |
| Gen_Segment_Tree (const DynList< T > &values, const T &id, Op oper=Op()) | |
| Construct from a DynList<T>. | |
| Gen_Segment_Tree (const Gen_Segment_Tree &)=default | |
| Gen_Segment_Tree (Gen_Segment_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_constructible_v< Op >) | |
| Take the elements of another tree, which is left empty. | |
| Gen_Segment_Tree & | operator= (const Gen_Segment_Tree &)=default |
| Gen_Segment_Tree & | operator= (Gen_Segment_Tree &&other) noexcept(noexcept(swap(other))) |
| Exchange this tree with another, which is left with the previous elements of this one. | |
| void | update (const size_t i, const T &delta) |
Point update: a[i] = Op(a[i], delta). | |
| void | set (const size_t i, const T &val) |
Set a[i] = val. | |
| T | query (const size_t l, const size_t r) const |
Range query over [l, r]. | |
| T | get (const size_t i) const |
Retrieve the value a[i]. | |
| constexpr size_t | size () const noexcept |
| Number of logical elements. | |
| constexpr bool | is_empty () const noexcept |
| True if the tree contains no elements. | |
| Array< T > | values () const |
| Reconstruct all original values into an Array. | |
| void | swap (Gen_Segment_Tree &other) noexcept(std::is_nothrow_swappable_v< T > and std::is_nothrow_swappable_v< Op >) |
Swap this tree with other in O(1). | |
Private Member Functions | |
| void | build (size_t node, const size_t start, const size_t end) |
| void | update_impl (size_t node, const size_t start, const size_t end, const size_t idx, const T &delta) |
| void | set_impl (size_t node, const size_t start, const size_t end, const size_t idx, const T &val) |
| T | query_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const |
| template<class Getter > | |
| void | fill_and_build (Getter getter) |
| template<class Getter > | |
| void | fill_leaves_recursive (size_t node, size_t start, size_t end, Getter &getter) |
| template<class AlephIt > | |
| void | fill_from_aleph_it (AlephIt it) |
| void | swap_identity_and_op (Gen_Segment_Tree &other) |
Private Attributes | |
| Array< T > | tree |
| size_t | n = 0 |
| T | identity |
| Op | op |
Segment tree over an arbitrary associative binary operation.
This is the classical segment tree supporting:
It maintains an implicit binary tree backed by Array<T>, using 1-based node indexing and allocating roughly 4*n nodes.
Unlike Sparse Table, the operation need not be idempotent. Unlike Fenwick Tree, the operation need not be invertible. Only associativity is required.
update(i, delta) applies a[i] = op(a[i], delta). Use set(i, val) if you need assignment semantics.| T | element type. |
| Op | associative binary functor. |
Definition at line 143 of file tpl_segment_tree.H.
| using Aleph::Gen_Segment_Tree< T, Op >::Item_Type = T |
Definition at line 247 of file tpl_segment_tree.H.
|
inline |
Construct a segment tree with num elements, all equal to init_val.
| num | number of elements. |
| init_val | value for every position. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 257 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.
|
inline |
Construct from an initializer list.
| il | initializer list with the element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 278 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.
|
inline |
Construct from an Array<T>.
| values | source array with the initial element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 296 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().
|
inline |
Construct from a std::vector<T>.
| values | source vector with the initial element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 311 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().
|
inline |
Construct from a DynList<T>.
| values | source list with the initial element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 326 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().
|
default |
|
inlinenoexcept |
Take the elements of another tree, which is left empty.
The implicit move took the array but copied the size: the moved-from tree then indexed an array that it no longer had.
| [in,out] | other | tree to move from. |
Definition at line 344 of file tpl_segment_tree.H.
|
inlineprivate |
Definition at line 150 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::Gen_Segment_Tree< T, Op >::op, and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::build(), and Aleph::Gen_Segment_Tree< T, Op >::fill_and_build().
|
inlineprivate |
Definition at line 210 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive(), and Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), and Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it().
|
inlineprivate |
Definition at line 235 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree().
|
inlineprivate |
Definition at line 220 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive(), and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive().
|
inline |
Retrieve the value a[i].
Equivalent to query(i, i). O(log n).
| i | 0-based index. |
| std::out_of_range | if i >= size(). |
Definition at line 419 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::query_impl().
Referenced by scenario_1_el_arbol_del_tiempo(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::Gen_Segment_Tree< T, Op >::values().
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 431 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::n.
|
default |
|
inlinenoexcept |
Exchange this tree with another, which is left with the previous elements of this one.
| [in,out] | other | tree to move from. |
Definition at line 359 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::swap().
|
inline |
Range query over [l, r].
Returns Op(a[l], a[l+1], ..., a[r]).
| 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 402 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Gen_Segment_Tree< T, Op >::n, Aleph::Gen_Segment_Tree< T, Op >::query_impl(), and r.
Referenced by scenario_1_el_arbol_del_tiempo(), scenario_4_rmq_comparison(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 195 of file tpl_segment_tree.H.
References Aleph::and, Aleph::Gen_Segment_Tree< T, Op >::identity, l, Aleph::Gen_Segment_Tree< T, Op >::op, Aleph::Gen_Segment_Tree< T, Op >::query_impl(), r, and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::get(), Aleph::Gen_Segment_Tree< T, Op >::query(), and Aleph::Gen_Segment_Tree< T, Op >::query_impl().
|
inline |
Set a[i] = val.
| i | 0-based index. |
| val | new value for position i. |
| std::out_of_range | if i >= size(). |
Definition at line 385 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::set_impl().
Referenced by scenario_1_el_arbol_del_tiempo(), scenario_4_rmq_comparison(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 178 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::op, Aleph::Gen_Segment_Tree< T, Op >::set_impl(), and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::set(), and Aleph::Gen_Segment_Tree< T, Op >::set_impl().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 428 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by scenario_1_el_arbol_del_tiempo(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Swap this tree with other in O(1).
| [in,out] | other | tree to exchange with. |
| what | swapping the identities or the operations throws. They are exchanged before the arrays: if the swap of the operations throws, the identities are exchanged back, and both trees are left as they were, if the swap that threw left its arguments as they were. If the swap that puts the identities back throws too, both trees can only be destroyed or assigned. |
Definition at line 454 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::n, Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op(), and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::operator=(), Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op(), and TEST().
|
inlineprivate |
Definition at line 477 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::identity, Aleph::Gen_Segment_Tree< T, Op >::op, and Aleph::Gen_Segment_Tree< T, Op >::swap().
Referenced by Aleph::Gen_Segment_Tree< T, Op >::swap().
|
inline |
Point update: a[i] = Op(a[i], delta).
| i | 0-based index. |
| delta | value to combine with a[i]. |
| std::out_of_range | if i >= size(). |
Definition at line 371 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 161 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::op, Aleph::Gen_Segment_Tree< T, Op >::tree, and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
Referenced by Aleph::Gen_Segment_Tree< T, Op >::update(), and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
|
inline |
Reconstruct all original values into an Array.
O(n log n).
Definition at line 437 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Segment_Tree< T, Op >::get(), and Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), and TEST().
|
private |
Definition at line 147 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::query_impl(), and Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op().
|
private |
Definition at line 146 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it(), Aleph::Gen_Segment_Tree< T, Op >::get(), Aleph::Gen_Segment_Tree< T, Op >::is_empty(), Aleph::Gen_Segment_Tree< T, Op >::query(), Aleph::Gen_Segment_Tree< T, Op >::set(), Aleph::Gen_Segment_Tree< T, Op >::size(), Aleph::Gen_Segment_Tree< T, Op >::swap(), Aleph::Gen_Segment_Tree< T, Op >::update(), and Aleph::Gen_Segment_Tree< T, Op >::values().
|
private |
Definition at line 148 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::Gen_Segment_Tree< T, Op >::query_impl(), Aleph::Gen_Segment_Tree< T, Op >::set_impl(), Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op(), and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
|
private |
Definition at line 145 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive(), Aleph::Gen_Segment_Tree< T, Op >::query_impl(), Aleph::Gen_Segment_Tree< T, Op >::set_impl(), Aleph::Gen_Segment_Tree< T, Op >::swap(), and Aleph::Gen_Segment_Tree< T, Op >::update_impl().