Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binHeap.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
217# ifndef TPL_BINHEAP_H
218# define TPL_BINHEAP_H
219
220# include <type_traits>
221# include <utility>
222
223# include <ah-concepts.H>
224# include <htlist.H>
225# include <tpl_arrayStack.H>
226# include <tpl_binNode.H>
227# include <tpl_dynListQueue.H>
228
229using namespace Aleph;
230
231namespace Aleph
232{
234 {
235 struct Control_Fields //Defining Control Flags
236 {
237 int is_leaf: 4; // true If the node is leaf
238 int is_left: 4; // true if the node is a left child
239 };
240
241 BinHeapNode_Data *pLink; // Pointer to the parent
243
244 public:
245 BinHeapNode_Data() noexcept : pLink(nullptr)
246 {
247 control_fields.is_leaf = true;
248 control_fields.is_left = true;
249 }
250
251 BinHeapNode_Data *&getU() noexcept { return pLink; }
252
254
255 void reset() noexcept
256 {
257 control_fields.is_leaf = true;
258 control_fields.is_left = true;
259 }
260 };
261
263
264 // The previous and the next node of a node of the list of the nodes that
265 // lack a child: the links LLINK and RLINK of the node, under the names of
266 // their other use. See "Representation" in the description of this file
267# define PREV(p) (p->getL())
268# define NEXT(p) (p->getR())
269# define ULINK(p) reinterpret_cast<Node*&>((p)->getU())
270# define IS_LEAF(p) ((p)->get_control_fields().is_leaf)
271# define IS_LEFT(p) ((p)->get_control_fields().is_left)
272# define CTRL_BITS(p) ((p)->get_control_fields())
273
303 template <template <class> class NodeType, typename Key,
304 class Compare = Aleph::less<Key>>
305 requires StrictWeakOrder<Compare, Key>
307 {
308 protected:
309 Compare cmp;
310
311 // The operations that compare keys throw only if the comparison does,
312 // or the conversion of its result to bool: the one that a condition
313 // makes, which for a result of a class type may be another function
314 // than that of an implicit conversion
315 static constexpr bool nothrow_cmp =
316 noexcept(static_cast<bool>(std::declval<Compare &>()(std::declval<Key &>(),
317 std::declval<Key &>())));
318
319 public:
320 using Node = NodeType<Key>;
321
322 Compare &key_comp() noexcept { return cmp; }
323
324 Compare &get_compare() noexcept { return cmp; }
325
326 private:
337 size_t num_nodes;
338
339 public:
340 void swap(GenBinHeap & h) noexcept
341 {
342 std::swap(root, h.root);
343 std::swap(last, h.last);
344 std::swap(num_nodes, h.num_nodes);
345 std::swap(cmp, h.cmp);
346 adopt_nodes();
347 h.adopt_nodes();
348 }
349
350 private:
351 // After a swap, the root must point up to the header of its new heap,
352 // and an empty heap has its own header as its last node, as when it was
353 // built
354 void adopt_nodes() noexcept
355 {
356 if (root != nullptr)
357 ULINK(root) = head;
358 else
359 last = &head_node;
360 }
361
364 static bool is_in_list(Node *p) noexcept
365 {
366 if (IS_LEAF(p))
367 return true;
368
369 return ULINK(LLINK(p)) == RLINK(LLINK(p));
370 }
371
375 static bool has_sibling(Node *p) noexcept
376 {
377 return ULINK(p) != RLINK(p);
378 }
379
386 void swap_with_parent(Node *p) noexcept
387 {
388 assert(num_nodes >= 2);
389 assert(p != root);
390
391 Node *pp = ULINK(p); // the parent of p
392
393 const bool p_has_sibling = has_sibling(p);
394 const bool p_is_in_list = is_in_list(p); // does p lack a child?
395 const bool pp_is_in_list = is_in_list(pp); // is p the only child of pp? then p == last
396 const bool p_has_child = not IS_LEAF(p); // if p is in the list, a left child only
397
398 std::swap(CTRL_BITS(pp), CTRL_BITS(p));
399
400 if (pp == root)
401 root = p;
402
403 Node *ppp = ULINK(pp); // the parent of pp
404
405 ULINK(pp) = p;
406 ULINK(p) = ppp;
407
408 if (LLINK(ppp) == pp)
409 LLINK(ppp) = p;
410 else
411 RLINK(ppp) = p;
412
413 Node *sp = nullptr; // the sibling of p
414 if (p_has_sibling)
415 {
416 sp = p == LLINK(pp) ? RLINK(pp) : LLINK(pp);
417 assert(ULINK(sp) == pp);
418 ULINK(sp) = p;
419 }
420
421 if (p == last)
422 last = pp;
423
424 if (num_nodes == 2)
425 return;
426
427 // The children of p or, where p lacks one, its neighbours of the list
428 Node *lcp = LLINK(p);
429 Node *rcp = RLINK(p);
430
431 if (num_nodes == 3)
432 {
433 if (RLINK(pp) == p)
434 {
435 LLINK(lcp) = RLINK(lcp) = pp;
436 RLINK(pp) = lcp;
437 RLINK(p) = pp;
438 }
439 else
440 {
441 LLINK(rcp) = RLINK(rcp) = pp;
442 LLINK(pp) = rcp;
443 LLINK(p) = pp;
444 }
445
446 return;
447 }
448
449 if (not p_is_in_list) // lcp and rcp are the children of p
450 {
451 ULINK(lcp) = ULINK(rcp) = pp;
452
453 if (LLINK(pp) == p)
454 {
455 assert(RLINK(pp) == sp);
456 LLINK(p) = pp;
457 RLINK(p) = RLINK(pp);
458 }
459 else
460 {
461 assert(LLINK(pp) == sp);
462 RLINK(p) = pp;
463 LLINK(p) = LLINK(pp);
464 }
465
466 LLINK(pp) = lcp;
467 RLINK(pp) = rcp;
468
469 return;
470 }
471
472 if (not pp_is_in_list) // pp takes the place of p in the list
473 {
474 if (p_has_child)
475 ULINK(LLINK(p)) = pp;
476
477 RLINK(lcp) = LLINK(rcp) = pp;
478
479 if (LLINK(pp) == p)
480 {
481 assert(RLINK(pp) == sp);
482 LLINK(p) = pp;
483 RLINK(p) = RLINK(pp);
484 }
485 else
486 {
487 assert(LLINK(pp) == sp);
488 RLINK(p) = pp;
489 LLINK(p) = LLINK(pp);
490 }
491
492 LLINK(pp) = lcp;
493 RLINK(pp) = rcp;
494
495 return;
496 }
497
498 // p is the last node and pp the first one of the list, which follows
499 // p. They exchange those two places: pp ends as the only child of p,
500 // and so as its previous node too
501 RLINK(lcp) = pp;
502 LLINK(RLINK(pp)) = p;
503 LLINK(pp) = lcp;
504 RLINK(p) = RLINK(pp);
505 RLINK(pp) = p;
506 LLINK(p) = pp;
507 }
508
509 // The sifts exchange the node with its parent, or with a child, level by
510 // level, as they compare. If the comparison throws they leave the node
511 // where it was by then, and the operation that called them takes it
512 // back to its place: an exchange is undone by another one, which
513 // compares nothing and does not throw, so the heap is as it was before
514 // the exception goes on. That undoing is in functions of its own, cold
515 // and out of line, to keep it out of the way of the code that runs when
516 // nothing throws. A comparison that cannot throw leaves nothing to
517 // undo: the operations are compiled then without it (nothrow_cmp).
518
520 virtual void sift_up(Node *p) noexcept(nothrow_cmp)
521 {
522 while (p != root and cmp(KEY(p), KEY(ULINK(p))))
524 }
525
528 virtual void sift_down(Node *p) noexcept(nothrow_cmp)
529 {
530 while (not IS_LEAF(p))
531 {
532 Node *cp = LLINK(p); // the child of p with the least key
533 if (has_sibling(cp))
534 if (cmp(KEY(RLINK(p)), KEY(LLINK(p))))
535 cp = RLINK(p);
536
537 if (cmp(KEY(p), KEY(cp)))
538 return;
539
541 }
542 }
543
546 void back_up(Node *p, Node *pp) noexcept
547 {
548 while (ULINK(p) != pp)
550 }
551
556 void back_down(Node *p, Node *c) noexcept
557 {
558 if (Node *over = ULINK(c); over != p)
559 back_down(p, over); // first to the place over that of c
561 }
562
566 [[gnu::cold, gnu::noinline]] void back_under(Node *p, Node *pp) noexcept
567 {
568 Node *q = ULINK(p); // pp is over p if p is below its place, or at it
569 while (q != pp and q != head)
570 q = ULINK(q);
571 if (q == pp)
572 back_up(p, pp);
573 else
574 back_down(p, pp); // pp is at the place of p, which went up
575 }
576
579 [[gnu::cold, gnu::noinline]] void undo_insert(Node *p) noexcept
580 {
581 if (p != last)
582 back_down(p, last);
583 remove_last();
584 }
585
589 [[gnu::cold, gnu::noinline]] void undo_extraction(Node *moved, Node *old_root) noexcept
590 {
591 back_up(moved, head);
592 link_last(old_root);
594 }
595
598 [[gnu::cold, gnu::noinline]] void undo_removal(Node *node, Node *p) noexcept
599 {
600 replace_node(p, node);
601 p->reset();
602 link_last(p);
603 }
604
607 [[gnu::cold, gnu::noinline]] void link_last_two(Node *first, Node *second) noexcept
608 {
609 link_last(first);
610 link_last(second);
611 }
612
616 void swap_root_with_last() noexcept
617 {
618 assert(num_nodes > 1);
619 assert(ULINK(root) == head);
620 assert(not IS_LEAF(root));
621 assert(IS_LEAF(last));
622
623 if (num_nodes > 3) // general case
624 {
625 Node *lRoot = LLINK(root);
626 Node *rRoot = RLINK(root);
627 Node *f_last = ULINK(last);
628 Node *prev_last = LLINK(last);
629 Node *next_last = RLINK(last);
630
631 if (LLINK(f_last) == last)
632 LLINK(f_last) = root;
633 else
634 RLINK(f_last) = root;
635
636 // with more than 3 nodes the last one is not a child of the root
637 assert(f_last != root);
638 std::swap(ULINK(root), ULINK(last));
639
640 std::swap(LLINK(root), LLINK(last));
641 std::swap(RLINK(root), RLINK(last));
642
643 ULINK(lRoot) = ULINK(rRoot) = last;
644
645 LLINK(last) = lRoot;
646 RLINK(last) = rRoot;
647
648 PREV(root) = prev_last;
649 NEXT(root) = next_last;
650
651 NEXT(prev_last) = PREV(next_last) = root;
652 }
653 else if (num_nodes == 3) // special case with 3 nodes
654 {
655 assert(RLINK(root) == last);
656 assert(LLINK(last) == LLINK(root) and RLINK(last) == LLINK(root));
657
658 ULINK(last) = ULINK(root);
659 ULINK(root) = last;
660
661 Node *s_last = LLINK(last);
662 ULINK(s_last) = last;
663
664 LLINK(last) = s_last;
665 RLINK(last) = root;
666
667 LLINK(root) = RLINK(root) = s_last;
668 RLINK(s_last) = LLINK(s_last) = root;
669 }
670 else // special case with 2 nodes
671 {
672 assert(LLINK(root) == last);
673
674 ULINK(last) = ULINK(root);
675 ULINK(root) = last;
676 RLINK(last) = LLINK(last) = root;
677 RLINK(root) = LLINK(root) = last;
678 }
679
680 std::swap(CTRL_BITS(root), CTRL_BITS(last));
681 std::swap(root, last);
682 }
683
689 Node * remove_last() noexcept
690 {
691 assert(last != root and num_nodes > 0);
692 assert(IS_LEAF(last));
693
694 Node *ret_val = last;
695 Node *pp = ULINK(last);
696 Node *new_last = LLINK(last);
697
698 if (IS_LEFT(last))
699 { // pp is left without children: its links are both of the list
700 IS_LEAF(pp) = true;
701 LLINK(pp) = new_last;
702 }
703 else
704 { // pp is left with its left child: its right link is of the list
705 RLINK(pp) = RLINK(last);
706 LLINK(RLINK(last)) = pp;
707 }
708
709 RLINK(LLINK(last)) = pp; // the list is circular
710 last = new_last;
711 num_nodes--;
712 ret_val->reset();
713
714 return ret_val;
715 }
716
721 void replace_node(Node *node, Node *new_node) noexcept
722 {
723 assert(node != new_node);
724 assert(node != last);
725
726 // save node's immediate relatives
727 Node *parent = ULINK(node);
728 Node *left_child = LLINK(node);
729 Node *right_child = RLINK(node);
730
731 // update new_node's own pointers
732 ULINK(new_node) = parent;
733 LLINK(new_node) = left_child;
734 RLINK(new_node) = right_child;
735
736 // update parent
737 if (IS_LEFT(node))
738 {
739 assert(LLINK(parent) == node);
740 LLINK(parent) = new_node;
741 }
742 else
743 {
744 assert(RLINK(parent) == node);
745 RLINK(parent) = new_node;
746 }
747
748 // Update children
749 if (IS_LEAF(node))
750 { // they are not children, but the neighbours of node in the list
751 RLINK(left_child) = new_node;
752 LLINK(right_child) = new_node;
753 }
754 else
755 {
756 ULINK(left_child) = new_node;
757
758 if (ULINK(right_child) == node) // Node could have only one child
759 ULINK(right_child) = new_node;
760 else
761 {
762 assert(left_child == last);
763 RLINK(left_child) = new_node;
764 LLINK(right_child) = new_node;
765 }
766 }
767
768 CTRL_BITS(new_node) = CTRL_BITS(node);
769 }
770
774 static void __postorder_delete(Node *p, Node *incomplete_node) noexcept
775 {
776 if (IS_LEAF(p))
777 {
778 delete p;
779 return;
780 }
781
782 __postorder_delete(LLINK(p), incomplete_node);
783
784 if (p != incomplete_node)
785 __postorder_delete(RLINK(p), incomplete_node);
786
787 delete p;
788 }
789
790 public:
791 Node * getRoot() noexcept { return root; }
792
793 Node * getRoot() const noexcept { return const_cast<Node *>(root); }
794
795 private:
796 template <class Operation>
797 static
798 void __for_each_in_preorder(Node *p, Operation & operation)
799 {
800 if (p == nullptr)
801 return;
802
803 operation(p);
806 }
807
808 template <class Operation>
809 static
810 void __for_each_in_inorder(Node *p, Operation & operation)
811 {
812 if (p == nullptr)
813 return;
814
815 __for_each_in_inorder(advance_left(p), operation);
816 operation(p);
818 }
819
820 template <class Operation>
821 bool preorder_traverse(Node *p, Operation op) const
822 {
823 if (p == nullptr)
824 return true;
825 if (not op(p))
826 return false;
827 if (not preorder_traverse(advance_left(p), op))
828 return false;
829 return preorder_traverse(advance_right(p), op);
830 }
831
832 public:
833 template <class Operation>
834 bool preorder_traverse(Operation op) const
835 {
836 return preorder_traverse(getRoot(), op);
837 }
838
839 template <class Operation>
840 void for_each_in_preorder(Operation & operation) const
841 {
842 return __for_each_in_preorder<Operation>(getRoot(), operation);
843 }
844
845 template <class Operation>
846 void for_each_in_preorder(Operation && operation = Operation()) const
847 {
848 return for_each_in_preorder(operation);
849 }
850
851 template <class Operation>
852 void for_each_in_inorder(Operation & operation) const
853 {
854 return __for_each_in_inorder<Operation>(getRoot(), operation);
855 }
856
857 template <class Operation>
858 void for_each_in_inorder(Operation && operation = Operation()) const
859 {
860 return for_each_in_inorder(operation);
861 }
862
863 private:
864 template <class Op>
865 bool __level_traverse(Node *root, Op & operation) const
866 {
867 if (root == nullptr)
868 return true;
869
871 queue.put(root);
872
873 while (not queue.is_empty())
874 {
875 Node *p = queue.get();
876
877 if (not operation(p))
878 return false;
879
880 Node *c = advance_left(p);
881 if (c == nullptr)
882 continue;
883
884 queue.put(c);
885
886 c = advance_right(p);
887 if (c != nullptr)
888 queue.put(c);
889 }
890
891 return true;
892 }
893
894 public:
895 template <class Op>
896 bool level_traverse(Op operation = Op()) const
897 {
898 return __level_traverse(getRoot(), operation);
899 }
900
915 noexcept(std::is_nothrow_default_constructible_v<Node> and
916 std::is_nothrow_default_constructible_v<Compare> and
917 std::is_nothrow_constructible_v<Compare, Compare &>)
918 : GenBinHeap(Compare())
919 {
920 // empty
921 }
922
931 GenBinHeap(Compare __cmp)
932 noexcept(std::is_nothrow_default_constructible_v<Node> and
933 std::is_nothrow_constructible_v<Compare, Compare &>)
934 : cmp(__cmp), head(&head_node), root(RLINK(head)), last(&head_node),
935 num_nodes(0)
936 {
937 // empty
938 }
939
940 virtual ~GenBinHeap() noexcept
941 { /* empty */
942 }
943
944 private:
949 void link_last(Node *p) noexcept
950 {
951 Node *pp = RLINK(last); // the parent of p
952 LLINK(p) = last;
953 ULINK(p) = pp;
954
955 if (IS_LEFT(last))
956 { // p will be a right child: pp is the parent of the last node too,
957 // and with its second child it leaves the list
958 IS_LEFT(p) = false;
959 RLINK(p) = RLINK(pp);
960 LLINK(RLINK(pp)) = p;
961 RLINK(pp) = p;
962 }
963 else
964 { // p will be a left child: pp stays as the first node of the list,
965 // after p, which is its previous node as well as its child
966 IS_LEFT(p) = true;
967 RLINK(p) = pp;
968 IS_LEAF(pp) = false; // if p is a left child ==> pp was a leaf
969 LLINK(pp) = p;
970 }
971
972 assert(not IS_LEAF(pp));
973
974 RLINK(last) = p;
975 last = p;
976 num_nodes++;
977 }
978
979 public:
989 Node * insert(Node *p) noexcept(nothrow_cmp)
990 {
991 assert(IS_LEAF(p));
992
993 if (root == nullptr) // Is HEAP empty?
994 { // Yes, initialize
995
996 assert(num_nodes == 0);
997
998 root = p;
999 LLINK(p) = RLINK(p) = p;
1000 ULINK(p) = head;
1001 IS_LEAF(p) = true;
1002 IS_LEFT(p) = false; /* root is right child of header node */
1003 last = root;
1004 num_nodes = 1;
1005 return p;
1006 }
1007
1008 link_last(p);
1009 if constexpr (nothrow_cmp)
1010 sift_up(p);
1011 else
1012 try
1013 {
1014 sift_up(p);
1015 }
1016 catch (...)
1017 {
1018 undo_insert(p);
1019 throw;
1020 }
1021 return p;
1022 }
1023
1032 {
1033 Node *ret_val = root;
1034 if (num_nodes == 1)
1035 {
1036 root = nullptr;
1037 last = &head_node; // as in a heap just built
1038 ret_val->reset();
1039 num_nodes = 0;
1040
1041 return ret_val;
1042 }
1043
1045 remove_last();
1046 if constexpr (nothrow_cmp)
1047 sift_down(root);
1048 else
1049 {
1050 Node *moved = root; // the node that was the last one
1051 try
1052 {
1053 sift_down(moved);
1054 }
1055 catch (...)
1056 {
1057 undo_extraction(moved, ret_val);
1058 throw;
1059 }
1060 }
1061 ret_val->reset();
1062
1063 return ret_val;
1064 }
1065
1077 {
1078 ah_underflow_error_if(root == nullptr) << "Heap is empty";
1079 return getMin_ne();
1080 }
1081
1084 {
1085 return getMin();
1086 }
1087
1101 void update(Node *p) noexcept(nothrow_cmp)
1102 {
1103 if constexpr (nothrow_cmp)
1104 {
1105 sift_down(p);
1106 sift_up(p);
1107 }
1108 else
1109 {
1110 Node *pp = ULINK(p); // the node over the place of p
1111 try
1112 {
1113 sift_down(p);
1114 sift_up(p);
1115 }
1116 catch (...)
1117 {
1118 back_under(p, pp);
1119 throw;
1120 }
1121 }
1122 }
1123
1136 {
1137 ah_underflow_error_if(root == nullptr) << "Heap is empty";
1138
1139 if (node == root)
1140 return getMin_ne();
1141
1142 if (node == last)
1143 return remove_last();
1144
1145 Node *p = remove_last();
1146
1147 if (node == last)
1148 {
1149 remove_last();
1150 try
1151 {
1152 insert(p);
1153 }
1154 catch (...)
1155 { // insert() left p out: the two go back to the end
1156 link_last_two(node, p);
1157 throw;
1158 }
1159
1160 return node;
1161 }
1162 replace_node(node, p);
1163 node->reset(); // undo_removal() gives it the flags of its place again
1164 try
1165 {
1166 update(p);
1167 }
1168 catch (...)
1169 { // update() left p at the place of node
1170 undo_removal(node, p);
1171 throw;
1172 }
1173
1174 return node;
1175 }
1176
1180 {
1181 if (root == nullptr)
1182 return;
1183
1184 if (num_nodes <= 3)
1185 { // without comparing the keys, since the comparison may throw
1186 while (num_nodes > 1)
1187 delete remove_last();
1188 delete root;
1189 root = nullptr;
1190 last = &head_node;
1191 num_nodes = 0;
1192 return;
1193 }
1194
1195 if (IS_LEFT(last))
1197 else
1198 __postorder_delete(root, nullptr);
1199
1200 root = nullptr; // reset as if it were the constructor
1201 last = &head_node;
1202 num_nodes = 0;
1203 }
1204
1208 {
1209 ah_underflow_error_if(root == nullptr) << "Heap is empty";
1210
1211 return root;
1212 }
1213
1214 Node * top() const
1215 {
1216 ah_underflow_error_if(root == nullptr) << "Heap is empty";
1217
1218 return const_cast<Node *>(root);
1219 }
1220
1221 const size_t &size() const noexcept { return num_nodes; }
1222
1223 bool is_empty() const noexcept { return size() == 0; }
1224
1225 protected:
1230 static Node * advance_left(Node *p) noexcept
1231 {
1232 if (IS_LEAF(p))
1233 return nullptr;
1234
1235 return LLINK(p);
1236 }
1237
1241 static Node * advance_right(Node *p) noexcept
1242 {
1243 if (IS_LEAF(p))
1244 return nullptr;
1245
1246 if (not has_sibling(LLINK(p)))
1247 return nullptr;
1248
1249 return RLINK(p);
1250 }
1251
1254 virtual bool verify_heap(Node *p) const
1255 {
1256 Node *left_link = advance_left(p);
1257 if (left_link == nullptr)
1258 {
1259 assert(IS_LEAF(p));
1260 return true;
1261 }
1262
1263 if (cmp(KEY(left_link), KEY(p)))
1264 return false;
1265
1266 Node *right_link = advance_right(p);
1267 if (right_link == nullptr)
1268 return verify_heap(left_link);
1269
1270 if (cmp(KEY(right_link), KEY(p)))
1271 return false;
1272
1273 return verify_heap(left_link) and verify_heap(right_link);
1274 }
1275
1276 public:
1283 bool verify_heap() const
1284 {
1285 if (root == nullptr)
1286 return true;
1287
1288 return verify_heap(root);
1289 }
1290
1292 {
1293 static const size_t Stack_Size = 64;
1294
1297 Node *curr = nullptr;
1298 size_t pos = 0;
1299
1300 public:
1302 Iterator() noexcept {}
1303
1305 : heap_ptr(&const_cast<GenBinHeap &>(h))
1306 {
1307 if (h.is_empty())
1308 return;
1309 curr = h.root;
1310 }
1311
1312 void reset_first() noexcept
1313 {
1314 s.empty();
1315 if (heap_ptr->is_empty())
1316 curr = nullptr;
1317 else
1318 curr = heap_ptr->root;
1319 pos = 0;
1320 }
1321
1322 void reset_last() noexcept
1323 {
1324 s.empty();
1325 if (heap_ptr->is_empty())
1326 curr = nullptr;
1327 else
1328 {
1329 auto ptr = heap_ptr->root;
1330 curr = ptr;
1331 while (true)
1332 {
1333 ptr = advance_right(ptr);
1334 if (ptr == nullptr)
1335 break;
1336 curr = ptr;
1337 }
1338 }
1339 pos = heap_ptr->num_nodes - 1;
1340 }
1341
1342 bool has_curr() const noexcept { return curr != nullptr; }
1343
1344 Node * get_curr_ne() const noexcept { return curr; }
1345
1346 Node * get_curr() const
1347 {
1348 ah_overflow_error_if(not has_curr()) << "Iterator overflow";
1349 return get_curr_ne();
1350 }
1351
1352 void next_ne() noexcept
1353 {
1354 ++pos;
1355 auto l = advance_left(curr), r = advance_right(curr);
1356 if (l != nullptr)
1357 {
1358 curr = l;
1359 if (r != nullptr)
1360 s.push(r);
1361 return;
1362 }
1363
1364 if (r != nullptr)
1365 {
1366 curr = r;
1367 return;
1368 }
1369
1370 if (s.is_empty())
1371 curr = nullptr;
1372 else
1373 curr = s.pop();
1374 }
1375
1376 void next()
1377 {
1378 ah_overflow_error_if(not has_curr()) << "Iterator overflow";
1379 next_ne();
1380 }
1381
1382 size_t get_pos() const noexcept { return pos; }
1383
1384 void end() noexcept
1385 {
1386 s.empty();
1387 curr = nullptr;
1389 }
1390 };
1391 };
1392
1409 template <class Key, typename Compare = Aleph::less<Key>>
1410 requires StrictWeakOrder<Compare, Key>
1411 struct BinHeap : public GenBinHeap<BinHeapNode, Key, Compare>
1412 {
1415 using GenBinHeap<BinHeapNode, Key, Compare>::GenBinHeap;
1416 };
1417
1435 template <class Key, typename Compare = Aleph::less<Key>>
1437 struct BinHeapVtl : public GenBinHeap<BinHeapNodeVtl, Key, Compare>
1438 {
1441 using GenBinHeap<BinHeapNodeVtl, Key, Compare>::GenBinHeap;
1442 };
1443
1444# undef PREV
1445# undef NEXT
1446# undef ULINK
1447# undef IS_LEAF
1448# undef IS_LEFT
1449# undef CTRL_BITS
1450} // end namespace Aleph
1451# endif // TPL_BINHEAP_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#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
@ KEY
Definition btreepic.C:169
#define PREV(p)
Definition btreepic.C:399
long double h
Definition btreepic.C:154
BinHeapNode_Data *& getU() noexcept
BinHeapNode_Data * pLink
Control_Fields control_fields
Control_Fields & get_control_fields() noexcept
void reset() noexcept
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
bool is_empty() const noexcept
Return true if this is empty.
Iterator() noexcept
Default constructor creates an "end" iterator.
Node * get_curr_ne() const noexcept
bool has_curr() const noexcept
Iterator(const GenBinHeap &h)
static const size_t Stack_Size
size_t get_pos() const noexcept
InlineStack< Node *, Stack_Size > s
Generic heap of nodes.
bool preorder_traverse(Operation op) const
Node * getRoot() noexcept
bool preorder_traverse(Node *p, Operation op) const
Node * insert(Node *p) noexcept(nothrow_cmp)
Inserts a node into a heap.
Compare & get_compare() noexcept
virtual bool verify_heap(Node *p) const
Whether no node of the tree of root p has a key less than that of its parent.
bool is_empty() const noexcept
Node * last
The node at the place n, where the list of the nodes that lack a child ends; the header node in an em...
static Node * advance_left(Node *p) noexcept
The left child of p, or null if it has none.
Node *& root
The node at the place 1; null in an empty heap.
void undo_extraction(Node *moved, Node *old_root) noexcept
Undoes an extraction whose sift_down() threw: the node that took the place of the root goes back up t...
Node * top() const
void for_each_in_preorder(Operation &&operation=Operation()) const
void undo_removal(Node *node, Node *p) noexcept
Undoes a removal whose update(p) threw, which left p at the place of node: node goes back to it,...
void update(Node *p) noexcept(nothrow_cmp)
Updates the priority of a node contained in the heap.
static bool is_in_list(Node *p) noexcept
Whether p is in the list of the nodes that lack a child: it is a leaf, or its left child is its only ...
Node * getRoot() const noexcept
Node * head
The address of the header node.
void undo_insert(Node *p) noexcept
Undoes an insertion whose sift_up(p) threw: takes p back down to the last place, where it was linked,...
Node * remove(Node *node)
Removes node from the heap.
virtual void sift_down(Node *p) noexcept(nothrow_cmp)
Takes p down while its key is not less than that of its child with the least key.
Node * getMax()
See getMin(); to use when the priority order is reversed.
void for_each_in_inorder(Operation &operation) const
void back_up(Node *p, Node *pp) noexcept
Takes back up the node p, which went down from under the node pp, until it is under it again.
GenBinHeap(Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty heap with the comparison __cmp.
void swap_with_parent(Node *p) noexcept
Exchanges the node p, which is not the root, with its parent: each one takes the place of the other i...
void adopt_nodes() noexcept
virtual ~GenBinHeap() noexcept
bool verify_heap() const
Checks the order of the heap.
size_t num_nodes
The number n of nodes.
void back_under(Node *p, Node *pp) noexcept
Undoes what sift_down(p) and sift_up(p) did before the comparison threw: takes p, which is below or a...
static Node * advance_right(Node *p) noexcept
The right child of p, or null if it has none.
void remove_all_and_delete() noexcept
Deletes all the nodes of the heap, invokes the destructors of the removed nodes, and frees all the me...
bool level_traverse(Op operation=Op()) const
void replace_node(Node *node, Node *new_node) noexcept
Puts new_node, which is out of the heap, at the place of node, which is not the last one: in the tree...
static void __for_each_in_inorder(Node *p, Operation &operation)
Node * getMin_ne() noexcept(nothrow_cmp)
Removes the node with the lowest priority from a heap that is not empty.
virtual void sift_up(Node *p) noexcept(nothrow_cmp)
Takes p up while its key is less than that of its parent.
void swap(GenBinHeap &h) noexcept
void for_each_in_preorder(Operation &operation) const
void swap_root_with_last() noexcept
Exchanges the root with the last node: each one takes the place of the other in the tree,...
Compare & key_comp() noexcept
static void __for_each_in_preorder(Node *p, Operation &operation)
void link_last(Node *p) noexcept
Links the node p as the last one of a heap that is not empty: after the last node in the list of the ...
static void __postorder_delete(Node *p, Node *incomplete_node) noexcept
Deletes the nodes of the tree of root p.
void back_down(Node *p, Node *c) noexcept
Takes back down the node p, which went up, to the place of the node c, which is below it.
Node * top()
Returns the node with the lowest priority according to the comparison criterion specified in the decl...
void for_each_in_inorder(Operation &&operation=Operation()) const
Node * getMin()
Removes the node with the lowest priority from the heap.
static bool has_sibling(Node *p) noexcept
Whether the parent of p, which is not the root, has another child.
const size_t & size() const noexcept
void link_last_two(Node *first, Node *second) noexcept
Links again as the last nodes two nodes that were taken out: first, and then second.
bool __level_traverse(Node *root, Op &operation) const
NodeType< Key > Node
static constexpr bool nothrow_cmp
Node * remove_last() noexcept
Takes the last node, which is not the root, out of the tree and of the list of the nodes that lack a ...
Node head_node
The header node: the root is its right child.
GenBinHeap() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty heap with a comparison built by default.
Stack of fixed capacity whose storage is inside the object.
T pop() noexcept
Pop the top; the stack must not be empty.
T & push(const T &data) noexcept
Push a copy of data.
bool is_empty() const noexcept
Return true if the stack is empty.
void empty() noexcept
Empty the stack.
Strict weak ordering constraint for BST comparators.
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
Singly linked list implementations with head-tail access.
#define NEXT(p)
Definition htlist.H:58
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Heap of nodes with virtual destructor.
Node heap without virtual destructor.
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Stack implementations backed by dynamic or fixed arrays.
#define CTRL_BITS(p)
#define ULINK(p)
#define IS_LEAF(p)
#define IS_LEFT(p)
Basic binary tree node definitions.
Dynamic queue implementation based on linked lists.
DynList< int > l