10template <
typename T,
typename Comp = std::less<T>,
int D = 8>
11struct DaryHeapNormalImpl {};
29template <
typename T,
typename Comp = std::less<T>,
int D = 8>
32 using size_type = std::size_t;
37 size_type
size()
const {
return arr_.size(); }
40 bool empty()
const {
return arr_.empty(); }
43 const T&
top() {
return arr_.front(); }
57 arr_.push_back(std::move(t));
63 template <
typename... Args>
65 arr_.emplace_back(std::forward<Args>(args)...);
76 arr_.front() = std::move(arr_.back());
86 size_type i = arr_.size() - 1;
87 while (i > 0 && comp_(arr_[i], arr_[(i - 1) / D])) {
88 std::swap(arr_[i], arr_[(i - 1) / D]);
95 while (i * D + 1 < arr_.size()) {
96 size_type min_child = i * D + 1;
97 for (size_type child = i * D + 2; child < std::min(i * D + (D + 1), arr_.size()); child++) {
98 if (comp_(arr_[child], arr_[min_child])) {
102 if (comp_(arr_[min_child], arr_[i])) {
103 std::swap(arr_[min_child], arr_[i]);
D-ary heap, slightly faster than binary heap due to less random memory access.
Definition: dary_heap.hpp:30
void pop()
Remove the top element from the heap.
Definition: dary_heap.hpp:75
void push(const T &t)
Insert an element into the heap.
Definition: dary_heap.hpp:50
bool empty() const
Returns whether the heap is empty.
Definition: dary_heap.hpp:40
size_type size() const
Returns the number of elements.
Definition: dary_heap.hpp:37
void push(T &&t)
Insert an element into the heap.
Definition: dary_heap.hpp:56
const T & top()
Returns the top element.
Definition: dary_heap.hpp:43
void emplace(Args &&... args)
Construct a new element in place in the heap.
Definition: dary_heap.hpp:64