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

the tree of losers of \( k \) sources with the keys in its nodes, the winner the smallest key by less, the lower source on equal keys More...

#include <ADS/priority/MultiwayMerge.h>

Public member functions

 FlatLoserTree (std::size_t k, C less=C())
 the tree of \( k \) sources, all exhausted
std::size_t sources (void) const
 the number of sources, \( k \)
void start (std::size_t s, const T &x)
 before build(): the source \( s \) starts with the key \( x \), whose address is kept if T is large
void build (void)
 plays the matches of the keys given by start(), in \( O( K ) \) time
bool empty (void) const
 whether all the sources are exhausted
std::size_t winner (void) const
 the source with the smallest key
const T & top (void) const
 the key of the winner
void replaceTop (const T &x)
 the winner takes the key \( x \), and the matches on its path are replayed, \( \log_2 K \) matches
void exhaustTop (void)
 the winner is exhausted, and the matches on its path are replayed as by replaceTop(), with the largest key and the source marked as exhausted

Detailed description

template<class T, class C = std::less<>>
class ADS::FlatLoserTree< T, C >

the tree of losers of \( k \) sources with the keys in its nodes, the winner the smallest key by less, the lower source on equal keys

Constructor and destructor documentation

◆ FlatLoserTree()

template<class T, class C = std::less<>>
ADS::FlatLoserTree< T, C >::FlatLoserTree ( std::size_t k,
C less = C() )
explicit

the tree of \( k \) sources, all exhausted

Precondition
\( 1 \le k < 2^{31} \)

Member function documentation

◆ build()

template<class T, class C = std::less<>>
void ADS::FlatLoserTree< T, C >::build ( void )

plays the matches of the keys given by start(), in \( O( K ) \) time

In pseudocode (Knuth 5.4.1, beyond the book), the node \( v \) holding the key and the source of its loser, \( \mathit{leaf}[ s ] \) those of the source \( s \), the leaves beyond \( k \) exhausted:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}() \cr 2 & \quad \textbf{for}\ v \gets K - 1\ \textbf{downto}\ 1 \ \textbf{do} \cr 3 & \qquad a \gets \text{the winner below } 2 v \text{, or } \mathit{leaf}[ 2 v - K ] \text{ if } 2 v \ge K \cr 4 & \qquad b \gets \text{the winner below } 2 v + 1 \text{, or } \mathit{leaf}[ 2 v + 1 - K ] \text{ if } 2 v + 1 \ge K \cr 5 & \qquad \textbf{if}\ \mathrm{beats}( b , a )\ \textbf{then} \ \text{the winner below } v \gets b;\ N[ v ] \gets a \cr 6 & \qquad \textbf{else}\ \text{the winner below } v \gets a;\ N[ v ] \gets b \cr 7 & \quad N[ 0 ] \gets \text{the winner below } 1 \cr 8 & \textbf{function}\ \mathrm{beats}( a , b ) \cr 9 & \quad \textbf{return}\ ( a.\mathit{key} , a.\mathit{src} ) < ( b.\mathit{key} , b.\mathit{src} ) \text{ lexicographically, an} \text{ exhausted source last} \end{array}} \]

◆ empty()

template<class T, class C = std::less<>>
bool ADS::FlatLoserTree< T, C >::empty ( void ) const
nodiscard

whether all the sources are exhausted

◆ exhaustTop()

template<class T, class C = std::less<>>
void ADS::FlatLoserTree< T, C >::exhaustTop ( void )

the winner is exhausted, and the matches on its path are replayed as by replaceTop(), with the largest key and the source marked as exhausted

Precondition
not empty()

◆ replaceTop()

template<class T, class C = std::less<>>
void ADS::FlatLoserTree< T, C >::replaceTop ( const T & x)

the winner takes the key \( x \), and the matches on its path are replayed, \( \log_2 K \) matches

In pseudocode (Knuth 5.4.1, beyond the book), with \( \mathrm{beats}() \) of build() and the swaps made by masks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{replaceTop}( x ) \cr 2 & \quad c \gets ( x , N[ 0 ].\mathit{src} );\ v \gets \lfloor ( K + c.\mathit{src} ) / 2 \rfloor \cr 3 & \quad \textbf{while}\ v > 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathrm{beats}( N[ v ] , c )\ \textbf{then} \ \text{swap } N[ v ] \text{ and } c \cr 5 & \qquad v \gets \lfloor v / 2 \rfloor \cr 6 & \quad N[ 0 ] \gets c \end{array}} \]

Precondition
not empty()

◆ sources()

template<class T, class C = std::less<>>
std::size_t ADS::FlatLoserTree< T, C >::sources ( void ) const
nodiscard

the number of sources, \( k \)

◆ start()

template<class T, class C = std::less<>>
void ADS::FlatLoserTree< T, C >::start ( std::size_t s,
const T & x )

before build(): the source \( s \) starts with the key \( x \), whose address is kept if T is large

Precondition
\( s < k \)

◆ top()

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

the key of the winner

Precondition
not empty()

◆ winner()

template<class T, class C = std::less<>>
std::size_t ADS::FlatLoserTree< T, C >::winner ( void ) const
nodiscard

the source with the smallest key

Precondition
not empty()

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