42 FirstHasHigherPriority first_has_higher_priority_fn_;
49 : data_(data), heap_to_orig_(data_.size()), orig_to_heap_(data_.size())
64 const int final_heap_size = data_.
size();
65 if (final_heap_size > 1) {
66 for (
int64_t i = this->get_parent(final_heap_size - 1); i >= 0; i--) {
67 this->heapify(i, final_heap_size);
70 heap_size_ = final_heap_size;
88 return heap_size_ == 0;
117 return heap_to_orig_[0];
126 const int64_t top_index_orig = heap_to_orig_[0];
128 if (heap_size_ > 1) {
129 this->swap_indices(0, heap_size_);
130 this->heapify(0, heap_size_);
132 return top_index_orig;
141 const int64_t heap_index = orig_to_heap_[index];
142 if (heap_index >= heap_size_) {
146 this->heapify(heap_index, heap_size_);
155 int64_t current = orig_to_heap_[index];
156 if (current >= heap_size_) {
164 const int64_t parent = this->get_parent(current);
165 if (this->first_has_higher_priority(parent, current)) {
168 this->swap_indices(current, parent);
189 return heap_to_orig_.
as_span().take_front(heap_size_);
199 return heap_to_orig_.
as_span().drop_front(heap_size_);
207 return heap_to_orig_;
216 return this->partial_to_dot(heap_size_);
222 const T &value_a = data_[heap_to_orig_[a]];
223 const T &value_b = data_[heap_to_orig_[
b]];
224 return first_has_higher_priority_fn_(value_a, value_b);
229 std::swap(heap_to_orig_[a], heap_to_orig_[
b]);
230 orig_to_heap_[heap_to_orig_[a]] = a;
231 orig_to_heap_[heap_to_orig_[
b]] =
b;
237 const int left = this->get_left(parent);
238 const int right = this->get_right(parent);
242 if (right < heap_size && this->first_has_higher_priority(right, max_index)) {
245 if (max_index != parent) {
246 this->swap_indices(parent, max_index);
247 this->heapify(max_index, heap_size);
249 if (left < heap_size) {
250 BLI_assert(!this->first_has_higher_priority(left, parent));
252 if (right < heap_size) {
253 BLI_assert(!this->first_has_higher_priority(right, parent));
260 return (child - 1) / 2;
265 return parent * 2 + 1;
270 return parent * 2 + 2;
273 std::string partial_to_dot(
const int size)
const
276 Array<dot::Node *> dot_nodes(size);
277 for (
const int i : IndexRange(size)) {
278 std::stringstream ss;
279 ss << data_[heap_to_orig_[i]];
280 const std::string name = ss.str();
282 node.
set_shape(dot::Attr_shape::Rectangle);
283 node.attributes.set(
"ordering",
"out");
284 dot_nodes[i] = &
node;
286 const int64_t parent = this->get_parent(i);
287 digraph.
new_edge(*dot_nodes[parent], node);