Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_arrayHeap.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
47#ifndef TPL_ARRAYHEAP_H
48#define TPL_ARRAYHEAP_H
49
50#include <ah-concepts.H>
51#include <algorithm>
52#include <cstddef>
53#include <limits>
54#include <stdexcept>
55#include <utility>
56
57#include <ahFunction.H>
58#include <ahUtils.H>
59#include <ahDefs.H>
60#include <ahAssert.H>
61#include <array_it.H>
62#include <htlist.H>
63#include <tpl_dynDlist.H>
64#include <ah-args-ctor.H>
65#include <ahDry.H>
66#include <ah-dry.H>
67#include <ah-errors.H>
68#include <ah-iterator.H>
69
70#include <bit>
71#include <memory>
72#include <type_traits>
73#include <utility>
74
75namespace Aleph
76{
77 // The sifts exchange an item with its parent, or with a child, level by
78 // level, as they compare. An exchange is undone by another one, which
79 // compares nothing: if the comparison throws, they take the item back to
80 // its entry before the exception goes on, so that the range is as it was.
81 // A comparison that does not throw leaves nothing to undo, and the
82 // compiler drops that part.
83 namespace arrayheap_detail
84 {
93 template <typename T>
94 struct From_One
95 {
97
102 constexpr T & operator [] (const size_t i) const noexcept { return first[i - 1]; }
103 };
104
116 template <class A>
117 inline constexpr
118 void undo_sift_up(A ptr, size_t i, const size_t r)
119 {
120 for (auto levels = std::bit_width(r) - std::bit_width(i); levels != 0; )
121 {
122 const size_t c = r >> --levels; // the child of i on the way to r
123 std::swap(ptr[i], ptr[c]);
124 i = c;
125 }
126 }
127
136 template <class A>
137 inline constexpr
138 void undo_sift_down(A ptr, size_t i, const size_t l)
139 {
140 for (; i != l; i = u_index(i))
141 std::swap(ptr[i], ptr[u_index(i)]);
142 }
143
158 template <class A, class Compare>
159 inline constexpr
160 size_t sift_down(A ptr, const size_t l, const size_t r, Compare & cmp)
161 {
162 size_t i = l;
163 try
164 {
165 while (true)
166 {
167 size_t c = l_index(i); // left child index (c = 2 * i)
168 if (c > r) [[unlikely]] // does it have a left child?
169 return i; // no ==> stop
170
171 // Prefetch grandchildren for next iteration, in the program that runs
172 if (const size_t gc = l_index(c); gc <= r and not std::is_constant_evaluated())
173 __builtin_prefetch(std::addressof(ptr[gc]), 0, 1);
174
175 if (c + 1 <= r) // does it have a right child?
176 if (cmp(ptr[c + 1], ptr[c])) // yes ==> pick the smaller child
177 c++;
178
179 if (cmp(ptr[i], ptr[c])) [[likely]] // does the heap property hold?
180 return i; // yes ==> stop
181
182 std::swap(ptr[c], ptr[i]);
183 i = c;
184 }
185 }
186 catch (...)
187 {
188 undo_sift_down(ptr, i, l);
189 throw;
190 }
191 }
192
207 template <class A, class Compare>
208 inline constexpr
209 decltype(auto) sift_up(A ptr, const size_t l, const size_t r, Compare & cmp)
210 {
211 size_t i = r;
212 try
213 {
214 for (size_t p; i > l; i = p)
215 {
216 p = u_index(i); // parent index (p = i / 2)
217
218 // Prefetch grandparent for next iteration, in the program that runs
219 if (const size_t gp = u_index(p); gp >= l and not std::is_constant_evaluated())
220 __builtin_prefetch(std::addressof(ptr[gp]), 0, 1);
221
222 if (cmp(ptr[p], ptr[i])) [[likely]] // does the heap property hold?
223 return ptr[i]; // yes, the entire range is already a heap
224
225 std::swap(ptr[p], ptr[i]); // swap nodes and restore level p
226 }
227 }
228 catch (...)
229 {
230 undo_sift_up(ptr, i, r);
231 throw;
232 }
233
234 return ptr[i];
235 }
236 }
237
255 template <typename T, class Compare>
256 inline
257 T &sift_up(T *ptr, const size_t l, const size_t r, Compare & cmp)
258 {
259 return arrayheap_detail::sift_up(ptr, l, r, cmp);
260 }
261
278 template <typename T, class Compare>
279 inline
280 void sift_down(T *ptr, const size_t l, const size_t r, Compare & cmp)
281 {
283 }
284
300 template <typename T, class Compare>
301 inline
302 void sift_down_up(T *ptr, const size_t l, const size_t i, const size_t r,
303 Compare & cmp)
304 {
305 const size_t down = arrayheap_detail::sift_down(ptr, i, r, cmp);
306 try
307 {
308 sift_up<T, Compare>(ptr, l, i, cmp);
309 }
310 catch (...)
311 { // sift_up() left the range as sift_down() left it
313 throw;
314 }
315 }
316
332 template <typename T, class Compare = Aleph::less<T>>
333 requires StrictWeakOrder<Compare, T>
334 inline constexpr
335 void heapsort(T *array, const size_t n, const Compare & cmp = Compare())
336 {
337 if (n < 2)
338 return; // nothing to sort: array may be null, and is not moved
339
341
342 // the items from 1, with no pointer to an entry before the array
343 const arrayheap_detail::From_One<T> items{array};
344 for (size_t i = 2; i <= n; ++i)
345 arrayheap_detail::sift_up(items, 1, i, inv_cmp);
346 for (size_t i = n; i > 1; --i)
347 {
348 std::swap(items[1], items[i]); // place the i-th item at the root
349 arrayheap_detail::sift_down(items, 1, i - 1, inv_cmp);
350 }
351 }
352
368 template <typename T, class Compare = Aleph::less<T>>
369 requires StrictWeakOrder<Compare, T>
370 inline constexpr
371 void faster_heapsort(T *array, const size_t n, const Compare & cmp = Compare())
372 {
373 if (n < 2)
374 return; // nothing to sort: array may be null, and is not moved
375
377
378 // the items from 1, with no pointer to an entry before the array
379 const arrayheap_detail::From_One<T> items{array};
380 for (size_t i = n / 2; i >= 1; --i)
381 arrayheap_detail::sift_down(items, i, n, inv_cmp);
382 for (size_t i = n; i > 1; --i)
383 {
384 std::swap(items[1], items[i]); // place the i-th item at the root
385 arrayheap_detail::sift_down(items, 1, i - 1, inv_cmp);
386 }
387 }
388
400 template <typename T, class Compare = Aleph::less<T>>
401 requires StrictWeakOrder<Compare, T>
402 bool valid_heap(T *array, const size_t l, const size_t r,
403 const Compare & cmp = Compare())
404 {
405 size_t i;
406 for (i = l_index(l) /* i = 2*l */; i <= r; i++)
407 if (cmp(array[i], array[u_index(i)]))
408 break;
409 return i > r;
410 }
411
439 template <typename T, class Compare = Aleph::less<T>>
440 requires StrictWeakOrder<Compare, T>
441 class ArrayHeap : public LocateFunctions<ArrayHeap<T, Compare>, T>,
442 public FunctionalMethods<ArrayHeap<T, Compare>, T>,
443 public GenericItems<ArrayHeap<T, Compare>, T>,
444 public StlAlephIterator<ArrayHeap<T, Compare>>
445 {
446 T *array = nullptr;
447 mutable size_t dim = 0;
448 size_t num_items = 0;
449
450 mutable bool array_allocated = false;
451
452 Compare cmp;
453
454 // Whether neither the comparison of two items nor the conversion of its
455 // result to bool can throw: the conversion that a condition makes, which
456 // for a result of a class type may be another function than that of an
457 // implicit conversion
458 static constexpr bool nothrow_cmp =
459 noexcept(static_cast<bool>(std::declval<Compare &>()(std::declval<T &>(),
460 std::declval<T &>())));
461
462 static size_t r_index(const size_t & i)
463 {
464 return (i << 1) + 1; // multiply i by 2 and add 1
465 }
466
467 public:
478 void allocate_storage(const size_t new_dim)
479 {
480 ah_invalid_argument_if(new_dim == 0) << "Heap capacity must be positive";
481 // Before new[]: its count new_dim + 1 wraps for the maximum of size_t,
482 // and for a count whose bytes do not fit, what new[] asks of the
483 // allocator depends on the compiler
484 ah_overflow_error_if(new_dim >= std::numeric_limits<size_t>::max() / sizeof(T))
485 << "ArrayHeap: capacity " << new_dim << " and the entry 0 do not fit in a size_t";
486
487 T *ptr = new T[new_dim + 1];
488 if (array_allocated)
489 delete [] array;
490
491 array = ptr;
492 dim = new_dim;
493 num_items = 0;
494 array_allocated = true;
495 }
496
501 void swap(ArrayHeap & h) noexcept
502 {
503 std::swap(array, h.array);
504 std::swap(dim, h.dim);
505 std::swap(num_items, h.num_items);
506 std::swap(array_allocated, h.array_allocated);
507 std::swap(cmp, h.cmp);
508 }
509
510 using Item_Type = T;
511
512 using Key_Type = T;
513
515
525 ArrayHeap(const size_t d = 1024, Compare cmp_fct = Compare())
526 : array(nullptr), cmp(cmp_fct)
527 {
529 }
530
540 ArrayHeap(T *ptr, const size_t & d, Compare cmp_fct = Compare())
541 : array(ptr), dim(d), cmp(cmp_fct)
542 {
543 ah_invalid_argument_if(ptr == nullptr or d == 0) << "ArrayHeap requires non-null buffer";
544 }
545
547 : array(nullptr), cmp(h.cmp)
548 {
549 allocate_storage(h.dim);
550 num_items = h.num_items;
551 for (size_t i = 1; i <= num_items; ++i)
552 array[i] = h.array[i];
553 }
554
555 ArrayHeap(ArrayHeap && h) noexcept
556 : cmp(h.cmp)
557 {
558 swap(h);
559 }
560
562 {
563 if (this == &h)
564 return *this;
565
566 if (dim < h.dim)
567 allocate_storage(h.dim);
568
569 num_items = h.num_items;
570 for (size_t i = 1; i <= num_items; ++i)
571 array[i] = h.array[i];
572 cmp = h.cmp;
573
574 return *this;
575 }
576
578 noexcept
579 {
580 swap(h);
581 return *this;
582 }
583
585 virtual ~ArrayHeap()
586 {
587 if (array_allocated and array != nullptr)
588 delete [] array;
589 }
590
596 {
597 ah_underflow_error_if(num_items == 0) << "Heap is empty";
598
599 return array[1];
600 }
601
606 const T &top() const
607 {
608 ah_underflow_error_if(num_items == 0) << "Heap is empty";
609
610 return array[1];
611 }
612
622 T &insert_ne(const T & key)
623 {
624 // The element is counted once it is in its place: if the comparison
625 // throws, sift_up() takes it back to the entry after the last one,
626 // which is not of the heap
627 const size_t r = num_items + 1;
628 array[r] = key;
629 T & ret = sift_up(array, 1, r, cmp);
630 num_items = r;
631 return ret;
632 }
633
649 T &insert_ne(T && key)
650 {
651 const size_t r = num_items + 1;
652 T & entry = array[r]; // not of the heap until it is counted
653 entry = std::move(key);
654 try
655 {
656 T & ret = sift_up(array, 1, r, cmp);
657 num_items = r;
658 return ret;
659 }
660 catch (...)
661 { // sift_up() took the element back to its entry
662 key = std::move(entry);
663 throw;
664 }
665 }
666
675 T &insert(const T & key)
676 {
677 ah_overflow_error_if(num_items >= dim) << "Heap out of capacity";
678 return insert_ne(key);
679 }
680
688 T &insert(T && key)
689 {
690 ah_overflow_error_if(num_items >= dim) << "Heap out of capacity";
691 return insert_ne(std::move(key));
692 }
693
700 T &put(const T & key) { return insert(key); }
701
708 T &append(const T & key) { return insert(key); }
709
716 T &put(T && key) { return insert(std::move(key)); }
717
724 T &append(T && key) { return insert(std::move(key)); }
725
738 {
739 ah_underflow_error_if(num_items == 0) << "Heap is empty";
740
741 // The last item takes the place of the minimum and goes down from it.
742 // If the comparison throws, sift_down() takes it back to the root,
743 // and the two go back to their entries
744 const size_t n = num_items - 1; // the items that stay
745 T ret_val = std::move(array[1]);
746 if (n > 0)
747 {
748 T & last = array[n + 1];
749 array[1] = std::move(last);
750 try
751 {
752 sift_down(array, 1, n, cmp);
753 }
754 catch (...)
755 {
756 last = std::move(array[1]);
757 array[1] = std::move(ret_val);
758 throw;
759 }
760 }
761 num_items = n;
762 return ret_val;
763 }
764
767 {
768 return getMin();
769 }
770
773 {
774 return getMin();
775 }
776
778 [[nodiscard]] constexpr size_t size() const noexcept { return num_items; }
779
781 [[nodiscard]] constexpr bool is_empty() const noexcept { return num_items == 0; }
782
787 [[nodiscard]] constexpr size_t capacity() const noexcept { return dim; }
788
806 void update(T & data)
807 {
808 ah_underflow_error_if(num_items == 0) << "Heap is empty";
809
810 assert(std::addressof(data) >= array + 1 and std::addressof(data) <= array + num_items);
811
812 const auto i = static_cast<size_t>(std::addressof(data) - array);
814 }
815
834 void remove(T & item)
835 {
836 ah_underflow_error_if(num_items == 0) << "Heap is empty";
837
838 assert(std::addressof(item) >= array + 1 and std::addressof(item) <= array + num_items);
839
840 const auto idx = static_cast<size_t>(std::addressof(item) - array);
841 if (idx == num_items)
842 {
843 --num_items;
844 return;
845 }
846
847 const size_t n = num_items - 1; // the items that stay
848 T & last = array[n + 1];
849 if constexpr (nothrow_cmp)
850 { // nothing to undo: the last item takes the place of the removed one
851 item = std::move(last);
852 num_items = n;
853 sift_down_up(array, 1, idx, n, cmp);
854 }
855 else
856 {
857 // The removed item must outlive the comparisons: it exchanges its
858 // entry with the last one, which goes to its place from there. If
859 // the comparison throws, sift_down_up() takes it back there, and
860 // the two exchange their entries again
861 std::swap(item, last);
862 try
863 {
864 sift_down_up(array, 1, idx, n, cmp);
865 }
866 catch (...)
867 {
868 std::swap(item, last);
869 throw;
870 }
871 num_items = n;
872
873 // The item is out, and nothing may throw any more. Its entry is
874 // emptied by what cannot throw: it takes an empty item or, failing
875 // that, the item is moved out of it. With neither, the item stays
876 // there until another one takes the entry
877 if constexpr (std::is_nothrow_default_constructible_v<T> and
878 std::is_nothrow_move_assignable_v<T>)
879 last = T();
880 else if constexpr (std::is_nothrow_move_constructible_v<T>)
881 [[maybe_unused]] T taken(std::move(last));
882 }
883 }
884
892 T &operator [](const size_t i)
893 {
894 return array[i];
895 }
896
902 const T &operator [](const size_t i) const
903 {
904 return array[i];
905 }
906
907 struct Iterator : public Array_Iterator<T>
908 {
909 Iterator(const ArrayHeap & h) noexcept
910 : Array_Iterator<T>(no_exception_ctor, h.array + 1, h.dim, h.num_items)
911 { /* empty */
912 }
913
915 [[nodiscard]] const T & get_curr() const { return Array_Iterator<T>::get_curr(); }
916
918 [[nodiscard]] const T & get_curr_ne() const noexcept
919 {
921 }
922 };
923
926
927 private:
928 // superfast array traversal
929 template <class Operation>
930 bool traverse_impl(Operation & operation)
931 {
932 for (size_t i = 1; i <= num_items; ++i)
933 if (not static_cast<bool>(operation(array[i])))
934 return false;
935
936 return true;
937 }
938
939 // Constant traversal: each item is handed out as a constant reference
940 template <class Operation>
941 bool const_traverse_impl(Operation & operation) const
942 {
943 for (size_t i = 1; i <= num_items; ++i)
944 if (not static_cast<bool>(operation(static_cast<const T &>(array[i]))))
945 return false;
946
947 return true;
948 }
949
950 public:
960 template <class Operation>
962 bool traverse(Operation & operation) const
964 {
965 return const_traverse_impl(operation);
966 }
967
974 template <class Operation>
976 bool traverse(Operation & operation)
978 {
979 return traverse_impl(operation);
980 }
981
988 template <class Operation>
990 bool traverse(Operation && operation = Operation()) const
991 noexcept(Aleph::NothrowPredicateWith<Operation &, const T &>)
992 {
993 return const_traverse_impl(operation);
994 }
995
1002 template <class Operation>
1004 bool traverse(Operation && operation = Operation())
1005 noexcept(Aleph::NothrowPredicateWith<Operation &, T &>)
1006 {
1007 return traverse_impl(operation);
1008 }
1009 };
1010} // end namespace Aleph
1011# endif /* TPL_ARRAYHEAP_H */
Variadic constructor macros for containers.
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
STL-compatible iterator adapters for Aleph containers.
Debug assertion and warning utilities.
Core definitions, constants, and utility macros for Aleph-w.
@ no_exception_ctor
Definition ahDefs.H:74
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Standard functor implementations and comparison objects.
General utility functions and helpers.
Iterator wrapper for C++ raw arrays and circular buffers.
long double h
Definition btreepic.C:154
Fixed-capacity binary heap backed by a raw array.
T & insert_ne(T &&key)
Insert an element without checking capacity (move overload).
bool traverse(Operation &&operation=Operation()) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse all elements (const forwarding overload).
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse all elements (const overload).
T & put(const T &key)
Synonym for insert().
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all elements.
T & insert(const T &key)
Insert an element into the heap.
T & insert_ne(const T &key)
Insert an element without checking capacity.
ArrayHeap(ArrayHeap &&h) noexcept
T getMax()
See getMin().
T getMin()
Remove the smallest element in the heap and return a copy of its value.
const T & top() const
Return a const reference to the minimum element.
constexpr bool is_empty() const noexcept
Return true if the heap is empty.
T & append(const T &key)
Synonym for insert().
ArrayHeap(const ArrayHeap &h)
bool traverse_impl(Operation &operation)
void swap(ArrayHeap &h) noexcept
Swap all state with another heap.
T & append(T &&key)
Synonym for insert() (move overload).
void update(T &data)
Update the priority of an element stored in the heap.
bool const_traverse_impl(Operation &operation) const
ArrayHeap(const size_t d=1024, Compare cmp_fct=Compare())
Construct an empty heap with internal storage.
constexpr size_t capacity() const noexcept
Return the maximum number of elements that can be stored.
T get()
See getMin().
ArrayHeap(T *ptr, const size_t &d, Compare cmp_fct=Compare())
Construct an empty heap using an external buffer.
T & put(T &&key)
Synonym for insert() (move overload).
static size_t r_index(const size_t &i)
T & top()
Return a mutable reference to the minimum element.
virtual ~ArrayHeap()
Destructor.
void allocate_storage(const size_t new_dim)
Allocate internal storage and reset the heap.
constexpr size_t size() const noexcept
Return the number of elements currently stored.
void remove(T &item)
Remove an element from the heap given a reference to it.
static constexpr bool nothrow_cmp
T & operator[](const size_t i)
Return a mutable reference to the i-th entry (1-based).
bool traverse(Operation &&operation=Operation()) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all elements (forwarding overload).
T & insert(T &&key)
Insert an element into the heap (move overload).
ArrayHeap & operator=(const ArrayHeap &h)
Iterator wrapper for C++ raw arrays.
Definition array_it.H:85
T & get_curr() const
Get the current item with bounds checking.
Definition array_it.H:268
T & get_curr_ne() const noexcept
Get the current item without bounds checking.
Definition array_it.H:258
Read-only view of an Aleph iterator: the iterator of a constant container.
Performs order reversal of Compare by swapping operands.
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Singly linked list implementations with head-tail access.
constexpr void undo_sift_up(A ptr, size_t i, const size_t r)
Take back down an item that sift_up() took up.
constexpr size_t sift_down(A ptr, const size_t l, const size_t r, Compare &cmp)
Move the element at position l downwards to its place.
constexpr void undo_sift_down(A ptr, size_t i, const size_t l)
Take back up an item that sift_down() took down.
constexpr decltype(auto) sift_up(A ptr, const size_t l, const size_t r, Compare &cmp)
Move the element at position r upwards to its place.
Main namespace for Aleph-w library functions.
void sift_down_up(T *ptr, const size_t l, const size_t i, const size_t r, Compare &cmp)
Restore the heap property by sifting down and then sifting up.
void sift_down(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position l downwards.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
bool valid_heap(T *array, const size_t l, const size_t r, const Compare &cmp=Compare())
Check whether a range satisfies the heap property.
constexpr size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
Definition ahUtils.H:218
and
Check uniqueness with explicit hash + equality functors.
constexpr size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
Definition ahUtils.H:204
constexpr void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
constexpr void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
T & sift_up(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position r upwards.
const T & get_curr_ne() const noexcept
See get_curr().
Iterator(const ArrayHeap &h) noexcept
const T & get_curr() const
The current item, read-only: the heap order must not be broken.
The items of an array, numbered from 1.
constexpr T & operator[](const size_t i) const noexcept
The item of a number.
Generic list of items stored in a container.
Definition ah-dry.H:2896
Gsl_Compatible_Rng r
Dynamic doubly linked list implementation.
DynList< int > l