Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Fenwick_Tree< T, Plus, Minus > Class Template Reference

Fenwick tree over an arbitrary abelian group. More...

#include <tpl_fenwick_tree.H>

Inheritance diagram for Aleph::Gen_Fenwick_Tree< T, Plus, Minus >:
[legend]
Collaboration diagram for Aleph::Gen_Fenwick_Tree< T, Plus, Minus >:
[legend]

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)
 

Detailed Description

template<typename T, class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
requires (FenwickBinaryOp<Plus, T> && FenwickBinaryOp<Minus, T>)
class Aleph::Gen_Fenwick_Tree< T, Plus, Minus >

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:

The identity element is assumed to be T() (the value produced by the default constructor).

Template Parameters
Telement type.
Plusbinary functor for the group operation.
Minusbinary functor for the inverse operation.
Example
// XOR Fenwick tree
struct Xor { int operator()(int a, int b) const { return a ^ b; } };
Gen_Fenwick_Tree<int, Xor, Xor> ft(10); // XOR is its own inverse
ft.update(3, 7);
int pfx = ft.prefix(5);

Definition at line 139 of file tpl_fenwick_tree.H.

Member Typedef Documentation

◆ Item_Type

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
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.

Constructor & Destructor Documentation

◆ Gen_Fenwick_Tree() [1/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( const size_t  num,
Plus  pop = Plus(),
Minus  mop = Minus() 
)
inline

Construct a tree with num elements, all equal to the identity T().

Parameters
numnumber of elements.
popgroup operation functor.
mopgroup subtraction functor.

Definition at line 207 of file tpl_fenwick_tree.H.

◆ Gen_Fenwick_Tree() [2/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( std::initializer_list< T >  il,
Plus  pop = Plus(),
Minus  mop = Minus() 
)
inline

Construct from an initializer list in O(n) time.

Example
Gen_Fenwick_Tree<int> ft = {3, 1, 4, 1, 5};
assert(ft.prefix(2) == 8); // 3 + 1 + 4
Fenwick tree over an arbitrary abelian group.
T prefix(size_t i) const
Prefix query: Plus(a[0], a[1], ..., a[i]).

Definition at line 223 of file tpl_fenwick_tree.H.

◆ Gen_Fenwick_Tree() [3/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( const Array< T > &  values,
Plus  pop = Plus(),
Minus  mop = Minus() 
)
inline

Construct from an Array<T> in O(n) time.

Parameters
valuessource array with the initial element values.
popgroup operation functor.
mopgroup subtraction functor.

Definition at line 237 of file tpl_fenwick_tree.H.

◆ Gen_Fenwick_Tree() [4/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( const std::vector< T > &  values,
Plus  pop = Plus(),
Minus  mop = Minus() 
)
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.

Parameters
valuessource vector with the initial element values.
popgroup operation functor.
mopgroup subtraction functor.

Definition at line 254 of file tpl_fenwick_tree.H.

◆ Gen_Fenwick_Tree() [5/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( const DynList< T > &  values,
Plus  pop = Plus(),
Minus  mop = Minus() 
)
inline

Construct from a DynList<T> in O(n) time.

Parameters
valuessource list with the initial element values.
popgroup operation functor.
mopgroup subtraction functor.

Definition at line 268 of file tpl_fenwick_tree.H.

References LocateFunctions< Container, Type >::get_it().

◆ Gen_Fenwick_Tree() [6/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( const Gen_Fenwick_Tree< T, Plus, Minus > &  other)
default

Copy another tree: its array, its size and its operations.

Parameters
[in]othertree to copy.

◆ Gen_Fenwick_Tree() [7/7]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::Gen_Fenwick_Tree ( Gen_Fenwick_Tree< T, Plus, Minus > &&  other)
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.

Parameters
[in,out]othertree to move from.
Exceptions
whatmoving the operations throws; then other is left empty.

Definition at line 290 of file tpl_fenwick_tree.H.

Member Function Documentation

◆ build()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::build ( )
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.

◆ fill_from_aleph_it()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
template<class AlephIt >
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::fill_from_aleph_it ( AlephIt  it)
inlineprotected

Fill the internal storage from an Aleph-style iterator (get_it()) and build.

Definition at line 188 of file tpl_fenwick_tree.H.

◆ fill_from_indexed()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
template<class F >
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::fill_from_indexed ( F  getter)
inlineprotected

Fill the internal storage from a 0-based indexed getter and build.

Definition at line 169 of file tpl_fenwick_tree.H.

◆ fill_from_std_iter()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
template<class StdIt >
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::fill_from_std_iter ( StdIt  first,
StdIt  last 
)
inlineprotected

Fill the internal storage from a standard iterator range and build.

Definition at line 178 of file tpl_fenwick_tree.H.

◆ get()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
T Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::get ( const size_t  i) const
inline

Retrieve the logical value a[i].

Equivalent to query(i, i). O(log n).

Parameters
i0-based index.
Exceptions
std::out_of_rangeif i >= size().

Definition at line 371 of file tpl_fenwick_tree.H.

Referenced by scenario_order_book(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ is_empty()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
constexpr bool Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::is_empty ( ) const
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().

◆ lowbit()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
static constexpr size_t Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::lowbit ( size_t  x)
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.

◆ operator=() [1/2]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Gen_Fenwick_Tree & Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::operator= ( const Gen_Fenwick_Tree< T, Plus, Minus > &  other)
default

Copy another tree into this one: its array, its size and its operations.

Parameters
[in]othertree to copy.
Returns
this tree.

◆ operator=() [2/2]

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Gen_Fenwick_Tree & Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::operator= ( Gen_Fenwick_Tree< T, Plus, Minus > &&  other)
inlinenoexcept

Exchange this tree with another, which is left with the previous elements of this one.

Parameters
[in,out]othertree to move from.
Returns
this tree.

Definition at line 309 of file tpl_fenwick_tree.H.

References Aleph::swap().

◆ prefix()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
T Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::prefix ( size_t  i) const
inline

Prefix query: Plus(a[0], a[1], ..., a[i]).

Parameters
i0-based index.
Returns
accumulated value from index 0 through i (inclusive).
Exceptions
std::out_of_rangeif 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().

◆ query()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
T Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::query ( const size_t  l,
const size_t  r 
) const
inline

Range query: Plus(a[l], a[l+1], ..., a[r]).

Parameters
l0-based left index (inclusive).
r0-based right index (inclusive).
Returns
accumulated value over [l, r].
Exceptions
std::out_of_rangeif 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().

◆ set()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::set ( const size_t  i,
const T &  value 
)
inline

Set a[i] = value.

Computes the required delta and calls update(). O(log n).

Parameters
i0-based index.
valuenew value for position i.
Exceptions
std::out_of_rangeif i >= size().

Definition at line 384 of file tpl_fenwick_tree.H.

References value.

Referenced by TEST(), and TEST().

◆ size()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
constexpr size_t Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::size ( ) const
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().

◆ swap()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::swap ( Gen_Fenwick_Tree< T, Plus, Minus > &  other)
inlinenoexcept

Swap this tree with other in O(1).

Parameters
[in,out]othertree to exchange with.
Exceptions
whatswapping 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().

◆ swap_operations()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::swap_operations ( Gen_Fenwick_Tree< T, Plus, Minus > &  other)
inlineprivate

◆ update()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
void Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::update ( size_t  i,
const T &  delta 
)
inline

Point update: a[i] = Plus(a[i], delta).

Parameters
i0-based index.
deltavalue to add.
Exceptions
std::out_of_rangeif 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().

◆ values()

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Array< T > Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::values ( ) const
inline

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().

Member Data Documentation

◆ minus_op

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Minus Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::minus_op
protected

◆ n

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
size_t Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::n = 0
protected

Definition at line 143 of file tpl_fenwick_tree.H.

◆ plus_op

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Plus Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::plus_op
protected

◆ tree

template<typename T , class Plus = Aleph::plus<T>, class Minus = Aleph::minus<T>>
Array<T> Aleph::Gen_Fenwick_Tree< T, Plus, Minus >::tree
protected

Definition at line 142 of file tpl_fenwick_tree.H.


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