ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::Fenwick< G > Class Template Reference

the prefix aggregates of \( n \) values of the monoid G More...

#include <ADS/sequence/Fenwick.h>

Public types

using Value = typename G::Value
 the values
using Algebra = G
 their algebra

Public member functions

 Fenwick (std::size_t n=0)
 \( n \) values, all the identity
template<std::input_iterator I>
 Fenwick (I first, I last)
 the values of \( [ \mathit{first} , \mathit{last} ) \), in \( O( n ) \) time
std::size_t size (void) const
 the number \( n \) of values
bool empty (void) const
 whether there is no value
const Value & get (std::size_t i) const
 the value \( a_i \)
Value prefix (std::size_t r) const
 the prefix \( \pi_r = a_0 \oplus \cdots \oplus a_{r-1} \), the identity if \( r = 0 \), in \( O( \log n ) \) time
Value range (std::size_t l, std::size_t r) const
 the range \( a_l \oplus \cdots \oplus a_{r-1} \), i.e., \( \pi_l^{-1} \oplus \pi_r \), in \( O( \log n ) \) time
void set (std::size_t i, const Value &v)
 \( a_i \gets v \): by the difference in \( O( \log n ) \) time in an abelian group, by the parts of the nodes in \( O( \log^2 n ) \) time otherwise
void add (std::size_t i, const Value &d)
 \( a_i \gets a_i \oplus d \), in \( O( \log n ) \) time
void push_back (const Value &v)
 \( v \) appended as \( a_n \), its node built in \( O( \log n ) \) time
void pop_back (void)
 the last value removed, in \( O( 1 ) \) time, since no other node contains it
void clear (void) noexcept
 all the values removed
template<std::input_iterator I>
void assign (I first, I last)
 the values of \( [ \mathit{first} , \mathit{last} ) \) in place of all the others, the nodes built in \( O( n ) \) time
std::size_t period (void) const
 the updates by difference after which a group of floating point values is rebuilt from its values
void period (std::size_t p)
 sets the period of the rebuilding to \( p \)

Detailed description

template<Monoid G>
class ADS::Fenwick< G >

the prefix aggregates of \( n \) values of the monoid G

Member typedef documentation

◆ Algebra

template<Monoid G>
using ADS::Fenwick< G >::Algebra = G

their algebra

◆ Value

template<Monoid G>
using ADS::Fenwick< G >::Value = typename G::Value

the values

Constructor and destructor documentation

◆ Fenwick() [1/2]

template<Monoid G>
ADS::Fenwick< G >::Fenwick ( std::size_t n = 0)
inlineexplicit

\( n \) values, all the identity

◆ Fenwick() [2/2]

template<Monoid G>
template<std::input_iterator I>
ADS::Fenwick< G >::Fenwick ( I first,
I last )
inline

the values of \( [ \mathit{first} , \mathit{last} ) \), in \( O( n ) \) time

Member function documentation

◆ add()

template<Monoid G>
void ADS::Fenwick< G >::add ( std::size_t i,
const Value & d )

\( a_i \gets a_i \oplus d \), in \( O( \log n ) \) time

Precondition
\( i < n \)

◆ assign()

template<Monoid G>
template<std::input_iterator I>
void ADS::Fenwick< G >::assign ( I first,
I last )

the values of \( [ \mathit{first} , \mathit{last} ) \) in place of all the others, the nodes built in \( O( n ) \) time

◆ clear()

template<Monoid G>
void ADS::Fenwick< G >::clear ( void )
inlinenoexcept

all the values removed

◆ empty()

template<Monoid G>
bool ADS::Fenwick< G >::empty ( void ) const
inlinenodiscard

whether there is no value

◆ get()

template<Monoid G>
const Value & ADS::Fenwick< G >::get ( std::size_t i) const
inlinenodiscard

the value \( a_i \)

Precondition
\( i < n \)

◆ period() [1/2]

template<Monoid G>
void ADS::Fenwick< G >::period ( std::size_t p)
inline

sets the period of the rebuilding to \( p \)

Precondition
\( p \ge 1 \)

◆ period() [2/2]

template<Monoid G>
std::size_t ADS::Fenwick< G >::period ( void ) const
inlinenodiscard

the updates by difference after which a group of floating point values is rebuilt from its values

◆ pop_back()

template<Monoid G>
void ADS::Fenwick< G >::pop_back ( void )
inline

the last value removed, in \( O( 1 ) \) time, since no other node contains it

Precondition
the tree is not empty

◆ prefix()

template<Monoid G>
Value ADS::Fenwick< G >::prefix ( std::size_t r) const
nodiscard

the prefix \( \pi_r = a_0 \oplus \cdots \oplus a_{r-1} \), the identity if \( r = 0 \), in \( O( \log n ) \) time

In pseudocode (Fenwick, beyond the book), \( \lambda( j ) \) the lowest bit of \( j \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{prefix}( r ) \cr 2 & \quad x \gets e;\ j \gets r \cr 3 & \quad \textbf{while}\ j > 0\ \textbf{do}\ x \gets t_j \oplus x; \ j \gets j - \lambda( j ) \cr 4 & \quad \textbf{return}\ x \end{array}} \]

Precondition
\( r \le n \)

◆ push_back()

template<Monoid G>
void ADS::Fenwick< G >::push_back ( const Value & v)

\( v \) appended as \( a_n \), its node built in \( O( \log n ) \) time

◆ range()

template<Monoid G>
Value ADS::Fenwick< G >::range ( std::size_t l,
std::size_t r ) const
nodiscard

the range \( a_l \oplus \cdots \oplus a_{r-1} \), i.e., \( \pi_l^{-1} \oplus \pi_r \), in \( O( \log n ) \) time

Precondition
\( l \le r \le n \)

◆ set()

template<Monoid G>
void ADS::Fenwick< G >::set ( std::size_t i,
const Value & v )

\( a_i \gets v \): by the difference in \( O( \log n ) \) time in an abelian group, by the parts of the nodes in \( O( \log^2 n ) \) time otherwise

In pseudocode (beyond the book), \( \delta = a_i^{-1} \oplus v \) in an abelian group:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{set}( i , v ) \cr 2 & \quad \delta \gets a_i^{-1} \oplus v;\ a_i \gets v;\ j \gets i + 1 \cr 3 & \quad \textbf{while}\ j \le n\ \textbf{do}\ t_j \gets t_j \oplus \delta\ \text{(in a group)},\ t_j \gets \mathrm{node}( j ) \ \text{(otherwise)};\ j \gets j + \lambda( j ) \cr 4 & \textbf{function}\ \mathrm{node}( j ) \cr 5 & \quad x \gets e;\ k \gets \lambda( j ) / 2 \cr 6 & \quad \textbf{while}\ k \ge 1\ \textbf{do}\ x \gets x \oplus t_{j - k};\ k \gets k / 2 \cr 7 & \quad \textbf{return}\ x \oplus a_{j-1} \end{array}} \]

Precondition
\( i < n \)

◆ size()

template<Monoid G>
std::size_t ADS::Fenwick< G >::size ( void ) const
inlinenodiscard

the number \( n \) of values


The documentation for this class was generated from the following file: