|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Fenwick tree over an arbitrary abelian group. More...
#include <tpl_fenwick_tree.H>
Public Types | |
| using | Item_Type = T |
| The type of element stored in the tree. | |
Public Member Functions | |
| Gen_Fenwick_Tree (const size_t num, Plus pop=Plus(), Minus mop=Minus()) | |
Construct a tree with num elements, all equal to the identity T(). | |
| Gen_Fenwick_Tree (std::initializer_list< T > il, Plus pop=Plus(), Minus mop=Minus()) | |
| Construct from an initializer list in O(n) time. | |
| Gen_Fenwick_Tree (const Array< T > &values, Plus pop=Plus(), Minus mop=Minus()) | |
| Construct from an Array<T> in O(n) time. | |
| Gen_Fenwick_Tree (const std::vector< T > &values, Plus pop=Plus(), Minus mop=Minus()) | |
| Construct from a std::vector<T> in O(n) time. | |
| Gen_Fenwick_Tree (const DynList< T > &values, Plus pop=Plus(), Minus mop=Minus()) | |
| Construct from a DynList<T> in O(n) time. | |
| Gen_Fenwick_Tree (const Gen_Fenwick_Tree &other)=default | |
| Copy another tree: its array, its size and its operations. | |
| Gen_Fenwick_Tree (Gen_Fenwick_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Plus > and std::is_nothrow_move_constructible_v< Minus >) | |
| Take the elements of another tree, which is left empty. | |
| Gen_Fenwick_Tree & | operator= (const Gen_Fenwick_Tree &other)=default |
| Copy another tree into this one: its array, its size and its operations. | |
| Gen_Fenwick_Tree & | operator= (Gen_Fenwick_Tree &&other) noexcept(noexcept(swap(other))) |
| Exchange this tree with another, which is left with the previous elements of this one. | |
| void | update (size_t i, const T &delta) |
Point update: a[i] = Plus(a[i], delta). | |
| T | prefix (size_t i) const |
Prefix query: Plus(a[0], a[1], ..., a[i]). | |
| T | query (const size_t l, const size_t r) const |
Range query: Plus(a[l], a[l+1], ..., a[r]). | |
| T | get (const size_t i) const |
Retrieve the logical value a[i]. | |
| void | set (const size_t i, const T &value) |
Set a[i] = value. | |
| 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_Fenwick_Tree &other) noexcept(std::is_nothrow_swappable_v< Plus > and std::is_nothrow_swappable_v< Minus >) |
Swap this tree with other in O(1). | |
Protected Member Functions | |
| void | build () |
| O(n) bottom-up construction of the tree array from raw values already placed in tree[1..n]. | |
| template<class F > | |
| void | fill_from_indexed (F getter) |
| Fill the internal storage from a 0-based indexed getter and build. | |
| template<class StdIt > | |
| void | fill_from_std_iter (StdIt first, StdIt last) |
| Fill the internal storage from a standard iterator range and build. | |
| template<class AlephIt > | |
| void | fill_from_aleph_it (AlephIt it) |
| Fill the internal storage from an Aleph-style iterator (get_it()) and build. | |
Static Protected Member Functions | |
| static constexpr size_t | lowbit (size_t x) noexcept |
Return the least significant set bit of x (as a mask). | |
Protected Attributes | |
| Array< T > | tree |
| size_t | n = 0 |
| Plus | plus_op |
| Minus | minus_op |
Private Member Functions | |
| void | swap_operations (Gen_Fenwick_Tree &other) |
Fenwick tree over an arbitrary abelian group.
Maintains an implicit array a[0..n-1] and a companion tree array so that prefix queries a[0] + a[1] + ... + a[i] and point updates a[i] += delta are answered in O(log n).
The group is specified through two binary functors:
Plus(a, b) — the group operation (must be associative and commutative).Minus(a, b) — the group subtraction, i.e. Plus(Minus(a, b), b) == a.The identity element is assumed to be T() (the value produced by the default constructor).
| T | element type. |
| Plus | binary functor for the group operation. |
| Minus | binary functor for the inverse operation. |
Definition at line 139 of file tpl_fenwick_tree.H.
| using Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Item_Type = T |
The type of element stored in the tree.
Definition at line 198 of file tpl_fenwick_tree.H.
|
inline |
Construct a tree with num elements, all equal to the identity T().
| num | number of elements. |
| pop | group operation functor. |
| mop | group subtraction functor. |
Definition at line 207 of file tpl_fenwick_tree.H.
|
inline |
Construct from an initializer list in O(n) time.
Definition at line 223 of file tpl_fenwick_tree.H.
|
inline |
Construct from an Array<T> in O(n) time.
| values | source array with the initial element values. |
| pop | group operation functor. |
| mop | group subtraction functor. |
Definition at line 237 of file tpl_fenwick_tree.H.
|
inline |
Construct from a std::vector<T> in O(n) time.
This overload is intentionally provided as STL-interoperability convenience. Aleph containers remain the primary API surface.
| values | source vector with the initial element values. |
| pop | group operation functor. |
| mop | group subtraction functor. |
Definition at line 254 of file tpl_fenwick_tree.H.
|
inline |
Construct from a DynList<T> in O(n) time.
| values | source list with the initial element values. |
| pop | group operation functor. |
| mop | group subtraction functor. |
Definition at line 268 of file tpl_fenwick_tree.H.
References LocateFunctions< Container, Type >::get_it().
|
default |
Copy another tree: its array, its size and its operations.
| [in] | other | tree to copy. |
|
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. |
| what | moving the operations throws; then other is left empty. |
Definition at line 290 of file tpl_fenwick_tree.H.
|
inlineprotected |
O(n) bottom-up construction of the tree array from raw values already placed in tree[1..n].
Definition at line 160 of file tpl_fenwick_tree.H.
|
inlineprotected |
Fill the internal storage from an Aleph-style iterator (get_it()) and build.
Definition at line 188 of file tpl_fenwick_tree.H.
|
inlineprotected |
Fill the internal storage from a 0-based indexed getter and build.
Definition at line 169 of file tpl_fenwick_tree.H.
|
inlineprotected |
Fill the internal storage from a standard iterator range and build.
Definition at line 178 of file tpl_fenwick_tree.H.
|
inline |
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 393 of file tpl_fenwick_tree.H.
Referenced by Moved_From_Case< Fenwick_Tree< int > >::is_empty(), TEST(), and Moved_From_Case< Fenwick_Tree< int > >::use().
|
inlinestaticconstexprprotectednoexcept |
Return the least significant set bit of x (as a mask).
This is the classic Fenwick-tree "lowbit" helper. Implemented using only well-defined unsigned arithmetic to avoid relying on signed-style negation of a size_t.
Definition at line 153 of file tpl_fenwick_tree.H.
|
default |
Copy another tree into this one: its array, its size and its operations.
| [in] | other | tree to copy. |
|
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 309 of file tpl_fenwick_tree.H.
References Aleph::swap().
|
inline |
Prefix query: Plus(a[0], a[1], ..., a[i]).
| i | 0-based index. |
| std::out_of_range | if i >= size(). |
Definition at line 336 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if.
Referenced by scenario_order_book(), scenario_pnl_dashboard(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Fenwick_Tree< int > >::use().
|
inline |
Range query: Plus(a[l], a[l+1], ..., a[r]).
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
[l, r]. | std::out_of_range | if l > r or r >= size(). |
Definition at line 354 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if, l, Aleph::prefix(), and r.
Referenced by scenario_pnl_dashboard(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 390 of file tpl_fenwick_tree.H.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Fenwick_Tree< int > >::use().
|
inlinenoexcept |
Swap this tree with other in O(1).
| [in,out] | other | tree to exchange with. |
| what | swapping the operations throws. They are exchanged before the arrays: if the swap of the subtractions throws, the additions 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 additions back throws too, both trees can only be destroyed or assigned. |
Definition at line 416 of file tpl_fenwick_tree.H.
References Aleph::Array< T >::swap().
Referenced by TEST().
|
inlineprivate |
Definition at line 431 of file tpl_fenwick_tree.H.
References Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::minus_op, Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::plus_op, and Aleph::swap().
|
inline |
Point update: a[i] = Plus(a[i], delta).
| i | 0-based index. |
| delta | value to add. |
| std::out_of_range | if i >= size(). |
Definition at line 321 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if.
Referenced by Moved_From_Case< Fenwick_Tree< int > >::make(), scenario_order_book(), scenario_pnl_dashboard(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Fenwick_Tree< int > >::use().
Reconstruct all original values into an Array.
O(n log n) in the general case.
Definition at line 399 of file tpl_fenwick_tree.H.
References Aleph::Array< T >::create().
Referenced by TEST().
|
protected |
Definition at line 146 of file tpl_fenwick_tree.H.
Referenced by Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::swap_operations().
|
protected |
Definition at line 143 of file tpl_fenwick_tree.H.
|
protected |
Definition at line 145 of file tpl_fenwick_tree.H.
Referenced by Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::swap_operations().
Definition at line 142 of file tpl_fenwick_tree.H.