ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SequenceHeap< T, Better > Class Template Reference

the sequence heap of plain items, the best one by better() on top More...

#include <ADS/priority/SequenceHeap.h>

Public types

using Item = T
 the type of the items

Public member functions

 SequenceHeap (Better better=Better())
 the empty heap of the order better
void push (const T &x)
 pushes \( x \), in \( O( \log n ) \) amortized time
const T & top (void)
 the best item
T pop (void)
 removes the best item and returns it, in \( O( \log n ) \) amortized time
bool empty (void) const
 whether the heap has no item
std::size_t size (void) const
 the number of items, \( n \)
void clear (void)
 removes all the items

Static public attributes

static constexpr std::size_t M = 4096
 the items of the insertion heap and of a buffer, \( m \)
static constexpr std::size_t K = 16
 the sequences of a group, \( k \)

Detailed description

template<class T, class Better = std::less< T >>
class ADS::SequenceHeap< T, Better >

the sequence heap of plain items, the best one by better() on top

Member typedef documentation

◆ Item

template<class T, class Better = std::less< T >>
using ADS::SequenceHeap< T, Better >::Item = T

the type of the items

Constructor and destructor documentation

◆ SequenceHeap()

template<class T, class Better = std::less< T >>
ADS::SequenceHeap< T, Better >::SequenceHeap ( Better better = Better())
inlineexplicit

the empty heap of the order better

Member function documentation

◆ clear()

template<class T, class Better = std::less< T >>
void ADS::SequenceHeap< T, Better >::clear ( void )

removes all the items

◆ empty()

template<class T, class Better = std::less< T >>
bool ADS::SequenceHeap< T, Better >::empty ( void ) const
inlinenodiscard

whether the heap has no item

◆ pop()

template<class T, class Better = std::less< T >>
T ADS::SequenceHeap< T, Better >::pop ( void )

removes the best item and returns it, in \( O( \log n ) \) amortized time

In pseudocode (Sanders, beyond the book; the incremental quicksort of Paredes and Navarro for the bulk), with the names of push():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad \textbf{if}\ \text{the bulk is open }\ \textbf{then} \cr 3 & \qquad \textbf{if}\ \text{it is the first look at more than } m \text{ items }\ \textbf{then}\ \text{put its best item last by a} \text{ scan} \cr 4 & \qquad \textbf{else if}\ \text{its best item is not last } \ \textbf{then}\ \text{close it: to } I \text{ if at most } m \text{, else to the quicksort} \cr 5 & \qquad \textbf{if}\ \text{the bulk is still open } \ \textbf{then}\ n \gets n - 1;\ \textbf{return}\ \text{its last} \text{ item, removed} \cr 6 & \quad \textbf{if}\ I \text{ is not a heap}\ \textbf{then} \ \text{make it one from the bottom up} \cr 7 & \quad \textbf{if}\ D = \emptyset\ \textbf{then} \ \mathrm{refillDeletion}() \cr 8 & \quad n \gets n - 1 \cr 9 & \quad \textbf{if}\ D = \emptyset\ \textbf{or}\ \mathrm{better}( \mathrm{top}( I ) , \text{the first of } D )\ \textbf{then} \ \textbf{return}\ \mathrm{deleteMin}( I ) \cr 10 & \quad \textbf{return}\ \text{the first of } D \text{, removed} \cr 11 & \textbf{procedure}\ \mathrm{refillDeletion}() \cr 12 & \quad \textbf{foreach}\ i\ \textbf{do}\ \textbf{if} \ G_i.\mathit{buf} = \emptyset\ \textbf{then}\ \text{refill it} \text{ with the first } m \text{ items of the merge of } G_i \cr 13 & \quad \text{refill the buffer of the bulk, if empty, with its next} \text{ best items by the quicksort} \cr 14 & \quad \textbf{while}\ |D| < m\ \textbf{and}\ \text{some buffer} \text{ has items }\ \textbf{do} \cr 15 & \qquad \text{move to } D \text{ the best first item of the} \text{ buffers, by a heap of them} \cr 16 & \qquad \text{refill that buffer from its source if it ran out} \end{array}} \]

Precondition
the heap is not empty

◆ push()

template<class T, class Better = std::less< T >>
void ADS::SequenceHeap< T, Better >::push ( const T & x)

pushes \( x \), in \( O( \log n ) \) amortized time

In pseudocode (Sanders, beyond the book), \( I \) the insertion heap, \( D \) the deletion buffer, \( G_i \) the group \( i \) with its buffer \( G_i.\mathit{buf} \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( x ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then}\ \text{open a bulk} \cr 3 & \quad n \gets n + 1 \cr 4 & \quad \textbf{if}\ \text{the bulk is open }\ \textbf{then} \ \text{append } x \text{ to it};\ \textbf{return} \cr 5 & \quad \textbf{if}\ |I| = m\ \textbf{then}\ \mathrm{flush}() \cr 6 & \quad \text{insert } x \text{ in } I \text{, moved up if } I \text{ is a heap} \cr 7 & \textbf{procedure}\ \mathrm{flush}() \cr 8 & \quad S \gets \text{the items of } I \text{, sorted};\ I \gets \emptyset \cr 9 & \quad \textbf{if}\ \text{the first of } S \text{ is not better } \text{than the last of } D \text{ and of } G_1.\mathit{buf} \ \textbf{then} \cr 10 & \qquad \mathrm{join}( 1 , S );\ \textbf{return} \cr 11 & \quad s \gets \text{the merge of } S \text{ with } D \text{ and } G_1.\mathit{buf} \cr 12 & \quad D \gets \text{the first } |D| \text{ items of } s; \ G_1.\mathit{buf} \gets \text{the next } |G_1.\mathit{buf}| \cr 13 & \quad \mathrm{join}( 1 , \text{the rest of } s ) \cr 14 & \textbf{procedure}\ \mathrm{join}( i , s ) \cr 15 & \quad \textbf{if}\ G_i \text{ has } k \text{ sequences} \ \textbf{then} \cr 16 & \qquad \textbf{if}\ \text{they hold at most half of their room } \textbf{then}\ \text{merge them into fewer full ones} \cr 17 & \qquad \textbf{else}\ \mathrm{join}( i + 1 , \text{the merge of} \text{ the sequences of } G_i \text{ and of } G_{i+1}.\mathit{buf} );\ G_i \gets \emptyset \cr 18 & \quad \text{add } s \text{ to the sequences of } G_i \end{array}} \]

◆ size()

template<class T, class Better = std::less< T >>
std::size_t ADS::SequenceHeap< T, Better >::size ( void ) const
inlinenodiscard

the number of items, \( n \)

◆ top()

template<class T, class Better = std::less< T >>
const T & ADS::SequenceHeap< T, Better >::top ( void )
nodiscard

the best item

Precondition
the heap is not empty

Member data documentation

◆ K

template<class T, class Better = std::less< T >>
std::size_t ADS::SequenceHeap< T, Better >::K = 16
staticconstexpr

the sequences of a group, \( k \)

◆ M

template<class T, class Better = std::less< T >>
std::size_t ADS::SequenceHeap< T, Better >::M = 4096
staticconstexpr

the items of the insertion heap and of a buffer, \( m \)


The documentation for this class was generated from the following file: