46 FirstHasHigherPriority first_has_higher_priority_fn_;
53 : data_(
data), heap_to_orig_(data_.
size()), orig_to_heap_(data_.
size())
68 const int final_heap_size = data_.size();
69 if (final_heap_size > 1) {
70 for (
int64_t i = this->get_parent(final_heap_size - 1);
i >= 0;
i--) {
71 this->heapify(
i, final_heap_size);
74 heap_size_ = final_heap_size;
92 return heap_size_ == 0;
121 return heap_to_orig_[0];
130 const int64_t top_index_orig = heap_to_orig_[0];
132 if (heap_size_ > 1) {
133 this->swap_indices(0, heap_size_);
134 this->heapify(0, heap_size_);
136 return top_index_orig;
145 const int64_t heap_index = orig_to_heap_[index];
146 if (heap_index >= heap_size_) {
150 this->heapify(heap_index, heap_size_);
159 int64_t current = orig_to_heap_[index];
160 if (current >= heap_size_) {
168 const int64_t parent = this->get_parent(current);
169 if (this->first_has_higher_priority(parent, current)) {
172 this->swap_indices(current, parent);
193 return heap_to_orig_.as_span().
take_front(heap_size_);
203 return heap_to_orig_.as_span().
drop_front(heap_size_);
211 return heap_to_orig_;
220 return this->partial_to_dot(heap_size_);
226 const T &value_a = data_[heap_to_orig_[a]];
227 const T &value_b = data_[heap_to_orig_[
b]];
228 return first_has_higher_priority_fn_(value_a, value_b);
233 std::swap(heap_to_orig_[a], heap_to_orig_[
b]);
234 orig_to_heap_[heap_to_orig_[a]] = a;
235 orig_to_heap_[heap_to_orig_[
b]] =
b;
241 const int left = this->get_left(parent);
242 const int right = this->get_right(parent);
246 if (right < heap_size && this->first_has_higher_priority(right, max_index)) {
249 if (max_index != parent) {
250 this->swap_indices(parent, max_index);
251 this->heapify(max_index, heap_size);
253 if (
left < heap_size) {
256 if (right < heap_size) {
257 BLI_assert(!this->first_has_higher_priority(right, parent));
264 return (child - 1) / 2;
269 return parent * 2 + 1;
274 return parent * 2 + 2;
277 std::string partial_to_dot(
const int size)
const
279 dot_export::DirectedGraph digraph;
280 Array<dot_export::Node *> dot_nodes(
size);
281 for (
const int i : IndexRange(
size)) {
282 std::stringstream ss;
283 ss << data_[heap_to_orig_[
i]];
284 const std::string
name = ss.str();
288 dot_nodes[
i] = &node;
290 const int64_t parent = this->get_parent(
i);
291 digraph.
new_edge(*dot_nodes[parent], node);