44# ifndef TPL_FIBONACCI_HEAP_H
45# define TPL_FIBONACCI_HEAP_H
110 template <
typename T,
class Compare = Aleph::less<T>>
139 explicit Node(
T && d)
noexcept(std::is_nothrow_move_constructible_v<T>)
153 template <
typename Arg,
typename...
Args,
154 std::enable_if_t<(
sizeof...(Args) >= 1) ||
155 (!std::is_same_v<std::decay_t<Arg>,
T> &&
156 !std::is_same_v<std::decay_t<Arg>,
Node>),
int> = 0>
158 :
data(std::forward<Arg>(
arg), std::forward<Args>(
args)...) {}
193 y->left->right =
y->right;
194 y->right->left =
y->left;
198 if (x->
child ==
nullptr)
365 while (curr !=
nullptr)
369 if (curr->
child !=
nullptr)
434 template <
typename...
Args>
436 :
cmp(std::forward<Args>(
args)...)
471 other.min_node =
nullptr;
492 other.min_node =
nullptr;
545 Node *node =
new Node(std::move(val));
563 template <
typename...
Args>
618 if (
z->child !=
nullptr)
621 Node *child =
z->child;
625 child = child->
right;
627 while (child !=
z->child);
649 z->left->right =
z->right;
650 z->right->left =
z->left;
656 T data = std::move(
z->data);
678 <<
"Fibonacci_Heap::decrease_key: null node pointer";
680 <<
"Fibonacci_Heap::decrease_key: new key is greater than current key";
685 if (
y !=
nullptr &&
cmp(x->
data,
y->data))
704 <<
"Fibonacci_Heap::decrease_key: null node pointer";
706 <<
"Fibonacci_Heap::decrease_key: new key is greater than current key";
708 x->
data = std::move(
k);
711 if (
y !=
nullptr &&
cmp(x->
data,
y->data))
737 <<
"Fibonacci_Heap::update_key: null node pointer";
770 <<
"Fibonacci_Heap::delete_node: null node pointer";
785 if (x->
child !=
nullptr)
791 child = child->
right;
793 while (child != x->
child);
876 other.min_node =
nullptr;
965 template <
typename T,
class Compare>
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Functional programming utilities for Aleph-w containers.
General utility functions and helpers.
Implementation of a Fibonacci Heap priority queue.
void swap(Fibonacci_Heap &other) noexcept
Swaps contents with another heap.
static constexpr size_t MAX_DEGREE
Maximum degree tracked during consolidation.
Node * min_node
Pointer to the current minimum root.
Fibonacci_Heap(Compare compare=Compare()) noexcept
Default constructor.
const T & get_min() const
Returns the minimum element without removing it.
void swap(Fibonacci_Heap< T, Compare > &a, Fibonacci_Heap< T, Compare > &b) noexcept
Swaps two Fibonacci heaps.
Fibonacci_Heap & operator=(Fibonacci_Heap &&other) noexcept
Move assignment operator.
Node * insert(T &&val)
Inserts a new element (move).
Node * get_min_node() const noexcept
Returns a pointer to the minimum node.
Fibonacci_Heap & operator=(const Fibonacci_Heap &)=delete
Copy assignment is disabled (use merge or manual copy)
Fibonacci_Heap(Fibonacci_Heap &&other) noexcept
Move constructor.
Compare key_comp() const
Returns the comparison functor.
Compare cmp
Comparison functor used to order nodes.
void clear() noexcept(std::is_nothrow_destructible_v< T >)
Removes all elements from the heap.
T value_type
Type alias for the element type.
void add_to_root_list(Node *node)
Adds a node to the root list.
void cascading_cut(Node *y)
Performs cascading cut operation.
Node * insert(const T &val)
Inserts a new element (copy).
void merge(Fibonacci_Heap &other)
Merges another heap into this one.
void decrease_key(Node *x, T &&k)
Decreases the key of a node (move version).
static void link(Node *y, Node *x)
Links two trees of the same degree.
bool empty() const noexcept
void cut(Node *x, Node *y)
Cuts a node from its parent and adds it to the root list.
~Fibonacci_Heap()
Destructor.
std::vector< Node * > consolidate_array_
Reusable scratch array for Fibonacci_Heap::consolidate().
void consolidate()
Consolidates the root list after extract_min.
Node * emplace(Args &&... args)
Constructs and inserts an element in-place.
size_t size() const noexcept
Returns the number of elements in the heap.
T extract_min()
Extracts and returns the minimum element.
size_t num_nodes
Number of elements stored in the heap.
void merge(Fibonacci_Heap &&other)
Merges another heap into this one (rvalue version).
void delete_node(Node *x)
Deletes a specific node from the heap.
Node * update_key(Node *x, const T &k)
Updates the key of a node (increase or decrease).
Compare key_compare
Type alias for the comparison functor.
Fibonacci_Heap(std::in_place_t, Args &&... args) noexcept
Constructor with in-place comparator construction.
void decrease_key(Node *x, const T &k)
Decreases the key of a node.
void delete_all_nodes(Node *node)
Recursively deletes all nodes in a tree.
bool is_empty() const noexcept
Checks if the heap is empty.
Fibonacci_Heap(const Fibonacci_Heap &)=delete
Copy constructor is disabled (use merge or manual copy)
constexpr bool is_empty() const noexcept
Return true if list is empty.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
void next()
Advance all underlying iterators (bounds-checked).
DynList< T > maps(const C &c, Op op)
Classic map operation.
Represents a node in the Fibonacci Heap.
T data
The data stored in this node.
Node * parent
Parent node (nullptr if root)
bool mark
Has this node lost a child since becoming non-root?
size_t degree
Number of children.
Node(Arg &&arg, Args &&... args)
Perfect-forwarding constructor for in-place construction.
Node * left
Left sibling in circular list.
Node(T &&d) noexcept(std::is_nothrow_move_constructible_v< T >)
Construct a node moving d into the internal storage.
Node(const T &d)
Construct a node copying d into the internal storage.
Node * child
Pointer to one child (head of child list)
Node * right
Right sibling in circular list.