|
Aleph-w 3.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 &)=default | |
| Gen_Fenwick_Tree (Gen_Fenwick_Tree &&) noexcept=default | |
| Gen_Fenwick_Tree & | operator= (const Gen_Fenwick_Tree &)=default |
| Gen_Fenwick_Tree & | operator= (Gen_Fenwick_Tree &&) noexcept=default |
| 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 |
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 |
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 143 of file tpl_fenwick_tree.H.
The type of element stored in the tree.
Definition at line 202 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 211 of file tpl_fenwick_tree.H.
|
inline |
Construct from an initializer list in O(n) time.
Definition at line 227 of file tpl_fenwick_tree.H.
References StlAlephIterator< SetName >::begin(), StlAlephIterator< SetName >::end(), and Aleph::maps().
|
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 241 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 258 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 272 of file tpl_fenwick_tree.H.
References LocateFunctions< Container, Type >::get_it().
|
default |
|
defaultnoexcept |
|
inlineprotected |
O(n) bottom-up construction of the tree array from raw values already placed in tree[1..n].
Definition at line 164 of file tpl_fenwick_tree.H.
Fill the internal storage from an Aleph-style iterator (get_it()) and build.
Definition at line 192 of file tpl_fenwick_tree.H.
Fill the internal storage from a 0-based indexed getter and build.
Definition at line 173 of file tpl_fenwick_tree.H.
References Aleph::maps().
|
inlineprotected |
Fill the internal storage from a standard iterator range and build.
Definition at line 182 of file tpl_fenwick_tree.H.
Retrieve the logical 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 344 of file tpl_fenwick_tree.H.
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 366 of file tpl_fenwick_tree.H.
|
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 157 of file tpl_fenwick_tree.H.
References Aleph::maps().
|
default |
|
defaultnoexcept |
Prefix query: Plus(a[0], a[1], ..., a[i]).
| i | 0-based index. |
| std::out_of_range | if i >= size(). |
Definition at line 309 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if.
Referenced by TEST().
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 327 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if, l, and Aleph::prefix().
Set a[i] = value.
Computes the required delta and calls update(). O(log n).
| i | 0-based index. |
| value | new value for position i. |
| std::out_of_range | if i >= size(). |
Definition at line 357 of file tpl_fenwick_tree.H.
Number of logical elements.
Definition at line 363 of file tpl_fenwick_tree.H.
Referenced by TEST().
|
inlinenoexcept |
Swap this tree with other in O(1).
Definition at line 381 of file tpl_fenwick_tree.H.
References Aleph::maps(), and Aleph::Array< T >::swap().
Referenced by TEST().
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 294 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if.
Reconstruct all original values into an Array.
O(n log n) in the general case.
Definition at line 372 of file tpl_fenwick_tree.H.
References Aleph::Array< T >::create(), and Aleph::maps().
|
protected |
Definition at line 150 of file tpl_fenwick_tree.H.
|
protected |
Definition at line 147 of file tpl_fenwick_tree.H.
|
protected |
Definition at line 149 of file tpl_fenwick_tree.H.
Definition at line 146 of file tpl_fenwick_tree.H.