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

A binary min-heap container. Supports custom comparator and allocator. More...

#include <flow_binary_heap.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 allocator_type = std::allocator_traits<Allocator>::template rebind_alloc<T>

Public Member Functions

constexpr BinaryHeap () noexcept(noexcept(container_type()) &&noexcept(Compare()))
 Constructs an empty heap with default comparator and allocator. Comparator must be default initializable.
constexpr BinaryHeap (const allocator_type &allocator) noexcept(noexcept(container_type(allocator)) &&noexcept(Compare()))
 Constructs an empty heap with a specific allocator. Comparator must be default initializable.
constexpr BinaryHeap (Compare comparator, const allocator_type &allocator={}) noexcept(noexcept(container_type(allocator)) &&noexcept(Compare(std::move(comparator))))
 Constructs an empty heap with a specific allocator.
constexpr BinaryHeap (const BinaryHeap &)=default
constexpr BinaryHeap (const BinaryHeap &rhs, const allocator_type &allocator)
constexpr BinaryHeap (BinaryHeap &&)=default
constexpr BinaryHeap (BinaryHeap &&rhs, const allocator_type &allocator) noexcept(noexcept(container_type(std::move(rhs.data_), allocator)) &&noexcept(Compare(std::move(rhs.comparator_))))
template<std::input_iterator It>
constexpr BinaryHeap (It first, It last, const allocator_type &allocator={})
 Iterator range constructor. Comparator must be default initializable.
template<std::input_iterator It>
constexpr BinaryHeap (It first, It last, Compare comparator, const allocator_type &allocator={})
 Iterator range constructor.
 ~BinaryHeap ()=default
allocator_type get_allocator () const noexcept
constexpr std::size_t size () const noexcept
 Return the heap size.
constexpr std::size_t capacity () const noexcept
 Return the heap capacity.
constexpr bool empty () const noexcept
 Return true if empty.
const T & top () const noexcept
 Return the min element.
void clear () noexcept
 Clear the heap.
void reserve (std::size_t capacity)
 Reserve the heap capacity.
void push (const T &value)
 Push the value to the heap.
void push (T &&value)
 Push the value to the heap.
template<typename ... Args>
void emplace (Args &&... args)
 Construct the value inplace in the heap.
void drop () noexcept(std::is_nothrow_move_assignable_v< T >)
 Drop the min element.
pop () noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
 Pop the min element.

Private Types

using container_type = Vector<T, Allocator>

Private Member Functions

std::size_t fixDownWithHole (std::size_t index) noexcept(std::is_nothrow_move_assignable_v< T >)
void fixUp (std::size_t index, std::size_t top, T &&value) noexcept(std::is_nothrow_move_assignable_v< T >)
void heapify () noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)

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

Private Attributes

container_type data_
Compare comparator_

Detailed Description

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
class flow::BinaryHeap< T, Compare, Allocator >

A binary min-heap container. Supports custom comparator and allocator.

Definition at line 14 of file flow_binary_heap.h.

Member Typedef Documentation

◆ allocator_type

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::allocator_type = std::allocator_traits<Allocator>::template rebind_alloc<T>

Definition at line 24 of file flow_binary_heap.h.

◆ const_iterator

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::const_iterator = const T*

Definition at line 22 of file flow_binary_heap.h.

◆ const_pointer

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::const_pointer = const T*

Definition at line 20 of file flow_binary_heap.h.

◆ const_reference

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::const_reference = const T&

Definition at line 18 of file flow_binary_heap.h.

◆ container_type

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::container_type = Vector<T, Allocator>
private

Definition at line 27 of file flow_binary_heap.h.

◆ difference_type

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::difference_type = std::ptrdiff_t

Definition at line 23 of file flow_binary_heap.h.

◆ iterator

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::iterator = T*

Definition at line 21 of file flow_binary_heap.h.

◆ pointer

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::pointer = T*

Definition at line 19 of file flow_binary_heap.h.

◆ reference

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::reference = T&

Definition at line 17 of file flow_binary_heap.h.

◆ value_type

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
using flow::BinaryHeap< T, Compare, Allocator >::value_type = T

Definition at line 16 of file flow_binary_heap.h.

Constructor & Destructor Documentation

