ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MultiwayMerge.h

The engineered merge of \( k \) sorted sequences, beyond the book. More...

Files

file  ADS/priority/MultiwayMerge.h
 The engineered merge of \( k \) sorted sequences, beyond the book [see LoserTree.h for the tree of losers of the notes]: FlatLoserTree, the tree of losers with the keys in its nodes, for the merges whose sequences arrive a block at a time, and multiwayMerge(), the merge of \( k \) sorted ranges in memory, by a funnel of binary mergers.

Classes

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 More...

Functions

template<class I, class O, class C = std::less<>>
O ADS::multiwayMerge (const std::vector< std::pair< I, I > > &R, O out, C less=C())
 the items of the sorted ranges \( R[ i ] = [ \mathit{first} , \mathit{last} ) \) merged into out, the range of lower index first on equal items; the end of the output

Detailed description

The engineered merge of \( k \) sorted sequences, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of priority queues.

Function documentation

◆ multiwayMerge()

template<class I, class O, class C = std::less<>>
O ADS::multiwayMerge ( const std::vector< std::pair< I, I > > & R,
O out,
C less = C() )

the items of the sorted ranges \( R[ i ] = [ \mathit{first} , \mathit{last} ) \) merged into out, the range of lower index first on equal items; the end of the output

In pseudocode (the lazy funnel of Brodal, Fagerberg and Vinther with binary mergers, beyond the book), each merger \( v \) of the tree with two children \( x \) and \( y \) and a buffer of 4 KiB, each source a leaf:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiwayMerge}( R , \mathit{out} ) \cr 2 & \quad \textbf{if}\ \text{one range has items }\ \textbf{then} \ \textbf{return}\ \text{its copy} \cr 3 & \quad \textbf{if}\ \text{two ranges have items }\ \textbf{then} \ \textbf{return}\ \text{their merge} \cr 4 & \quad \textbf{if not}\ \text{contiguous items copied as bytes} \ \textbf{then return}\ \text{the merge by a FlatLoserTree} \cr 5 & \quad \textbf{if}\ \text{3 or 4 ranges of items over 16 bytes} \ \textbf{then return}\ \text{the merge by the least heads of pairs} \cr 6 & \quad T \gets \text{a tree of mergers, the halves of the ranges} \text{ below each node} \cr 7 & \quad \mathrm{fill}( \text{the root of } T , \mathit{out} , n ); \ \textbf{return}\ \text{the end of } \mathit{out} \cr 8 & \textbf{procedure}\ \mathrm{fill}( v , o , c )\quad \triangleright\ \text{up to } c \text{ items of the children of } v \text{ to } o \cr 9 & \quad \textbf{while}\ c > 0\ \textbf{do} \cr 10 & \qquad \textbf{if}\ x \text{ or } y \text{ is exhausted, after} \text{ a refill}\ \textbf{then}\ \text{copy from the other one}; \ \textbf{return} \cr 11 & \qquad m \gets \min\{ |x| , |y| , c \}\quad \triangleright \ \text{the items left in the buffers} \cr 12 & \qquad \textbf{repeat}\ m\ \text{times: move to } o \text{ the least head of } x \text{ and } y \text{, by a mask} \cr 13 & \qquad c \gets c - m \cr 14 & \textbf{function}\ \mathrm{refill}( x )\quad \triangleright \ \text{when the buffer of } x \text{ runs out} \cr 15 & \quad \mathrm{fill}( x , \text{the buffer of } x , \text{its size} ) \end{array}} \]

Precondition
each range sorted by less (an audit)
Postcondition
the output sorted, with the items of the ranges (an audit)