Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::SegmentTree< T, BinOp, Allocator > Class Template Reference

A standard segment tree that supports O(log n) for point update and range query. The binary operation must be commutative. More...

#include <flow_segment_tree.h>

Public Types

using value_type = T
using reference = T&
using const_reference = const T&
using pointer = T*
using const_pointer = const T*
using iterator = T*
using const_iterator = const T*
using difference_type = std::ptrdiff_t
using container_type = Vector<T, Allocator>
using allocator_type = container_type::allocator_type

Public Member Functions

constexpr SegmentTree () noexcept(noexcept(container_type()) &&noexcept(BinOp()))
constexpr SegmentTree (const allocator_type &allocator) noexcept(noexcept(container_type(allocator)) &&noexcept(BinOp()))
constexpr SegmentTree (const SegmentTree &)=default
constexpr SegmentTree (const SegmentTree &rhs, const allocator_type &allocator)
constexpr SegmentTree (SegmentTree &&)=default
constexpr SegmentTree (SegmentTree &&rhs, const allocator_type &allocator) noexcept(noexcept(container_type(std::move(rhs.data_), allocator)) &&noexcept(BinOp(std::move(rhs.binOp_))))
template<std::forward_iterator It>
constexpr SegmentTree (It first, It last, BinOp binOp={}, const allocator_type &allocator={})
 Iterator range constructor.
constexpr SegmentTree (std::initializer_list< T > list, BinOp binOp={}, const allocator_type &allocator={})
 Initializer list constructor.
 ~SegmentTree ()=default
allocator_type get_allocator () const noexcept
constexpr std::size_t size () const
 Return the number of input elements. This only counts the leaf node from the input.
constexpr bool empty () const
 Check if the segmeent tree is empty.
constexpr void setPoint (std::size_t i, const T &value)
 Set the new value at index i, then update the tree structure.
constexpr std::optional< T > getRange (std::size_t first, std::size_t last) const
 Query the value from [first, last) after applying the binary operator adjacent-pair wise. If the range is empty, return std::nullopt.

Private Member Functions

constexpr void buildParent ()

Static Private Member Functions

static constexpr bool hasParent (std::size_t i) noexcept
static constexpr std::size_t getParent (std::size_t i) noexcept
static constexpr std::size_t getLeftChild (std::size_t i) noexcept
static constexpr std::size_t getSibling (std::size_t i) noexcept
static constexpr bool isLeftChild (std::size_t i) noexcept
static constexpr bool isRightChild (std::size_t i) noexcept

Private Attributes

container_type data_
BinOp binOp_

Detailed Description

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
class flow::SegmentTree< T, BinOp, Allocator >

A standard segment tree that supports O(log n) for point update and range query. The binary operation must be commutative.

Template Parameters
T
Allocator
BinOp

Definition at line 14 of file flow_segment_tree.h.

Member Typedef Documentation

◆ allocator_type

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::allocator_type = container_type::allocator_type

Definition at line 25 of file flow_segment_tree.h.

◆ const_iterator

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::const_iterator = const T*

Definition at line 22 of file flow_segment_tree.h.

◆ const_pointer

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::const_pointer = const T*

Definition at line 20 of file flow_segment_tree.h.

◆ const_reference

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::const_reference = const T&

Definition at line 18 of file flow_segment_tree.h.

◆ container_type

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::container_type = Vector<T, Allocator>

Definition at line 24 of file flow_segment_tree.h.

◆ difference_type

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::difference_type = std::ptrdiff_t

Definition at line 23 of file flow_segment_tree.h.

◆ iterator

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::iterator = T*

Definition at line 21 of file flow_segment_tree.h.

◆ pointer

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::pointer = T*

Definition at line 19 of file flow_segment_tree.h.

◆ reference

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::reference = T&

Definition at line 17 of file flow_segment_tree.h.

◆ value_type

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
using flow::SegmentTree< T, BinOp, Allocator >::value_type = T

