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

The learned sort, beyond the book. More...

Files

file  ADS/sort/LearnedSort.h
 The learned sort, beyond the book: learnedSort() distributes the numbers of 32 or 64 bits into buckets by a model of the cumulative distribution function (CDF) of their keys, learned from a sample, and sorts each bucket by vectorQuickSort() [see VectorQuickSort.h]; it falls back to sampleSort() [see SampleSort.h] when the sample shows a distribution that the model does not fit.

Functions

template<SortStore Store, VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::learnedSort (Store &&A)
 the learned sort of \( A \) in the natural order (the total order of IEEE 754 for the floating point numbers): the buckets of a model of the CDF of a sample, each sorted by vectorQuickSort(), and sampleSort() when the sample shows a distribution that the model does not fit; not stable
template<VectorKey T>
void ADS::learnedSort (std::vector< T > &A)
 learnedSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { learnedSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::learnedSort (R &&A, P &&... p)
 learnedSort() on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses

Detailed description

The learned sort, beyond the book.

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

Function documentation

◆ learnedSort() [1/3]

template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { learnedSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::learnedSort ( R && A,
P &&... p )

learnedSort() on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses

◆ learnedSort() [2/3]

template<VectorKey T>
void ADS::learnedSort ( std::vector< T > & A)

learnedSort() above on a vector

◆ learnedSort() [3/3]

template<SortStore Store, VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::learnedSort ( Store && A)

the learned sort of \( A \) in the natural order (the total order of IEEE 754 for the floating point numbers): the buckets of a model of the CDF of a sample, each sorted by vectorQuickSort(), and sampleSort() when the sample shows a distribution that the model does not fit; not stable

In pseudocode (the learned sort of Kristo et al., with the model of the grid, beyond the book), \( b = 4096 \) the items that a bucket aims at and \( c_{\max} = 4 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{learnedSort}( A[ 0 .. n - 1 ] ) \cr 2 & \quad \textbf{if}\ n < 4 b\ \textbf{then}\ \mathrm{sampleSort}( A );\ \textbf{return} \cr 3 & \quad k \gets \min( \max( 2^{\lfloor \log_2( n / b ) \rfloor} , 64 ) , 2048 ) \cr 4 & \quad S \gets \text{the sorted keys of } \min( n , 16 k ) \text{ } \text{items of } A \text{ at random} \cr 5 & \quad t \gets \text{the distinct keys among } S[ i |S| / k ] \text{ with } 1 \le i < k \cr 6 & \quad \textbf{for}\ g \gets \log_2 k + 2\ \textbf{to}\ \log_2 k + 6 \ \textbf{do} \cr 7 & \qquad w \gets 2^{\max( 0 , \lceil \log_2( S_{\max} - S_{\min} ) \rceil - g )} \cr 8 & \qquad \mathit{first}[ \gamma ] \gets |\{ j : \lfloor ( t_j - S_{\min} ) / w \rfloor < \gamma \}| \text{ for each cell } \gamma \cr 9 & \qquad \textbf{if}\ \text{no cell holds more than } c_{\max} \text{ } \text{splitters}\ \textbf{then}\ \textbf{break} \cr 10 & \quad \textbf{if}\ \text{the model does not fit}\ \textbf{or}\ |t| < k / 2\ \textbf{then} \cr 11 & \qquad \mathrm{sampleSort}( A );\ \textbf{return} \cr 12 & \quad \textbf{foreach}\ x \in A\ \textbf{do}\quad \triangleright \ \text{one pass, the buckets counted} \cr 13 & \qquad \gamma \gets \text{the cell of } x \cr 14 & \qquad \beta( x ) \gets \mathit{first}[ \gamma ] + |\{ 0 \le i < c_{\max} : t_{\mathit{first}[ \gamma ] + i} < x \}| \cr 15 & \quad \text{move the items to a buffer by buckets, stably, and } \text{back to } A \cr 16 & \quad \textbf{foreach}\ \text{bucket } \beta\ \textbf{do} \ \mathrm{vectorQuickSort}( \beta ) \end{array}} \]

The splitters are padded by \( c_{\max} \) copies of the greatest key, since line 14 reads past the last cell, and a splitter of a later cell is greater than \( x \), so that the count is exact. If the sample repeats a splitter, the bucket of line 14 becomes \( 2 \beta + [ t_\beta = x ] \), the odd buckets holding the keys equal to a splitter, which are not sorted.

Postcondition
\( A \) is sorted, with the same items (audit)