ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LinkedHeapPriorityQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------- File LinkedHeapPriorityQueue.h ---------------------*/
3/*--------------------------------------------------------------------------*/
50/*--------------------------------------------------------------------------*/
51/*----------------------------- DEFINITIONS --------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#ifndef __ADS_LinkedHeapPriorityQueue
55 #define __ADS_LinkedHeapPriorityQueue
56/* self-identification: #endif constant */
57
58/*--------------------------------------------------------------------------*/
59/*------------------------------ INCLUDES ----------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#include "ADS/Contract.h"
63#include "ADS/Iterators.h"
66
67#include <concepts>
68#include <cstddef>
69#include <memory>
70#include <utility>
71
72/*--------------------------------------------------------------------------*/
73/*--------------------------- NAMESPACE ------------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76namespace ADS
77{
78
79/*--------------------------------------------------------------------------*/
80/*--------------------- CLASS LinkedHeapPriorityQueue ----------------------*/
81/*--------------------------------------------------------------------------*/
82
85template< class T , std::totally_ordered P = int , bool L = false >
87{
88
89/*--------------------------------------------------------------------------*/
90 public:
91 using Item = T;
92 using Priority = P;
93
95 LinkedHeapPriorityQueue( void ) = default;
96
99
103
106
109 operator=( LinkedHeapPriorityQueue && other ) noexcept;
110
113
115 [[nodiscard]] bool empty( void ) const;
116
118 [[nodiscard]] int size( void ) const;
119
122 [[nodiscard]] const T & min( void ) const;
123
126 [[nodiscard]] const P & minPriority( void ) const;
127
173 void insert( const T & x , const P & p );
174
203 T deleteMin( void );
204
205/*--------------------------------------------------------------------------*/
206 private:
208 struct Node
209 {
210 P priority;
211 T value;
212 Node * parent;
213 Node *left = nullptr ,
214 *right = nullptr;
217 Node( const P & p , const T & v , Node * u )
218 : priority( p ) , value( v ) , parent( u ) { }
219 };
220
221 public:
222/*------------------------- THE STANDARD INTERFACE -------------------------*/
223
224 // a constant view of the pairs ( priority , item ), in the symmetric
225 // order of the tree (no useful order), bidirectional
226
228 struct Shape
229 {
231 static auto root( const auto & o ) {
232 return( static_cast< const Node * >( o.f_root ) );
233 }
234
235 static auto left( const auto * n ) {
236 return( static_cast< const Node * >( n->left ) );
237 }
238
239 static auto right( const auto * n ) {
240 return( static_cast< const Node * >( n->right ) );
241 }
242
243 static auto read( const auto * n ) {
244 return( PairRef< const P & , const T & >( n->priority , n->value ) );
245 }
246 };
247
249 using value_type = std::pair< P , T >;
251 using size_type = std::size_t;
253 using difference_type = std::ptrdiff_t;
262
264 [[nodiscard]] const_iterator begin( void ) const {
265 return( const_iterator( this , true ) );
266 }
267
268 [[nodiscard]] const_iterator end( void ) const {
269 return( const_iterator( this , false ) );
270 }
271
272 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
274 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
275
276 private:
278 Node * at( int k ) const;
279
280 static void swap_contents( Node * a , Node * b );
281
284 [[nodiscard]] bool shaped( const Node * u , int k , int & n ) const;
285
288 [[nodiscard]] bool isHeap( void ) const;
289
292 [[nodiscard]] Node * nextParent( void ) const;
293
296 [[nodiscard]] Node * previousLeaf( void ) const;
297
299 void release( Node * u );
300
301 Node * f_root = nullptr;
302 Node * f_last = nullptr;
303 int f_n = 0;
304 ObjectPool< Node > f_pool;
305
306 }; // end( class LinkedHeapPriorityQueue )
307
308/*--------------------------------------------------------------------------*/
309
310} // end( namespace ADS )
311
312/*--------------------------------------------------------------------------*/
313/*-------------------------- IMPLEMENTATION --------------------------------*/
314/*--------------------------------------------------------------------------*/
315
316#include "ADS/priority/impl/LinkedHeapPriorityQueue.tpp"
317
318/*--------------------------------------------------------------------------*/
319
320#endif /* LinkedHeapPriorityQueue.h included */
321
322/*--------------------------------------------------------------------------*/
323/*------------------- End File LinkedHeapPriorityQueue.h -------------------*/
324/*--------------------------------------------------------------------------*/
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.
const_iterator end(void) const
the end of the view
Definition LinkedHeapPriorityQueue.h:268
std::ptrdiff_t difference_type
the type of the distances between iterators
Definition LinkedHeapPriorityQueue.h:253
const_iterator begin(void) const
the first item of the view
Definition LinkedHeapPriorityQueue.h:264
LinkedHeapPriorityQueue(const LinkedHeapPriorityQueue &)=delete
no copy: the nodes belong to the queue
const_iterator iterator
the same as const_iterator
Definition LinkedHeapPriorityQueue.h:261
PairRef< const P &, const T & > reference
the type of a reference to an item of the view
Definition LinkedHeapPriorityQueue.h:255
T Item
the type of the items
Definition LinkedHeapPriorityQueue.h:91
const T & min(void) const
the item of smallest priority, at the root
const P & minPriority(void) const
the smallest priority
const_iterator cbegin(void) const
the same as begin()
Definition LinkedHeapPriorityQueue.h:272
bool empty(void) const
whether the queue has no item
LinkedHeapPriorityQueue(void)=default
the empty queue
int size(void) const
the number of items,
LinkedHeapPriorityQueue & operator=(const LinkedHeapPriorityQueue &)=delete
no copy: the nodes belong to the queue
LinkedHeapPriorityQueue(LinkedHeapPriorityQueue &&other) noexcept
the items of other, which is left empty
reference const_reference
the type of a constant reference to an item
Definition LinkedHeapPriorityQueue.h:257
PathIterator< LinkedHeapPriorityQueue, Shape > const_iterator
the iterator of the view, constant
Definition LinkedHeapPriorityQueue.h:259
void insert(const T &x, const P &p)
inserts with the priority , a new last leaf moved up while smaller than its parent,...
LinkedHeapPriorityQueue & operator=(LinkedHeapPriorityQueue &&other) noexcept
the items of other, which is left empty, in place of these
std::pair< P, T > value_type
the type of the items of the view
Definition LinkedHeapPriorityQueue.h:249
const_iterator cend(void) const
the same as end()
Definition LinkedHeapPriorityQueue.h:274
T deleteMin(void)
removes the item of smallest priority and returns it, the last leaf moved into the root and down whil...
std::size_t size_type
the type of the sizes
Definition LinkedHeapPriorityQueue.h:251
~LinkedHeapPriorityQueue()
the queue and its nodes destroyed
P Priority
the type of the priorities
Definition LinkedHeapPriorityQueue.h:92
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
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
the shape of the tree, for PathIterator
Definition LinkedHeapPriorityQueue.h:229
static auto left(const auto *n)
the left child of the node
Definition LinkedHeapPriorityQueue.h:235
static auto read(const auto *n)
the priority and the item of the node
Definition LinkedHeapPriorityQueue.h:243
static auto root(const auto &o)
the root of the queue
Definition LinkedHeapPriorityQueue.h:231
static auto right(const auto *n)
the right child of the node
Definition LinkedHeapPriorityQueue.h:239