Definition at line 16 of file flow_segment_tree.h.

Constructor & Destructor Documentation

◆ SegmentTree() [1/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( )
inlineconstexprnoexcept

Definition at line 32 of file flow_segment_tree.h.

34 : data_(), binOp_() {
35 }
container_type data_

References binOp_, and data_.

Referenced by SegmentTree(), SegmentTree(), SegmentTree(), and SegmentTree().

◆ SegmentTree() [2/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( const allocator_type & allocator)
inlineexplicitconstexprnoexcept

Definition at line 37 of file flow_segment_tree.h.

40 }
A standard segment tree that supports O(log n) for point update and range query. The binary operation...

References binOp_, and data_.

◆ SegmentTree() [3/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( const SegmentTree< T, BinOp, Allocator > & )
constexprdefault

References SegmentTree().

◆ SegmentTree() [4/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( const SegmentTree< T, BinOp, Allocator > & rhs,
const allocator_type & allocator )
inlineconstexpr

Definition at line 43 of file flow_segment_tree.h.

References binOp_, data_, and SegmentTree().

◆ SegmentTree() [5/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( SegmentTree< T, BinOp, Allocator > && )
constexprdefault

References SegmentTree().

◆ SegmentTree() [6/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( SegmentTree< T, BinOp, Allocator > && rhs,
const allocator_type & allocator )
inlineconstexprnoexcept

Definition at line 48 of file flow_segment_tree.h.

References binOp_, data_, and SegmentTree().

◆ SegmentTree() [7/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
template<std::forward_iterator It>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( It first,
It last,
BinOp binOp = {},
const allocator_type & allocator = {} )
inlineexplicitconstexpr

Iterator range constructor.

Parameters
firstIterator to first element.
lastIterator to one-past-last element.
binOpBinary operator.
allocAllocator.

Definition at line 59 of file flow_segment_tree.h.

59 {}, const allocator_type& allocator = {})
61 std::copy(first, last, data_.begin() + size());
63 }
constexpr std::size_t size() const
Return the number of input elements. This only counts the leaf node from the input.
container_type::allocator_type allocator_type
constexpr void buildParent()

◆ SegmentTree() [8/8]

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::SegmentTree ( std::initializer_list< T > list,
BinOp binOp = {},
const allocator_type & allocator = {} )
inlineexplicitconstexpr

Initializer list constructor.

Parameters
listInitializer list.
binOpBinary operator.
allocAllocator.

Definition at line 69 of file flow_segment_tree.h.

69 {}, const allocator_type& allocator = {})
71 }
constexpr SegmentTree() noexcept(noexcept(container_type()) &&noexcept(BinOp()))

◆ ~SegmentTree()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
flow::SegmentTree< T, BinOp, Allocator >::~SegmentTree ( )
default

Member Function Documentation

◆ buildParent()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
void flow::SegmentTree< T, BinOp, Allocator >::buildParent ( )
inlineconstexprprivate

Definition at line 147 of file flow_segment_tree.h.

147 {
148 for (std::size_t i = size(); i-- > 1;) {
150 std::size_t right = left + 1;
152 }
153 }
static constexpr std::size_t getLeftChild(std::size_t i) noexcept

References binOp_, data_, getLeftChild(), and size().

◆ empty()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
bool flow::SegmentTree< T, BinOp, Allocator >::empty ( ) const
inlineconstexpr

Check if the segmeent tree is empty.

Returns
true if the segment tree is empty.

Definition at line 90 of file flow_segment_tree.h.

90 {
91 return data_.empty();
92 }

References data_.

◆ get_allocator()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
allocator_type flow::SegmentTree< T, BinOp, Allocator >::get_allocator ( ) const
inlinenoexcept

Definition at line 76 of file flow_segment_tree.h.

76 {
77 return data_.get_allocator();
78 }

References data_.

◆ getLeftChild()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
constexpr std::size_t flow::SegmentTree< T, BinOp, Allocator >::getLeftChild ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 164 of file flow_segment_tree.h.

164 {
165 return i * 2;
166 }

Referenced by buildParent().

◆ getParent()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
constexpr std::size_t flow::SegmentTree< T, BinOp, Allocator >::getParent ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 159 of file flow_segment_tree.h.

159 {
160 assert(hasParent(i) && "calling parent on the root node");
161 return i / 2;
162 }
static constexpr bool hasParent(std::size_t i) noexcept

References hasParent().

Referenced by getRange(), and setPoint().

◆ getRange()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
std::optional< T > flow::SegmentTree< T, BinOp, Allocator >::getRange ( std::size_t first,
std::size_t last ) const
inlineconstexpr

Query the value from [first, last) after applying the binary operator adjacent-pair wise. If the range is empty, return std::nullopt.

Parameters
firstThe begin index.
lastThe end index.
Returns
The range value or std::nullopt.

Definition at line 113 of file flow_segment_tree.h.

113 {
114 assert(first <= last && "invalid range");
117 last += elementSize;
118
120 for (; first < last;) {
121 if (isRightChild(first)) {
122 if (!result) {
123 result = data_[first];
124 } else {
126 }
127 ++first;
128 }
129
130 if (isRightChild(last)) {
131 --last;
132 if (!result) {
133 result = data_[last];
134 } else {
136 }
137 }
138
141 }
142
143 return result;
144 }
static constexpr bool isRightChild(std::size_t i) noexcept
static constexpr std::size_t getParent(std::size_t i) noexcept

References binOp_, data_, getParent(), isRightChild(), and size().

◆ getSibling()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
constexpr std::size_t flow::SegmentTree< T, BinOp, Allocator >::getSibling ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 168 of file flow_segment_tree.h.

168 {
169 return i ^ 1;
170 }

Referenced by setPoint().

◆ hasParent()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
constexpr bool flow::SegmentTree< T, BinOp, Allocator >::hasParent ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 155 of file flow_segment_tree.h.

155 {
156 return i > 1;
157 }

Referenced by getParent(), and setPoint().

◆ isLeftChild()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
constexpr bool flow::SegmentTree< T, BinOp, Allocator >::isLeftChild ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 172 of file flow_segment_tree.h.

172 {
173 return (i % 2) == 0;
174 }

Referenced by isRightChild().

◆ isRightChild()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
constexpr bool flow::SegmentTree< T, BinOp, Allocator >::isRightChild ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 176 of file flow_segment_tree.h.

176 {
177 return !isLeftChild(i);
178 }
static constexpr bool isLeftChild(std::size_t i) noexcept

References isLeftChild().

Referenced by getRange().

◆ setPoint()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
void flow::SegmentTree< T, BinOp, Allocator >::setPoint ( std::size_t i,
const T & value )
inlineconstexpr

Set the new value at index i, then update the tree structure.

Parameters
iThe index.
valueNew value.

Definition at line 97 of file flow_segment_tree.h.

97 {
98 i += size();
99 data_[i] = value;
100 while (hasParent(i)) {
104 i = parent;
105 }
106 }
static constexpr std::size_t getSibling(std::size_t i) noexcept

References binOp_, data_, getParent(), getSibling(), hasParent(), and size().

◆ size()

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
std::size_t flow::SegmentTree< T, BinOp, Allocator >::size ( ) const
inlineconstexpr

Return the number of input elements. This only counts the leaf node from the input.

Returns
The number of elements.

Definition at line 84 of file flow_segment_tree.h.

84 {
85 return data_.size() / 2;
86 }

References data_.

Referenced by buildParent(), getRange(), and setPoint().

Member Data Documentation

◆ binOp_

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
BinOp flow::SegmentTree< T, BinOp, Allocator >::binOp_
private

◆ data_

template<typename T, typename BinOp, typename Allocator = PolymorphicAllocator<T>>
container_type flow::SegmentTree< T, BinOp, Allocator >::data_
private

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