ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MinMaxHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File MinMaxHeap.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
59/*--------------------------------------------------------------------------*/
60/*----------------------------- DEFINITIONS --------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63#ifndef __ADS_MinMaxHeap
64 #define __ADS_MinMaxHeap
65/* self-identification: #endif constant */
66
67/*--------------------------------------------------------------------------*/
68/*------------------------------ INCLUDES ----------------------------------*/
69/*--------------------------------------------------------------------------*/
70
71#include "ADS/Contract.h"
73#include "ADS/sort/Sort.h"
74
75#include <bit>
76#include <cstddef>
77#include <functional>
78#include <utility>
79#include <vector>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88/*--------------------------------------------------------------------------*/
89/*--------------------------- CLASS MinMaxHeap -----------------------------*/
90/*--------------------------------------------------------------------------*/
91
93template< class T , class Better = std::less< T > >
95{
96
97/*--------------------------------------------------------------------------*/
98 public:
99 using Item = T;
100
102 explicit MinMaxHeap( Better better = Better() )
103 : f_better( std::move( better ) ) { }
104
130 void push( const T & x );
131
134 [[nodiscard]] const T & min( void ) const;
135
138 [[nodiscard]] const T & max( void ) const;
139
172 T popMin( void );
173
197 T popMax( void );
198
200 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
201
203 [[nodiscard]] std::size_t size( void ) const {
204 return( f_sorted ? f_hi - f_lo : f_A.size() );
205 }
206
209 void reserve( std::size_t n ) { f_A.reserve( n ); }
210
212 void clear( void ) {
213 f_A.clear();
214 f_sorted = false;
215 f_run = 0;
216 }
217
218/*--------------------------------------------------------------------------*/
219 private:
221 [[nodiscard]] std::size_t maxAt( void ) const;
222
224 static bool minLevel( std::size_t i ) {
225 return( ( std::bit_width( i + 1 ) - 1 ) % 2 == 0 );
226 }
227
229 template< bool Min >
230 bool first( const T & a , const T & b ) const {
231 return( Min ? f_better( a , b ) : f_better( b , a ) );
232 }
233
235 template< bool Min >
236 void up( std::size_t i );
237
239 template< bool Min >
240 void down( std::size_t i );
241
243 [[nodiscard]] bool ordered( void ) const;
244
246 void run( void );
247
249 void unsort( void );
250
251 std::vector< T > f_A;
252 Better f_better;
253 bool f_sorted = false;
254 std::size_t f_lo = 0 , f_hi = 0;
255 std::size_t f_run = 0;
256
257 }; // end( class MinMaxHeap )
258
259/*--------------------------------------------------------------------------*/
260/*-------------------------- CLASS IntervalHeap ----------------------------*/
261/*--------------------------------------------------------------------------*/
262
264template< class T , class Better = std::less< T > >
266{
267
268/*--------------------------------------------------------------------------*/
269 public:
270 using Item = T;
271
273 explicit IntervalHeap( Better better = Better() )
274 : f_better( std::move( better ) ) { }
275
308 void push( const T & x );
309
312 [[nodiscard]] const T & min( void ) const;
313
316 [[nodiscard]] const T & max( void ) const;
317
349 T popMin( void );
350
385 T popMax( void );
386
388 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
389
391 [[nodiscard]] std::size_t size( void ) const {
392 return( f_sorted ? f_hi - f_lo : f_A.size() );
393 }
394
397 void reserve( std::size_t n ) { f_A.reserve( n ); }
398
400 void clear( void ) {
401 f_A.clear();
402 f_sorted = false;
403 f_run = 0;
404 }
405
406/*--------------------------------------------------------------------------*/
407 private:
409 [[nodiscard]] std::size_t hi( std::size_t j ) const {
410 return( 2 * j + 1 < f_A.size() ? 2 * j + 1 : 2 * j );
411 }
412
415 [[nodiscard]] bool ordered( void ) const;
416
418 void run( void );
419
421 void unsort( void );
422
424 std::vector< T > f_A;
425 Better f_better;
426 bool f_sorted = false;
427 std::size_t f_lo = 0 , f_hi = 0;
428 std::size_t f_run = 0;
429
430 }; // end( class IntervalHeap )
431
432/*--------------------------------------------------------------------------*/
433
434} // end( namespace ADS )
435
436/*--------------------------------------------------------------------------*/
437/*-------------------------- IMPLEMENTATION --------------------------------*/
438/*--------------------------------------------------------------------------*/
439
440#include "ADS/priority/impl/MinMaxHeap.tpp"
441
442/*--------------------------------------------------------------------------*/
443
444#endif /* MinMaxHeap.h included */
445
446/*--------------------------------------------------------------------------*/
447/*-------------------------- End File MinMaxHeap.h -------------------------*/
448/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The unsigned key of an item whose order is that of an unsigned word, beyond the book,...
The sort that chooses its algorithm, beyond the book: sort() and stableSort() look at what they sort ...
void reserve(std::size_t n)
room for items, so that the pushes up to do not reallocate
Definition MinMaxHeap.h:397
T Item
the type of the items
Definition MinMaxHeap.h:270
const T & max(void) const
the worst item, the of the root
IntervalHeap(Better better=Better())
the empty queue of the order better
Definition MinMaxHeap.h:273
T popMax(void)
removes the worst item and returns it, in amortized time
T popMin(void)
removes the best item and returns it, in amortized time
void clear(void)
removes all the items
Definition MinMaxHeap.h:400
std::size_t size(void) const
the number of items,
Definition MinMaxHeap.h:391
void push(const T &x)
pushes , in time
bool empty(void) const
whether the queue has no item
Definition MinMaxHeap.h:388
const T & min(void) const
the best item, the of the root
void clear(void)
removes all the items
Definition MinMaxHeap.h:212
const T & max(void) const
the worst item, the worse of the two children of the root
T Item
the type of the items
Definition MinMaxHeap.h:99
MinMaxHeap(Better better=Better())
the empty queue of the order better
Definition MinMaxHeap.h:102
bool empty(void) const
whether the queue has no item
Definition MinMaxHeap.h:200
std::size_t size(void) const
the number of items,
Definition MinMaxHeap.h:203
void reserve(std::size_t n)
room for items, so that the pushes up to do not reallocate
Definition MinMaxHeap.h:209
T popMax(void)
removes the worst item and returns it, in amortized time
T popMin(void)
removes the best item and returns it, in amortized time
const T & min(void) const
the best item, at the root
void push(const T &x)
pushes , in time
Definition ADT.h:96