ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PairingHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File PairingHeap.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
89/*--------------------------------------------------------------------------*/
90/*----------------------------- DEFINITIONS --------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93#ifndef __ADS_PairingHeap
94 #define __ADS_PairingHeap
95/* self-identification: #endif constant */
96
97/*--------------------------------------------------------------------------*/
98/*------------------------------ INCLUDES ----------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101#include "ADS/Capabilities.h"
102#include "ADS/Contract.h"
106
107#include <concepts>
108#include <cstddef>
109#include <type_traits>
110#include <utility>
111#include <vector>
112
113/*--------------------------------------------------------------------------*/
114/*--------------------------- NAMESPACE ------------------------------------*/
115/*--------------------------------------------------------------------------*/
116
117namespace ADS
118{
119
120/*--------------------------------------------------------------------------*/
121/*-------------------------- CLASS PairingHeap -----------------------------*/
122/*--------------------------------------------------------------------------*/
123
126template< class T , std::totally_ordered P = int , bool Aux = false ,
127 class A = HugePageAllocator< T > >
129{
130
131/*--------------------------------------------------------------------------*/
132 public:
134 struct Node
135 {
138 Node * child = nullptr;
139 Node * next = nullptr;
140 Node * prev = nullptr;
141 };
142
143 using Item = T;
144 using Priority = P;
145 using Handle = Node *;
146
148 PairingHeap( void ) = default;
149
151 explicit PairingHeap( const A & alloc )
152 : f_pool( std::size_t( 1 ) << 24 ,
153 typename Pool::allocator_type( alloc ) ) { }
154
156 using allocator_type = A;
158 [[nodiscard]] allocator_type get_allocator( void ) const {
159 return( allocator_type( f_pool.get_allocator() ) );
160 }
161
163 PairingHeap( const PairingHeap & ) = delete;
164
166 PairingHeap & operator=( const PairingHeap & ) = delete;
167
169 PairingHeap( PairingHeap && other ) noexcept;
170
173
175 [[nodiscard]] bool empty( void ) const { return( f_root == nullptr ); }
176
178 [[nodiscard]] int size( void ) const { return( f_n ); }
179
182 [[nodiscard]] const T & min( void ) const;
183
186 [[nodiscard]] const P & minPriority( void ) const;
187
215 Handle insert( const T & x , const P & p );
216
260 T deleteMin( void );
261
287 void decrease( Handle x , const P & p );
288
319 T erase( Handle x );
320
339 void increase( Handle x , const P & p );
340
345 void update( Handle x , const P & p );
346
347/*--------------------------------------------------------------------------*/
348 private:
350 static Node * link( Node * a , Node * b );
351
353 static Node * twoPass( Node * x );
354
356 static Node * multiPass( Node * x );
357
359 static void cut( Node * x );
360
362 void unlinkMin( void );
363
366 void detach( Node * x );
367
369 void attach( Node * x );
370
373 [[nodiscard]] static bool tree( const Node * x , int & n );
374
376 [[nodiscard]] bool isHeap( void ) const;
377
378 Node * f_root = nullptr;
379 Node * f_min = nullptr;
380 int f_n = 0;
383
384 Pool f_pool;
385
386 }; // end( class PairingHeap )
387
390template< class T , std::totally_ordered P , bool Aux , class A >
391inline constexpr bool enable_stable_references< PairingHeap< T , P , Aux ,
392 A > > =
393 true;
394
395/*--------------------------------------------------------------------------*/
396
397} // end( namespace ADS )
398
399/*--------------------------------------------------------------------------*/
400/*-------------------------- IMPLEMENTATION --------------------------------*/
401/*--------------------------------------------------------------------------*/
402
403#include "ADS/priority/impl/PairingHeap.tpp"
404
405/*--------------------------------------------------------------------------*/
406
407#endif /* PairingHeap.h included */
408
409/*--------------------------------------------------------------------------*/
410/*------------------------- End File PairingHeap.h -------------------------*/
411/*--------------------------------------------------------------------------*/
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and fre...
The PRIORITYQUEUE of section 10.1, with decreasing priority, in two concepts.
an allocator of objects of type T, on huge pages for the large blocks
Definition HugePageAllocator.h:169
a pool of objects of type T in blocks of at most slots
Definition ObjectPool.h:108
a priority queue on a pairing heap, two-pass, or auxiliary two-pass if Aux
Definition PairingHeap.h:129
void decrease(Handle x, const P &p)
lowers to the priority of the node , cut with its subtree and linked with the root,...
allocator_type get_allocator(void) const
the allocator of the blocks of the nodes
Definition PairingHeap.h:158
~PairingHeap()
the heap and its nodes destroyed
PairingHeap & operator=(const PairingHeap &)=delete
no copy: the handles point into the heap
const P & minPriority(void) const
the smallest priority
PairingHeap(const PairingHeap &)=delete
no copy: the handles point into the heap
A allocator_type
the type of the allocator
Definition PairingHeap.h:156
T Item
the type of the items
Definition PairingHeap.h:143
P Priority
the type of the priorities
Definition PairingHeap.h:144
void update(Handle x, const P &p)
sets the priority of the node to , by decrease() or by increase()
PairingHeap(const A &alloc)
the empty heap, the blocks of its nodes from alloc
Definition PairingHeap.h:151
int size(void) const
the number of items,
Definition PairingHeap.h:178
PairingHeap(void)=default
the empty heap
bool empty(void) const
whether the heap has no item
Definition PairingHeap.h:175
Handle insert(const T &x, const P &p)
inserts with the priority and gives its handle, a tree of one node linked with the root,...
T erase(Handle x)
removes the item of the node , wherever it is, and returns it: the subtrees of its children joined by...
const T & min(void) const
the item of smallest priority
PairingHeap(PairingHeap &&other) noexcept
the items of other, which is left empty; its handles stay valid here
void increase(Handle x, const P &p)
raises to the priority of the node , which keeps its handle: the node leaves the heap as in erase() ...
Node * Handle
what insert() gives
Definition PairingHeap.h:145
T deleteMin(void)
removes the item of smallest priority and returns it, its children joined by two passes; the handle o...
constexpr bool enable_stable_references
whether the items of S keep their addresses while they are in it, so that a handle may be a pointer a...
Definition Capabilities.h:203
Definition ADT.h:96
a node of the heap
Definition PairingHeap.h:135
Node * child
the leftmost child
Definition PairingHeap.h:138
T value
the item
Definition PairingHeap.h:137
Node * prev
the left sibling, or the parent
Definition PairingHeap.h:140
Node * next
the right sibling
Definition PairingHeap.h:139
P priority
the priority
Definition PairingHeap.h:136