63#ifndef __ADS_MinMaxHeap
64 #define __ADS_MinMaxHeap
93template<
class T ,
class Better = std::less< T > >
103 : f_better( std::move( better ) ) { }
134 [[nodiscard]]
const T &
min(
void )
const;
138 [[nodiscard]]
const T &
max(
void )
const;
200 [[nodiscard]]
bool empty(
void )
const {
return(
size() == 0 ); }
203 [[nodiscard]] std::size_t
size(
void )
const {
204 return( f_sorted ? f_hi - f_lo : f_A.size() );
209 void reserve( std::size_t n ) { f_A.reserve( n ); }
221 [[nodiscard]] std::size_t maxAt(
void )
const;
224 static bool minLevel( std::size_t i ) {
225 return( ( std::bit_width( i + 1 ) - 1 ) % 2 == 0 );
230 bool first(
const T & a ,
const T & b )
const {
231 return( Min ? f_better( a , b ) : f_better( b , a ) );
236 void up( std::size_t i );
240 void down( std::size_t i );
243 [[nodiscard]]
bool ordered(
void )
const;
251 std::vector< T > f_A;
253 bool f_sorted =
false;
254 std::size_t f_lo = 0 , f_hi = 0;
255 std::size_t f_run = 0;
264template<
class T ,
class Better = std::less< T > >
274 : f_better( std::move( better ) ) { }
312 [[nodiscard]]
const T &
min(
void )
const;
316 [[nodiscard]]
const T &
max(
void )
const;
388 [[nodiscard]]
bool empty(
void )
const {
return(
size() == 0 ); }
391 [[nodiscard]] std::size_t
size(
void )
const {
392 return( f_sorted ? f_hi - f_lo : f_A.size() );
397 void reserve( std::size_t n ) { f_A.reserve( n ); }
409 [[nodiscard]] std::size_t hi( std::size_t j )
const {
410 return( 2 * j + 1 < f_A.size() ? 2 * j + 1 : 2 * j );
415 [[nodiscard]]
bool ordered(
void )
const;
424 std::vector< T > f_A;
426 bool f_sorted =
false;
427 std::size_t f_lo = 0 , f_hi = 0;
428 std::size_t f_run = 0;
440#include "ADS/priority/impl/MinMaxHeap.tpp"
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