◆ BinaryHeap() [1/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( )
inlineconstexprnoexcept

Constructs an empty heap with default comparator and allocator. Comparator must be default initializable.

Definition at line 34 of file flow_binary_heap.h.

37 : data_(), comparator_() {
38 }
container_type data_

References comparator_, and data_.

Referenced by BinaryHeap(), BinaryHeap(), BinaryHeap(), and BinaryHeap().

◆ BinaryHeap() [2/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( const allocator_type & allocator)
inlineexplicitconstexprnoexcept

Constructs an empty heap with a specific allocator. Comparator must be default initializable.

Definition at line 42 of file flow_binary_heap.h.

46 }
A binary min-heap container. Supports custom comparator and allocator.

References comparator_, and data_.

◆ BinaryHeap() [3/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( Compare comparator,
const allocator_type & allocator = {} )
inlineexplicitconstexprnoexcept

Constructs an empty heap with a specific allocator.

Parameters
comparatorComparator.
allocatorMemory allocator.

Definition at line 51 of file flow_binary_heap.h.

51 {})
52 noexcept(noexcept(container_type(allocator)) && noexcept(Compare(std::move(comparator))))
55 }
Vector< T, Allocator > container_type

◆ BinaryHeap() [4/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( const BinaryHeap< T, Compare, Allocator > & )
constexprdefault

References BinaryHeap().

◆ BinaryHeap() [5/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( const BinaryHeap< T, Compare, Allocator > & rhs,
const allocator_type & allocator )
inlineconstexpr

Definition at line 58 of file flow_binary_heap.h.

References BinaryHeap(), comparator_, and data_.

◆ BinaryHeap() [6/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( BinaryHeap< T, Compare, Allocator > && )
constexprdefault

References BinaryHeap().

◆ BinaryHeap() [7/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( BinaryHeap< T, Compare, Allocator > && rhs,
const allocator_type & allocator )
inlineconstexprnoexcept

Definition at line 63 of file flow_binary_heap.h.

References BinaryHeap(), comparator_, and data_.

◆ BinaryHeap() [8/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
template<std::input_iterator It>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( It first,
It last,
const allocator_type & allocator = {} )
inlineconstexpr

Iterator range constructor. Comparator must be default initializable.

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

Definition at line 74 of file flow_binary_heap.h.

74 {})
76 heapify();
77 }
void heapify() noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)

◆ BinaryHeap() [9/9]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
template<std::input_iterator It>
flow::BinaryHeap< T, Compare, Allocator >::BinaryHeap ( It first,
It last,
Compare comparator,
const allocator_type & allocator = {} )
inlineconstexpr

Iterator range constructor.

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

Definition at line 85 of file flow_binary_heap.h.

85 {})
87 heapify();
88 }

◆ ~BinaryHeap()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
flow::BinaryHeap< T, Compare, Allocator >::~BinaryHeap ( )
default

Member Function Documentation

◆ capacity()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
std::size_t flow::BinaryHeap< T, Compare, Allocator >::capacity ( ) const
inlineconstexprnoexcept

Return the heap capacity.

Returns
The heap capacity.

Definition at line 107 of file flow_binary_heap.h.

107 {
108 return data_.capacity();
109 }

References data_.

Referenced by reserve().

◆ clear()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::clear ( )
inlinenoexcept

Clear the heap.

Definition at line 125 of file flow_binary_heap.h.

125 {
126 data_.clear();
127 }

References data_.

◆ drop()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::drop ( )
inlinenoexcept

Drop the min element.

Definition at line 157 of file flow_binary_heap.h.

158 {
159 assert(!data_.empty() && "call drop on an empty heap");
161 fixUp(index, 0, std::move(data_.back()));
162 data_.popBack();
163 }
std::size_t fixDownWithHole(std::size_t index) noexcept(std::is_nothrow_move_assignable_v< T >)
void fixUp(std::size_t index, std::size_t top, T &&value) noexcept(std::is_nothrow_move_assignable_v< T >)

References data_, fixDownWithHole(), and fixUp().

Referenced by pop().

◆ emplace()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
template<typename ... Args>
void flow::BinaryHeap< T, Compare, Allocator >::emplace ( Args &&... args)
inline

Construct the value inplace in the heap.

Parameters
...argsConstructor arguments.

Definition at line 150 of file flow_binary_heap.h.

150 {
151 data_.emplaceBack(std::forward<Args>(args)...);
152 T value = std::move(data_.back());
153 fixUp(data_.size() - 1, 0, std::move(value));
154 }

References data_, and fixUp().

Referenced by push(), and push().

◆ empty()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
bool flow::BinaryHeap< T, Compare, Allocator >::empty ( ) const
inlineconstexprnoexcept

Return true if empty.

Returns
true if empty, otherwise false.

Definition at line 113 of file flow_binary_heap.h.

113 {
114 return data_.empty();
115 }

References data_.

◆ fixDownWithHole()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
std::size_t flow::BinaryHeap< T, Compare, Allocator >::fixDownWithHole ( std::size_t index)
inlineprivatenoexcept

Definition at line 178 of file flow_binary_heap.h.

