ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FibonacciHeapPriorityQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------- File FibonacciHeapPriorityQueue.h --------------------*/
3/*--------------------------------------------------------------------------*/
59/*--------------------------------------------------------------------------*/
60/*----------------------------- DEFINITIONS --------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63#ifndef __ADS_FibonacciHeapPriorityQueue
64 #define __ADS_FibonacciHeapPriorityQueue
65/* self-identification: #endif constant */
66
67/*--------------------------------------------------------------------------*/
68/*------------------------------ INCLUDES ----------------------------------*/
69/*--------------------------------------------------------------------------*/
70
71#include "ADS/Capabilities.h"
72#include "ADS/Contract.h"
73#include "ADS/Iterators.h"
77
78#include <concepts>
79#include <cstddef>
80#include <iterator>
81#include <type_traits>
82#include <utility>
83#include <vector>
84
85/*--------------------------------------------------------------------------*/
86/*--------------------------- NAMESPACE ------------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89namespace ADS
90{
91
92/*--------------------------------------------------------------------------*/
93/*------------------- CLASS FibonacciHeapPriorityQueue ---------------------*/
94/*--------------------------------------------------------------------------*/
95
98template< class T , std::totally_ordered P = int ,
99 class A = HugePageAllocator< T > >
101{
102
103/*--------------------------------------------------------------------------*/
104 public:
106 struct Node
107 {
110 Node * parent = nullptr;
111 Node * child = nullptr;
112 Node * left = this;
113 Node * right = this;
114 int degree = 0;
115 bool mark = false;
116 };
117
118 using Item = T;
119 using Priority = P;
120 using Handle = Node *;
121
123 FibonacciHeapPriorityQueue( void ) = default;
124
126 explicit FibonacciHeapPriorityQueue( const A & alloc )
127 : f_pool( std::size_t( 1 ) << 24 ,
128 typename Pool::allocator_type( alloc ) ) { }
129
131 using allocator_type = A;
133 [[nodiscard]] allocator_type get_allocator( void ) const {
134 return( allocator_type( f_pool.get_allocator() ) );
135 }
136
139
143
146
149
151 [[nodiscard]] bool empty( void ) const;
152
154 [[nodiscard]] int size( void ) const;
155
158 [[nodiscard]] const T & min( void ) const;
159
162 [[nodiscard]] const P & minPriority( void ) const;
163
185 Handle insert( const T & x , const P & p );
186
224 T deleteMin( void );
225
259 void decrease( Handle x , const P & p );
260
261/*------------------------- THE STANDARD INTERFACE -------------------------*/
262
263 // a constant view of the pairs ( priority , item ), in preorder over the
264 // circular lists of the roots and of the children (no useful order),
265 // forward; a list is done when the walk is back at its first node, the
266 // child of the parent (or the minimum, for the roots)
267
270 {
271 public:
276 using value_type = std::pair< P , T >;
278 using difference_type = std::ptrdiff_t;
280 using iterator_concept = std::forward_iterator_tag;
283 using iterator_category = std::input_iterator_tag;
284
286 const_iterator( void ) = default;
287
289 explicit const_iterator( const Node * min ) : f_x( min ) , f_min( min ) { }
290
292 reference operator*( void ) const {
293 return( reference( f_x->priority , f_x->value ) );
294 }
295
298 if( f_x->child ) {
299 f_x = f_x->child;
300 return( *this );
301 }
302 while( f_x ) {
303 const Node * first = f_x->parent ? f_x->parent->child : f_min;
304 if( f_x->right != first ) {
305 f_x = f_x->right;
306 return( *this );
307 }
308 f_x = f_x->parent; // the list is done
309 }
310 return( *this );
311 }
312
315 auto t = *this;
316 ++*this;
317 return( t );
318 }
319
321 bool operator==( const const_iterator & o ) const {
322 return( f_x == o.f_x );
323 }
324
325 private:
326 const Node * f_x = nullptr;
327 const Node * f_min = nullptr;
328 };
329
331 using value_type = std::pair< P , T >;
333 using size_type = std::size_t;
335 using difference_type = std::ptrdiff_t;
342
344 [[nodiscard]] const_iterator begin( void ) const {
345 return( const_iterator( f_min ) );
346 }
347
348 [[nodiscard]] const_iterator end( void ) const {
349 return( const_iterator() );
350 }
351
352 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
354 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
355
356/*--------------------------------------------------------------------------*/
357 private:
359 static void unlink( Node * x );
360
362 static void splice( Node * x , Node * y );
363
365 static void remove( Node * x , Node *& head );
366
367 void add_root( Node * z );
368
370 void cut( Node * x , Node * y );
371
374 void consolidate( Node * w );
375
377 [[nodiscard]] bool owns( const Node * x ) const;
378
382 [[nodiscard]] bool tree( const Node * x , const Node * parent ,
383 int & n ) const;
384
387 [[nodiscard]] bool isHeap( void ) const;
388
389 Node * f_min = nullptr;
390 int f_n = 0;
393
394 Pool f_pool;
395
396 }; // end( class FibonacciHeapPriorityQueue )
397
400template< class T , std::totally_ordered P , class A >
401inline constexpr bool
403
404/*--------------------------------------------------------------------------*/
405
406} // end( namespace ADS )
407
408/*--------------------------------------------------------------------------*/
409/*-------------------------- IMPLEMENTATION --------------------------------*/
410/*--------------------------------------------------------------------------*/
411
412#include "ADS/priority/impl/FibonacciHeapPriorityQueue.tpp"
413
414/*--------------------------------------------------------------------------*/
415
416#endif /* FibonacciHeapPriorityQueue.h included */
417
418/*--------------------------------------------------------------------------*/
419/*----------------- End File FibonacciHeapPriorityQueue.h ------------------*/
420/*--------------------------------------------------------------------------*/
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,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
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.
the iterator of the view, constant, forward
Definition FibonacciHeapPriorityQueue.h:270
reference operator*(void) const
the priority and the item of the node
Definition FibonacciHeapPriorityQueue.h:292
std::input_iterator_tag iterator_category
an input iterator for the older algorithms, since reference is not a reference
Definition FibonacciHeapPriorityQueue.h:283
std::ptrdiff_t difference_type
the type of the distances between iterators
Definition FibonacciHeapPriorityQueue.h:278
std::forward_iterator_tag iterator_concept
a forward iterator for the concepts of C++20
Definition FibonacciHeapPriorityQueue.h:280
bool operator==(const const_iterator &o) const
whether the two iterators are on the same node
Definition FibonacciHeapPriorityQueue.h:321
std::pair< P, T > value_type
the type of the items of the view
Definition FibonacciHeapPriorityQueue.h:276
const_iterator(const Node *min)
the first node of the view, the root
Definition FibonacciHeapPriorityQueue.h:289
const_iterator & operator++(void)
the next node in preorder
Definition FibonacciHeapPriorityQueue.h:297
const_iterator operator++(int)
the next node in preorder, returning the iterator before the step
Definition FibonacciHeapPriorityQueue.h:314
PairRef< const P &, const T & > reference
the pair of the priority and of the item of a node the type of a reference to an item of the view
Definition FibonacciHeapPriorityQueue.h:274
const_iterator(void)=default
the end of the view
Node * Handle
what insert() gives
Definition FibonacciHeapPriorityQueue.h:120
T Item
the type of the items
Definition FibonacciHeapPriorityQueue.h:118
FibonacciHeapPriorityQueue(const FibonacciHeapPriorityQueue &)=delete
no copy: the handles point into the heap
const_iterator iterator
the same as const_iterator
Definition FibonacciHeapPriorityQueue.h:341
const_iterator cbegin(void) const
the same as begin()
Definition FibonacciHeapPriorityQueue.h:352
FibonacciHeapPriorityQueue(const A &alloc)
the empty queue, the blocks of its nodes from alloc
Definition FibonacciHeapPriorityQueue.h:126
Handle insert(const T &x, const P &p)
inserts with the priority and gives its handle, a tree of one node among the roots,...
allocator_type get_allocator(void) const
the allocator of the blocks of the nodes
Definition FibonacciHeapPriorityQueue.h:133
std::ptrdiff_t difference_type
the type of the distances between iterators
Definition FibonacciHeapPriorityQueue.h:335
std::pair< P, T > value_type
the type of the items of the view
Definition FibonacciHeapPriorityQueue.h:331
FibonacciHeapPriorityQueue(void)=default
the empty heap
const T & min(void) const
the item of smallest priority, the least of the roots
PairRef< const P &, const T & > reference
the type of a reference to an item of the view
Definition FibonacciHeapPriorityQueue.h:337
bool empty(void) const
whether the heap has no item
A allocator_type
the type of the allocator
Definition FibonacciHeapPriorityQueue.h:131
~FibonacciHeapPriorityQueue()
the heap and its nodes destroyed
const_iterator end(void) const
the end of the view
Definition FibonacciHeapPriorityQueue.h:348
void decrease(Handle x, const P &p)
lowers to the priority of the node , in amortized time
int size(void) const
the number of items,
reference const_reference
the type of a constant reference to an item
Definition FibonacciHeapPriorityQueue.h:339
const_iterator cend(void) const
the same as end()
Definition FibonacciHeapPriorityQueue.h:354
std::size_t size_type
the type of the sizes
Definition FibonacciHeapPriorityQueue.h:333
T deleteMin(void)
removes the item of smallest priority and returns it, in amortized time; the handle of the item is n...
FibonacciHeapPriorityQueue & operator=(const FibonacciHeapPriorityQueue &)=delete
no copy: the handles point into the heap
const_iterator begin(void) const
the first item of the view
Definition FibonacciHeapPriorityQueue.h:344
FibonacciHeapPriorityQueue(FibonacciHeapPriorityQueue &&other) noexcept
the items of other, which is left empty; its handles stay valid here
P Priority
the type of the priorities
Definition FibonacciHeapPriorityQueue.h:119
const P & minPriority(void) const
the smallest priority
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
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
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
Definition ADT.h:96
a node of the heap
Definition FibonacciHeapPriorityQueue.h:107
T value
the item
Definition FibonacciHeapPriorityQueue.h:109
Node * right
the next node of its circular list
Definition FibonacciHeapPriorityQueue.h:113
Node * parent
the parent, nullptr for a root
Definition FibonacciHeapPriorityQueue.h:110
Node * child
a child, the entry of their circular list
Definition FibonacciHeapPriorityQueue.h:111
int degree
the number of children
Definition FibonacciHeapPriorityQueue.h:114
Node * left
the previous node of its circular list
Definition FibonacciHeapPriorityQueue.h:112
bool mark
whether it lost a child since it became one
Definition FibonacciHeapPriorityQueue.h:115
P priority
the priority
Definition FibonacciHeapPriorityQueue.h:108