ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
HeapPriorityQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File HeapPriorityQueue.h ------------------------*/
3/*--------------------------------------------------------------------------*/
48/*--------------------------------------------------------------------------*/
49/*----------------------------- DEFINITIONS --------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52#ifndef __ADS_HeapPriorityQueue
53 #define __ADS_HeapPriorityQueue
54/* self-identification: #endif constant */
55
56/*--------------------------------------------------------------------------*/
57/*------------------------------ INCLUDES ----------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#include "ADS/Contract.h"
61#include "ADS/Iterators.h"
64
65#include <concepts>
66#include <cstddef>
67#include <memory>
68#include <utility>
69#include <vector>
70
71/*--------------------------------------------------------------------------*/
72/*--------------------------- NAMESPACE ------------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75namespace ADS
76{
77
78/*--------------------------------------------------------------------------*/
79/*------------------------ CLASS HeapPriorityQueue -------------------------*/
80/*--------------------------------------------------------------------------*/
81
84template< class T , std::totally_ordered P = int , int D = 2 >
85 requires( D >= 2 )
87{
88
89/*--------------------------------------------------------------------------*/
90 public:
93 {
96 int pos;
97 };
98
99 using Item = T;
100 using Priority = P;
102
106 explicit HeapPriorityQueue( int n );
107
110
113
116
120
123
125 [[nodiscard]] bool empty( void ) const;
126
128 [[nodiscard]] int size( void ) const;
129
131 [[nodiscard]] int capacity( void ) const;
132
135 [[nodiscard]] const T & min( void ) const;
136
139 [[nodiscard]] const P & minPriority( void ) const;
140
168 T deleteMin( void );
169
193 Handle insert( const T & x , const P & p );
194
210 void decrease( Handle y , const P & p );
211
213 [[nodiscard]] std::vector< P > priorities( void ) const;
214
217 static int parent( int i ) { return( ( i - 1 ) / D ); }
218
221 static int child( int i , int j ) { return( D * i + j + 1 ); }
222
223/*------------------------- THE STANDARD INTERFACE -------------------------*/
224
225 // a constant view of the items with their priorities, PriorityItem, in
226 // the order of the heap (the minimum first, the rest in no useful order),
227 // random access; a change through it would break the heap
228
232 using size_type = std::size_t;
234 using difference_type = std::ptrdiff_t;
236 using reference = const PriorityItem &;
244
246 [[nodiscard]] const_iterator begin( void ) const {
247 return( const_iterator( f_H.begin() ) );
248 }
249
250 [[nodiscard]] const_iterator end( void ) const {
251 return( const_iterator( f_H.end() ) );
252 }
253
254 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
256 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
257
258/*--------------------------------------------------------------------------*/
259 private:
261 void swap( int i , int j );
262
263 void up( int i );
264
267 void minHeapRestore( int i , int dim );
268
270 [[nodiscard]] bool owns( Handle y ) const;
271
274 [[nodiscard]] bool isHeap( void ) const;
275
276 int f_capacity;
277 std::vector< PriorityItem * > f_H;
279
280 }; // end( class HeapPriorityQueue )
281
282/*--------------------------------------------------------------------------*/
283
284} // end( namespace ADS )
285
286/*--------------------------------------------------------------------------*/
287/*-------------------------- IMPLEMENTATION --------------------------------*/
288/*--------------------------------------------------------------------------*/
289
290#include "ADS/priority/impl/HeapPriorityQueue.tpp"
291
292/*--------------------------------------------------------------------------*/
293
294#endif /* HeapPriorityQueue.h included */
295
296/*--------------------------------------------------------------------------*/
297/*---------------------- End File HeapPriorityQueue.h ----------------------*/
298/*--------------------------------------------------------------------------*/
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.
static int parent(int i)
the parent of the place
Definition HeapPriorityQueue.h:217
HeapPriorityQueue(const HeapPriorityQueue &)=delete
no copy: the handles point into the queue
HeapPriorityQueue & operator=(HeapPriorityQueue &&other) noexcept
the items of other, which is left empty, in place of these; its handles stay valid here
const_iterator cend(void) const
the same as end()
Definition HeapPriorityQueue.h:256
std::size_t size_type
the type of the sizes
Definition HeapPriorityQueue.h:232
IndirectIterator< typename std::vector< PriorityItem * >::const_iterator > const_iterator
the iterator of the view, constant
Definition HeapPriorityQueue.h:240
int size(void) const
the number of items,
HeapPriorityQueue(int n)
the empty queue of capacity (the PriorityQueue( n ) of the book)
std::ptrdiff_t difference_type
the type of the distances between iterators
Definition HeapPriorityQueue.h:234
std::vector< P > priorities(void) const
the heap: the priorities, by their place
const PriorityItem & const_reference
the type of a constant reference to an item
Definition HeapPriorityQueue.h:238
const_iterator begin(void) const
the first item of the view
Definition HeapPriorityQueue.h:246
bool empty(void) const
whether the queue has no item
int capacity(void) const
the largest number of items,
~HeapPriorityQueue()
the queue and its items destroyed
static int child(int i, int j)
the child of the place ,
Definition HeapPriorityQueue.h:221
void decrease(Handle y, const P &p)
lowers to the priority of the item of the handle , which moves up, in time
PriorityItem value_type
the type of the items of the view
Definition HeapPriorityQueue.h:230
T Item
the type of the items
Definition HeapPriorityQueue.h:99
Handle insert(const T &x, const P &p)
inserts with the priority and gives its handle, a new leaf moved up, in time
const_iterator end(void) const
the end of the view
Definition HeapPriorityQueue.h:250
const PriorityItem & reference
the type of a reference to an item of the view
Definition HeapPriorityQueue.h:236
P Priority
the type of the priorities
Definition HeapPriorityQueue.h:100
PriorityItem * Handle
what insert() gives
Definition HeapPriorityQueue.h:101
T deleteMin(void)
removes the item of smallest priority and returns it, the last leaf moved to the root and down,...
const P & minPriority(void) const
the smallest priority
const_iterator cbegin(void) const
the same as begin()
Definition HeapPriorityQueue.h:254
const T & min(void) const
the item of smallest priority, the root
const_iterator iterator
the same as const_iterator
Definition HeapPriorityQueue.h:243
HeapPriorityQueue(HeapPriorityQueue &&other) noexcept
the items of other, which is left empty; its handles stay valid here
HeapPriorityQueue & operator=(const HeapPriorityQueue &)=delete
no copy: the handles point into the queue
the random-access iterator I on pointers read through them, *it being **base; the items are constant
Definition Iterators.h:507
a pool of objects of type T in blocks of at most slots
Definition ObjectPool.h:108
Definition ADT.h:96
the PRIORITYITEM of the book: priority, item, place in the vector
Definition HeapPriorityQueue.h:93
P priority
the priority
Definition HeapPriorityQueue.h:94
T value
the item
Definition HeapPriorityQueue.h:95
int pos
the place in the vector of the heap
Definition HeapPriorityQueue.h:96