179 {
180 for (;;) {
181 size_t child = getLeftChild(index);
182 if (child >= data_.size()) {
183 break;
184 }
185
186 // Select the less child.
188 if (rightChild < data_.size() && comparator_(data_[rightChild], data_[child])) {
190 }
192 index = child;
193 }
194 return index;
195 }
static constexpr std::size_t getLeftChild(std::size_t i) noexcept

References comparator_, data_, and getLeftChild().

Referenced by drop().

◆ fixUp()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::fixUp ( std::size_t index,
std::size_t top,
T && value )
inlineprivatenoexcept

Definition at line 198 of file flow_binary_heap.h.

199 {
200 while (index > top) {
202 if (comparator_(value, data_[parent])) {
204 index = parent;
205 } else {
206 break;
207 }
208 }
210 }
const T & top() const noexcept
Return the min element.
static constexpr std::size_t getParent(std::size_t i) noexcept

References comparator_, data_, getParent(), and top().

Referenced by drop(), and emplace().

◆ get_allocator()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
allocator_type flow::BinaryHeap< T, Compare, Allocator >::get_allocator ( ) const
inlinenoexcept

Definition at line 93 of file flow_binary_heap.h.

93 {
94 return data_.get_allocator();
95 }

References data_.

◆ getLeftChild()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
constexpr std::size_t flow::BinaryHeap< T, Compare, Allocator >::getLeftChild ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 233 of file flow_binary_heap.h.

233 {
234 return i * 2 + 1;
235 }

Referenced by fixDownWithHole().

◆ getParent()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
constexpr std::size_t flow::BinaryHeap< T, Compare, Allocator >::getParent ( std::size_t i)
inlinestaticconstexprprivatenoexcept

Definition at line 228 of file flow_binary_heap.h.

228 {
229 assert(i > 0 && "calling parent on the root node");
230 return (i - 1) / 2;
231 }

Referenced by fixUp().

◆ hasParent()

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

Definition at line 224 of file flow_binary_heap.h.

224 {
225 return i > 0;
226 }

◆ heapify()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::heapify ( )
inlineprivatenoexcept

Definition at line 212 of file flow_binary_heap.h.

213 {
214 if (data_.size() < 2) {
215 return;
216 }
217 for (std::size_t i = data_.size() / 2; i-- > 0 ;) {
218 T value = std::move(data_[i]);
221 }
222 }

References data_.

◆ pop()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
T flow::BinaryHeap< T, Compare, Allocator >::pop ( )
inlinenoexcept

Pop the min element.

Returns
The min element.

Definition at line 167 of file flow_binary_heap.h.

168 {
169 assert(!data_.empty() && "call pop on an empty heap");
170 T value = std::move(data_.front());
171 drop();
172 return value;
173 }
void drop() noexcept(std::is_nothrow_move_assignable_v< T >)
Drop the min element.

References data_, and drop().

◆ push() [1/2]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::push ( const T & value)
inline

Push the value to the heap.

Parameters
value

Definition at line 137 of file flow_binary_heap.h.

137 {
138 emplace(value);
139 }
void emplace(Args &&... args)
Construct the value inplace in the heap.

References emplace().

◆ push() [2/2]

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::push ( T && value)
inline

Push the value to the heap.

Parameters
value

Definition at line 143 of file flow_binary_heap.h.

143 {
145 }

References emplace().

◆ reserve()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
void flow::BinaryHeap< T, Compare, Allocator >::reserve ( std::size_t capacity)
inline

Reserve the heap capacity.

Parameters
capacity

Definition at line 131 of file flow_binary_heap.h.

131 {
132 data_.reserve(capacity);
133 }
constexpr std::size_t capacity() const noexcept
Return the heap capacity.

References capacity(), and data_.

◆ size()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
std::size_t flow::BinaryHeap< T, Compare, Allocator >::size ( ) const
inlineconstexprnoexcept

Return the heap size.

Returns
The heap size.

Definition at line 101 of file flow_binary_heap.h.

101 {
102 return data_.size();
103 }

References data_.

◆ top()

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
const T & flow::BinaryHeap< T, Compare, Allocator >::top ( ) const
inlinenoexcept

Return the min element.

Returns
The min element.

Definition at line 119 of file flow_binary_heap.h.

119 {
120 assert(!data_.empty() && "call top on an empty heap");
121 return data_.front();
122 }

References data_.

Referenced by fixUp().

Member Data Documentation

◆ comparator_

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
Compare flow::BinaryHeap< T, Compare, Allocator >::comparator_
private

◆ data_

template<typename T, typename Compare = std::less<T>, typename Allocator = PolymorphicAllocator<>>
container_type flow::BinaryHeap< T, Compare, Allocator >::data_
private

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