|
ADS
Algorithms and Data Structures
|
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 | |
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
|
explicit |
the tree of \( k \) sources, all exhausted
| 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}} \]
|
nodiscard |
whether all the sources are exhausted
| 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
| 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}} \]
|
nodiscard |
the number of sources, \( k \)
| 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
|
nodiscard |
the key of the winner
|
nodiscard |
the source with the smallest key