|
ADS
Algorithms and Data Structures
|
The prefix aggregates of a sequence by the tree of Fenwick, beyond the book, over any algebra of Algebra.h, and the observer that keeps them for an ObservableArray [see ObservableArray.h]. More...
#include "ADS/Algebra.h"#include "ADS/Contract.h"#include "ADS/Modification.h"#include <concepts>#include <cstddef>#include <iterator>#include <type_traits>#include <vector>#include "ADS/sequence/impl/Fenwick.tpp"Go to the source code of this file.
Classes | |
| class | ADS::Fenwick< G > |
| the prefix aggregates of \( n \) values of the monoid G More... | |
| class | ADS::FenwickObserver< G > |
| the prefix aggregates of the values of an array of values of G that tells its changes [see ObservableArray], a Fenwick< G > kept by its events More... | |
Namespaces | |
| namespace | ADS |
The prefix aggregates of a sequence by the tree of Fenwick, beyond the book, over any algebra of Algebra.h, and the observer that keeps them for an ObservableArray [see ObservableArray.h].
Fenwick< G > holds the values \( a_0 , \ldots , a_{n-1} \) of the monoid G and, for \( j = 1 , \ldots , n \), the node
\[ t_j = a_{j - \lambda( j )} \oplus \cdots \oplus a_{j - 1} , \qquad \lambda( j ) = j \mathbin{\&} ( - j ) , \]
the combination of the \( \lambda( j ) \) values that end at the index \( j - 1 \), \( \lambda( j ) \) the lowest bit of \( j \). The prefix \( a_0 \oplus \cdots \oplus a_{r-1} \) is the combination of the nodes of the binary digits of \( r \), at most \( \lceil \log_2 ( r + 1 ) \rceil \) of them, for any monoid; a node is the combination of the nodes \( j - 2^k \), \( 2^k < \lambda( j ) \), and of \( a_{j-1} \), so that the nodes are built in \( O( n ) \) and one more is appended in \( O( \log n ) \). The update of a value depends on the algebra: an abelian group changes the at most \( \lceil \log_2 ( n + 1 ) \rceil \) nodes that contain it by one combination with the difference \( a_i^{-1} \oplus v \), \( O( \log n ) \); a commutative monoid does the same for add(), the value combined with \( d \); any other monoid rebuilds those nodes from their parts, \( O( \log^2 n ) \). The range \( a_l \oplus \cdots \oplus a_{r-1} \) is \( \pi_l^{-1} \oplus \pi_r \) in a group, \( \pi_r \) the prefix of length \( r \). A group of floating point values (not exact_v) accumulates the rounding errors of the differences, and the tree is rebuilt from the values after every period() updates by difference, \( O( 1 ) \) amortized time per update when the period is at least \( n \) (see Prefix aggregates of an observable array).
FenwickObserver< G > is an observer of an array of values of G that tells its changes, as ObservableArray< G::Value >: it applies Assigned by set(), Inserted by push_back(), Erased by pop_back() and Cleared by clear(), and on Rebuilt it forgets its tree and builds it again from the array at its next query (the events after Rebuilt are then ignored, since the array it reads has them); audit( a ) compares it with the array.
The contracts [see Contract.h]: the indices are in \( [ 0 , n ) \), the lengths of the prefixes in \( [ 0 , n ] \), range() needs a group and \( l \le r \), add() a commutative monoid, pop_back() a tree that is not empty; an observer is built from its array, or given it by rebuild(), before the array changes. The operations are defined in Fenwick.tpp.