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

Segment tree over an arbitrary associative binary operation. More...

#include <tpl_segment_tree.H>

Inheritance diagram for Aleph::Gen_Segment_Tree< T, Op >:
[legend]
Collaboration diagram for Aleph::Gen_Segment_Tree< T, Op >:
[legend]

Public Types

using Item_Type = T
 

Public Member Functions

 Gen_Segment_Tree (const size_t num, const T &init_val, const T &id, Op oper=Op())
 Construct a segment tree with num elements, all equal to init_val.
 
 Gen_Segment_Tree (std::initializer_list< T > il, const T &id, Op oper=Op())
 Construct from an initializer list.
 
 Gen_Segment_Tree (const Array< T > &values, const T &id, Op oper=Op())
 Construct from an Array<T>.
 
 Gen_Segment_Tree (const std::vector< T > &values, const T &id, Op oper=Op())
 Construct from a std::vector<T>.
 
 Gen_Segment_Tree (const DynList< T > &values, const T &id, Op oper=Op())
 Construct from a DynList<T>.
 
 Gen_Segment_Tree (const Gen_Segment_Tree &)=default
 
 Gen_Segment_Tree (Gen_Segment_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_constructible_v< Op >)
 Take the elements of another tree, which is left empty.
 
Gen_Segment_Tree & operator= (const Gen_Segment_Tree &)=default
 
Gen_Segment_Tree & operator= (Gen_Segment_Tree &&other) noexcept(noexcept(swap(other)))
 Exchange this tree with another, which is left with the previous elements of this one.
 
void update (const size_t i, const T &delta)
 Point update: a[i] = Op(a[i], delta).
 
void set (const size_t i, const T &val)
 Set a[i] = val.
 
T query (const size_t l, const size_t r) const
 Range query over [l, r].
 
T get (const size_t i) const
 Retrieve the value a[i].
 
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_Segment_Tree &other) noexcept(std::is_nothrow_swappable_v< T > and std::is_nothrow_swappable_v< Op >)
 Swap this tree with other in O(1).
 

Private Member Functions

void build (size_t node, const size_t start, const size_t end)
 
void update_impl (size_t node, const size_t start, const size_t end, const size_t idx, const T &delta)
 
void set_impl (size_t node, const size_t start, const size_t end, const size_t idx, const T &val)
 
T query_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
 
template<class Getter >
void fill_and_build (Getter getter)
 
template<class Getter >
void fill_leaves_recursive (size_t node, size_t start, size_t end, Getter &getter)
 
template<class AlephIt >
void fill_from_aleph_it (AlephIt it)
 
void swap_identity_and_op (Gen_Segment_Tree &other)
 

Private Attributes

Array< T > tree
 
size_t n = 0
 
T identity
 
Op op
 

Detailed Description

template<typename T, class Op>
requires SegmentTreeOp<Op, T>
class Aleph::Gen_Segment_Tree< T, Op >

Segment tree over an arbitrary associative binary operation.

This is the classical segment tree supporting:

  • Point updates in O(log n)
  • Range queries in O(log n)

It maintains an implicit binary tree backed by Array<T>, using 1-based node indexing and allocating roughly 4*n nodes.

Unlike Sparse Table, the operation need not be idempotent. Unlike Fenwick Tree, the operation need not be invertible. Only associativity is required.

Identity element
Queries rely on an identity element id such that op(id, x) == x and op(x, id) == x.
Update semantics
update(i, delta) applies a[i] = op(a[i], delta). Use set(i, val) if you need assignment semantics.
Template Parameters
Telement type.
Opassociative binary functor.
Example
// Range-sum segment tree
st.query(1, 3); // 1 + 4 + 1 = 6
st.update(2, 10); // a[2] += 10 => {3, 1, 14, 1, 5}
st.query(1, 3); // 1 + 14 + 1 = 16
Segment tree over an arbitrary associative binary operation.
T query(const size_t l, const size_t r) const
Range query over [l, r].
void update(const size_t i, const T &delta)
Point update: a[i] = Op(a[i], delta).

Definition at line 143 of file tpl_segment_tree.H.

Member Typedef Documentation

◆ Item_Type

template<typename T , class Op >
using Aleph::Gen_Segment_Tree< T, Op >::Item_Type = T

Definition at line 247 of file tpl_segment_tree.H.

Constructor & Destructor Documentation

