18 #ifndef THRILL_COMMON_BINARY_HEAP_HEADER 19 #define THRILL_COMMON_BINARY_HEAP_HEADER 30 template <
typename Type,
typename Compare = std::less<Type> >
56 template <
typename... Args>
58 c_.emplace_back(std::forward<Args>(args) ...);
78 template <
typename Functor>
82 while (i !=
c_.size()) {
83 if (!std::forward<Functor>(f)(
c_[i])) {
102 template <
typename Iterator>
103 static void sift_up(Iterator first, Iterator last,
108 Iterator ptr = first + len;
109 if (comp(*ptr, *(--last)))
114 *last = std::move(*ptr);
120 }
while (comp(*ptr, t));
121 *last = std::move(t);
126 template <
typename Iterator>
127 static void push_heap(Iterator first, Iterator last,
const Compare& comp) {
128 sift_up(first, last, comp, last - first);
131 template <
typename Iterator>
133 Iterator first, Iterator ,
139 if (len < 2 || (len - 2) / 2 < child)
142 child = 2 * child + 1;
143 Iterator child_i = first + child;
145 if ((child + 1) < len && comp(*child_i, *(child_i + 1))) {
151 if (comp(*child_i, *start))
158 *start = std::move(*child_i);
161 if ((len - 2) / 2 < child)
165 child = 2 * child + 1;
166 child_i = first + child;
168 if ((child + 1) < len && comp(*child_i, *(child_i + 1))) {
173 }
while (!comp(*child_i, top));
174 *start = std::move(top);
177 template <
typename Iterator>
178 static void pop_heap(Iterator first, Iterator last,
183 sift_down(first, last, comp, len - 1, first);
187 template <
typename Iterator>
188 static void pop_heap(Iterator first, Iterator last,
const Compare& comp) {
189 pop_heap(first, last, comp, last - first);
205 #endif // !THRILL_COMMON_BINARY_HEAP_HEADER void emplace(Args &&... args)
add an items in the PQ.
typename Container::difference_type difference_type
std::vector< Timer > Container
static void sift_down(Iterator first, Iterator, const Compare &comp, difference_type len, Iterator start)
void pop()
remove the top item in the PQ
size_type size() const
return number of items in the PQ
Container c_
array holding binary heap
static void sift_up(Iterator first, Iterator last, const Compare &comp, difference_type len)
const_reference top() const
return reference to top item in PQ
static void push_heap(Iterator first, Iterator last, const Compare &comp)
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
static void pop_heap(Iterator first, Iterator last, const Compare &comp)
Container & container()
direct access to heap container
bool empty() const
check if PQ is empty
size_t erase(Functor &&f)
static void pop_heap(Iterator first, Iterator last, const Compare &comp, difference_type len)
const Timer & const_reference
BinaryHeap(const Compare &cmp=Compare())