|
ADS
Algorithms and Data Structures
|
The sort of a file larger than the memory, beyond the book, as Knuth describes it for tapes, here on sequences in memory that play the files. More...
Files | |
| file | ADS/sort/ExternalSort.h |
| The sort of a file larger than the memory, beyond the book, as Knuth describes it for tapes, here on sequences in memory that play the files: | |
Functions | |
| template<class T, class C = std::less<>> | |
| std::vector< std::vector< T > > | ADS::replacementSelection (const std::vector< T > &input, std::size_t M, C less=C()) |
| the runs of the input made by replacement selection with \( M \) items in memory | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::polyphaseMerge (std::vector< std::vector< T > > runs, std::size_t tapes, C less=C(), std::size_t *phases=nullptr) |
| the polyphase merge of the runs on \( T \) tapes; the phases go to *phases, if given | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::externalSort (const std::vector< T > &input, std::size_t M, std::size_t tapes, C less=C()) |
| the sort of input by replacementSelection() and then polyphaseMerge() | |
The sort of a file larger than the memory, beyond the book, as Knuth describes it for tapes, here on sequences in memory that play the files.
The definitions, the invariants and the costs of the area are in Theory of sorting.
| std::vector< T > ADS::externalSort | ( | const std::vector< T > & | input, |
| std::size_t | M, | ||
| std::size_t | tapes, | ||
| C | less = C() ) |
the sort of input by replacementSelection() and then polyphaseMerge()
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{externalSort}( I , M , T ) \cr 2 & \quad \textbf{return}\ \mathrm{polyphaseMerge}( \mathrm{replacementSelection}( I , M ) , T ) \end{array}} \]
| std::vector< T > ADS::polyphaseMerge | ( | std::vector< std::vector< T > > | runs, |
| std::size_t | tapes, | ||
| C | less = C(), | ||
| std::size_t * | phases = nullptr ) |
the polyphase merge of the runs on \( T \) tapes; the phases go to *phases, if given
In pseudocode (Algorithm 5.4.2D of Knuth, beyond the book), the perfect distribution of a level, \( a_1 \ge \cdots \ge a_{T-1} \), giving that of the next level as \( a'_i = a_1 + a_{i+1} \), with \( a_T = 0 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{polyphaseMerge}( R , T ) \cr 2 & \quad a \gets ( 1 , \ldots , 1 ) \cr 3 & \quad \textbf{while}\ \sum_i a_i < |R|\ \textbf{do}\ a \gets \text{the next level of } a \cr 4 & \quad \text{deal } a_i \text{ runs to the tape } i \text{, the runs } \text{of } R \text{ first, then empty ones} \cr 5 & \quad o \gets T \quad \triangleright\ \text{the output tape} \cr 6 & \quad \textbf{while}\ \text{more than one run is left}\ \textbf{do} \cr 7 & \qquad k \gets \text{the fewest runs on an input tape} \cr 8 & \qquad \textbf{repeat}\ k \text{ times}\ \text{merge the first run of } \text{each input tape onto } o \cr 9 & \qquad o \gets \text{an input tape left empty} \cr 10 & \quad \textbf{return}\ \text{the run left} \end{array}} \]
| std::vector< std::vector< T > > ADS::replacementSelection | ( | const std::vector< T > & | input, |
| std::size_t | M, | ||
| C | less = C() ) |
the runs of the input made by replacement selection with \( M \) items in memory
In pseudocode (section 5.4.1 of Knuth, beyond the book), a heap of pairs \( ( \mathit{run} , x ) \) ordered by the run and then by the item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{replacementSelection}( I , M ) \cr 2 & \quad H \gets \text{a heap of } ( 0 , x ) \text{ for the first } M \text{ items } x \text{ of } I \cr 3 & \quad R \gets \text{an empty sequence of runs} \cr 4 & \quad \textbf{while}\ H \ne \emptyset\ \textbf{do} \cr 5 & \qquad ( r , x ) \gets \mathrm{deleteMin}( H ) \cr 6 & \qquad \textbf{if}\ r = |R|\ \textbf{then}\ \text{append an empty run } \text{to } R \cr 7 & \qquad \textbf{if}\ I \text{ has an item } y \text{ left} \ \textbf{then} \cr 8 & \qquad\quad \textbf{if}\ y < x\ \textbf{then}\ \mathrm{insert}( H , ( r + 1 , y ) )\ \textbf{else}\ \mathrm{insert}( H , ( r , y ) ) \cr 9 & \qquad \text{append } x \text{ to the last run of } R \cr 10 & \quad \textbf{return}\ R \end{array}} \]