◆ Gen_Segment_Tree() [1/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( const size_t  num,
const T &  init_val,
const T &  id,
Op  oper = Op() 
)
inline

Construct a segment tree with num elements, all equal to init_val.

Parameters
numnumber of elements.
init_valvalue for every position.
ididentity element of the monoid.
operassociative binary functor.

Definition at line 257 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.

◆ Gen_Segment_Tree() [2/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( std::initializer_list< T >  il,
const T &  id,
Op  oper = Op() 
)
inline

Construct from an initializer list.

Parameters
ilinitializer list with the element values.
ididentity element of the monoid.
operassociative binary functor.
Example
assert(st.query(0, 2) == 8); // 3 + 1 + 4

Definition at line 278 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.

◆ Gen_Segment_Tree() [3/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( const Array< T > &  values,
const T &  id,
Op  oper = Op() 
)
inline

Construct from an Array<T>.

Parameters
valuessource array with the initial element values.
ididentity element of the monoid.
operassociative binary functor.

Definition at line 296 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().

◆ Gen_Segment_Tree() [4/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( const std::vector< T > &  values,
const T &  id,
Op  oper = Op() 
)
inline

Construct from a std::vector<T>.

Parameters
valuessource vector with the initial element values.
ididentity element of the monoid.
operassociative binary functor.

Definition at line 311 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().

◆ Gen_Segment_Tree() [5/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( const DynList< T > &  values,
const T &  id,
Op  oper = Op() 
)
inline

Construct from a DynList<T>.

Parameters
valuessource list with the initial element values.
ididentity element of the monoid.
operassociative binary functor.

Definition at line 326 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().

◆ Gen_Segment_Tree() [6/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( const Gen_Segment_Tree< T, Op > &  )
default

◆ Gen_Segment_Tree() [7/7]

template<typename T , class Op >
Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree ( Gen_Segment_Tree< T, Op > &&  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.

Definition at line 344 of file tpl_segment_tree.H.

Member Function Documentation

◆ build()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::build ( size_t  node,
const size_t  start,
const size_t  end 
)
inlineprivate

◆ fill_and_build()

◆ fill_from_aleph_it()

template<typename T , class Op >
template<class AlephIt >
void Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it ( AlephIt  it)
inlineprivate

◆ fill_leaves_recursive()

template<typename T , class Op >
template<class Getter >
void Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive ( size_t  node,
size_t  start,
size_t  end,
Getter &  getter 
)
inlineprivate

◆ get()

template<typename T , class Op >
T Aleph::Gen_Segment_Tree< T, Op >::get ( const size_t  i) const
inline

Retrieve the 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 419 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::query_impl().

Referenced by scenario_1_el_arbol_del_tiempo(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::Gen_Segment_Tree< T, Op >::values().

◆ is_empty()

template<typename T , class Op >
constexpr bool Aleph::Gen_Segment_Tree< T, Op >::is_empty ( ) const
inlineconstexprnoexcept

True if the tree contains no elements.

Definition at line 431 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::n.

Referenced by TEST(), and TEST().

◆ operator=() [1/2]

template<typename T , class Op >
Gen_Segment_Tree & Aleph::Gen_Segment_Tree< T, Op >::operator= ( const Gen_Segment_Tree< T, Op > &  )
default

◆ operator=() [2/2]

template<typename T , class Op >
Gen_Segment_Tree & Aleph::Gen_Segment_Tree< T, Op >::operator= ( Gen_Segment_Tree< T, Op > &&  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 359 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::swap().

◆ query()

template<typename T , class Op >
T Aleph::Gen_Segment_Tree< T, Op >::query ( const size_t  l,
const size_t  r 
) const
inline

Range query over [l, r].

Returns Op(a[l], a[l+1], ..., a[r]).

Parameters
l0-based left index (inclusive).
r0-based right index (inclusive).
Returns
result of applying Op over the range.
Exceptions
std::out_of_rangeif l > r or r >= size().

Definition at line 402 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, l, Aleph::Gen_Segment_Tree< T, Op >::n, Aleph::Gen_Segment_Tree< T, Op >::query_impl(), and r.

Referenced by scenario_1_el_arbol_del_tiempo(), scenario_4_rmq_comparison(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ query_impl()

template<typename T , class Op >
T Aleph::Gen_Segment_Tree< T, Op >::query_impl ( size_t  node,
const size_t  start,
const size_t  end,
const size_t  l,
const size_t  r 
) const
inlineprivate

◆ set()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::set ( const size_t  i,
const T &  val 
)
inline

Set a[i] = val.

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

Definition at line 385 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::set_impl().

Referenced by scenario_1_el_arbol_del_tiempo(), scenario_4_rmq_comparison(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ set_impl()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::set_impl ( size_t  node,
const size_t  start,
const size_t  end,
const size_t  idx,
const T &  val 
)
inlineprivate

◆ size()

template<typename T , class Op >
constexpr size_t Aleph::Gen_Segment_Tree< T, Op >::size ( ) const
inlineconstexprnoexcept

Number of logical elements.

Definition at line 428 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::n.

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

◆ swap()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::swap ( Gen_Segment_Tree< T, Op > &  other)
inlinenoexcept

Swap this tree with other in O(1).

Parameters
[in,out]othertree to exchange with.
Exceptions
whatswapping the identities or the operations throws. They are exchanged before the arrays: if the swap of the operations throws, the identities 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 identities back throws too, both trees can only be destroyed or assigned.

Definition at line 454 of file tpl_segment_tree.H.

References Aleph::Gen_Segment_Tree< T, Op >::n, Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op(), and Aleph::Gen_Segment_Tree< T, Op >::tree.

Referenced by Aleph::Gen_Segment_Tree< T, Op >::operator=(), Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op(), and TEST().

◆ swap_identity_and_op()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::swap_identity_and_op ( Gen_Segment_Tree< T, Op > &  other)
inlineprivate

◆ update()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::update ( const size_t  i,
const T &  delta 
)
inline

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

Parameters
i0-based index.
deltavalue to combine with a[i].
Exceptions
std::out_of_rangeif i >= size().

Definition at line 371 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::update_impl().

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

◆ update_impl()

template<typename T , class Op >
void Aleph::Gen_Segment_Tree< T, Op >::update_impl ( size_t  node,
const size_t  start,
const size_t  end,
const size_t  idx,
const T &  delta 
)
inlineprivate

◆ values()

Member Data Documentation

◆ identity

template<typename T , class Op >
T Aleph::Gen_Segment_Tree< T, Op >::identity
private

◆ n

◆ op

◆ tree


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