13 template <
typename T,
typename Compare = std::less<T>,
typename Allocator = PolymorphicAllocator<T>>
36 requires std::default_initializable<Compare>
43 noexcept(
noexcept(
container_type(allocator)) &&
noexcept(Compare()))
44 requires std::default_initializable<Compare>
52 noexcept(
noexcept(
container_type(allocator)) &&
noexcept(Compare(std::move(comparator))))
53 requires !std::default_initializable<Compare>
64 noexcept(
noexcept(
container_type(std::move(rhs.data_), allocator)) &&
noexcept(Compare(std::move(rhs.comparator_))))
73 template <std::input_iterator It>
84 :
BinaryHeap(list.begin(), list.end(), std::move(comparator), allocator) {
91 return data_.get_allocator();
98 constexpr std::size_t
size() const noexcept {
105 return data_.capacity();
110 constexpr bool empty() const noexcept {
111 return data_.empty();
116 const T&
top() const noexcept {
117 assert(!
data_.empty() &&
"call top on an empty heap");
118 return data_.front();
146 template <
typename ...Args>
148 data_.emplaceBack(std::forward<Args>(args)...);
149 T value = std::move(
data_.back());
155 noexcept(std::is_nothrow_move_assignable_v<T>) {
156 assert(!
data_.empty() &&
"call drop on an empty heap");
165 noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T>) {
166 assert(!
data_.empty() &&
"call pop on an empty heap");
167 T value = std::move(
data_.front());
176 noexcept(std::is_nothrow_move_assignable_v<T>) {
179 if (child >=
data_.size()) {
184 std::size_t rightChild = child + 1;
195 void fixUp(std::size_t index, std::size_t
top, T&& value)
196 noexcept(std::is_nothrow_move_assignable_v<T>) {
197 while (index >
top) {
206 data_[index] = std::move(value);
210 noexcept(std::is_nothrow_move_constructible_v<T>&& std::is_nothrow_move_assignable_v<T>) {
211 if (
data_.size() < 2) {
214 for (std::size_t i =
data_.size() / 2; i-- > 0 ;) {
215 T value = std::move(data_[i]);
216 std::size_t hole = fixDownWithHole(i);
217 fixUp(hole, i, std::move(value));
221 static constexpr bool hasParent(std::size_t i)
noexcept {
225 static constexpr std::size_t
getParent(std::size_t i)
noexcept {
226 assert(i > 0 &&
"calling parent on the root node");
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 bool empty() const noexcept
Return true if empty.
std::size_t fixDownWithHole(std::size_t index) noexcept(std::is_nothrow_move_assignable_v< T >)
constexpr BinaryHeap(BinaryHeap &&)=default
constexpr BinaryHeap(const BinaryHeap &rhs, const allocator_type &allocator)
constexpr std::size_t capacity() const noexcept
Return the heap capacity.
std::ptrdiff_t difference_type
Vector< T, Allocator > container_type
void heapify() noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
void push(T &&value)
Push the value to the heap.
const T & top() const noexcept
Return the min element.
void clear() noexcept
Clear the heap.
constexpr BinaryHeap(std::initializer_list< T > list, Compare comparator={}, const allocator_type &allocator={})
Initializer list range constructor.
constexpr BinaryHeap(BinaryHeap &&rhs, const allocator_type &allocator) noexcept(noexcept(container_type(std::move(rhs.data_), allocator)) &&noexcept(Compare(std::move(rhs.comparator_))))
const T & const_reference
allocator_type get_allocator() const noexcept
void reserve(std::size_t capacity)
Reserve the heap capacity.
static constexpr std::size_t getLeftChild(std::size_t i) noexcept
constexpr BinaryHeap(const BinaryHeap &)=default
constexpr BinaryHeap() noexcept(noexcept(container_type()) &&noexcept(Compare()))
Constructs an empty heap with default comparator and allocator. Comparator must be default initializa...
static constexpr std::size_t getParent(std::size_t i) noexcept
constexpr BinaryHeap(It first, It last, Compare comparator={}, const allocator_type &allocator={})
Iterator range constructor.
constexpr std::size_t size() const noexcept
Return the heap size.
T pop() noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
Pop the min element.
void push(const T &value)
Push the value to the heap.
void emplace(Args &&... args)
Construct the value inplace in the heap.
container_type::allocator_type allocator_type
static constexpr bool hasParent(std::size_t i) noexcept
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.
void drop() noexcept(std::is_nothrow_move_assignable_v< T >)
Drop the min element.
void fixUp(std::size_t index, std::size_t top, T &&value) noexcept(std::is_nothrow_move_assignable_v< T >)