Aleph-w 3.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 &&) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< Op >)=default
 
Gen_Segment_Treeoperator= (const Gen_Segment_Tree &)=default
 
Gen_Segment_Treeoperator= (Gen_Segment_Tree &&) noexcept(std::is_nothrow_move_assignable_v< Array< T > > and std::is_nothrow_move_assignable_v< Op >)=default
 
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< Tvalues () const
 Reconstruct all original values into an Array.
 
void swap (Gen_Segment_Tree &other) noexcept(noexcept(std::swap(std::declval< T & >(), std::declval< T & >())) and noexcept(std::swap(std::declval< Op & >(), std::declval< 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)
 

Private Attributes

Array< Ttree
 
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 148 of file tpl_segment_tree.H.

Member Typedef Documentation

◆ Item_Type

Definition at line 252 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 262 of file tpl_segment_tree.H.

References Aleph::divide_and_conquer_partition_dp(), 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
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.

Definition at line 283 of file tpl_segment_tree.H.

References Aleph::divide_and_conquer_partition_dp(), 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 301 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 316 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 331 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 > &&  ) const
defaultnoexcept

Member Function Documentation

◆ build()

◆ fill_and_build()

◆ fill_from_aleph_it()

◆ 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 406 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 418 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 > &&  )
defaultnoexcept

◆ 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 389 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 TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ query_impl()

◆ 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 372 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 TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ set_impl()

◆ 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 415 of file tpl_segment_tree.H.

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

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

◆ swap()

◆ 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 358 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()

◆ 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: