217# ifndef TPL_BINHEAP_H
218# define TPL_BINHEAP_H
220# include <type_traits>
229using namespace Aleph;
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())
303 template <
template <
class>
class NodeType,
typename Key,
305 requires StrictWeakOrder<Compare, Key>
316 noexcept(
static_cast<bool>(std::declval<Compare &>()(std::declval<Key &>(),
317 std::declval<Key &>())));
342 std::swap(
root,
h.root);
343 std::swap(
last,
h.last);
345 std::swap(
cmp,
h.cmp);
396 const bool p_has_child = not
IS_LEAF(p);
408 if (
LLINK(ppp) == pp)
417 assert(
ULINK(sp) == pp);
449 if (not p_is_in_list)
455 assert(
RLINK(pp) == sp);
461 assert(
LLINK(pp) == sp);
472 if (not pp_is_in_list)
481 assert(
RLINK(pp) == sp);
487 assert(
LLINK(pp) == sp);
548 while (
ULINK(p) != pp)
637 assert(f_last !=
root);
701 LLINK(pp) = new_last;
723 assert(node != new_node);
724 assert(node !=
last);
732 ULINK(new_node) = parent;
733 LLINK(new_node) = left_child;
734 RLINK(new_node) = right_child;
739 assert(
LLINK(parent) == node);
740 LLINK(parent) = new_node;
744 assert(
RLINK(parent) == node);
745 RLINK(parent) = new_node;
751 RLINK(left_child) = new_node;
752 LLINK(right_child) = new_node;
756 ULINK(left_child) = new_node;
758 if (
ULINK(right_child) == node)
759 ULINK(right_child) = new_node;
762 assert(left_child ==
last);
763 RLINK(left_child) = new_node;
764 LLINK(right_child) = new_node;
784 if (p != incomplete_node)
796 template <
class Operation>
808 template <
class Operation>
820 template <
class Operation>
833 template <
class Operation>
839 template <
class Operation>
842 return __for_each_in_preorder<Operation>(
getRoot(), operation);
845 template <
class Operation>
851 template <
class Operation>
854 return __for_each_in_inorder<Operation>(
getRoot(), operation);
857 template <
class Operation>
877 if (not operation(p))
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 &>)
932 noexcept(std::is_nothrow_default_constructible_v<Node>
and
933 std::is_nothrow_constructible_v<Compare, Compare &>)
1181 if (
root ==
nullptr)
1257 if (left_link ==
nullptr)
1267 if (right_link ==
nullptr)
1285 if (
root ==
nullptr)
1409 template <
class Key,
typename Compare = Aleph::less<Key>>
1410 requires StrictWeakOrder<Compare, Key>
1435 template <
class Key,
typename Compare = Aleph::less<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
BinHeapNode_Data() noexcept
BinHeapNode_Data *& getU() noexcept
Control_Fields control_fields
Control_Fields & get_control_fields() 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
void reset_first() noexcept
Iterator(const GenBinHeap &h)
static const size_t Stack_Size
void reset_last() noexcept
size_t get_pos() const noexcept
InlineStack< Node *, Stack_Size > s
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...
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
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.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Heap of nodes with virtual destructor.
Node heap without virtual destructor.
Stack implementations backed by dynamic or fixed arrays.
Basic binary tree node definitions.
Dynamic queue implementation based on linked lists.