|
ADS
Algorithms and Data Structures
|
The sort of strings by integer keys of their prefixes, beyond the book. More...
Files | |
| file | ADS/sort/StringSort.h |
| The sort of strings by integer keys of their prefixes, beyond the book: prefixKeySort() reads each string a few times in all, instead of once per pass or per comparison, and leaves the work to the quicksort of integers in the vector registers [see VectorQuickSort.h]. | |
Functions | |
| template<StringStore Store> | |
| void | ADS::prefixKeySort (Store &&A) |
| the stable sort of strings by integer keys of their prefixes | |
| void | ADS::prefixKeySort (std::vector< std::string > &A) |
| prefixKeySort() above on a vector | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { prefixKeySort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::prefixKeySort (R &&A, P &&... p) |
| prefixKeySort() 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 | |
The sort of strings by integer keys of their prefixes, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of sorting.
| decltype(auto) ADS::prefixKeySort | ( | R && | A, |
| P &&... | p ) |
prefixKeySort() 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
|
inline |
prefixKeySort() above on a vector
| void ADS::prefixKeySort | ( | Store && | A | ) |
the stable sort of strings by integer keys of their prefixes
In pseudocode (after the radix sorts of strings of Kärkkäinen and Rantala and of Bingmann and Sanders, beyond the book), the strings of a group \( G \) agreeing on their first \( d \) characters:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{step}( G , d ) \cr 2 & \quad \textbf{if}\ |G| \le 8\ \textbf{then}\ \text{sort } G \text{ by } \text{insertion};\ \textbf{return} \cr 3 & \quad d \gets d + \text{the prefix that the strings of } G \text{ } \text{share from } d \cr 4 & \quad \textbf{if}\ \text{they are all equal}\ \textbf{then return} \cr 5 & \quad b \gets \lceil \log_2 |G| \rceil \cr 6 & \quad \textbf{foreach}\ i \in G\ \textbf{do}\ k_i \gets \lfloor w_i / 2^b \rfloor 2^b + i \cr 7 & \quad \mathrm{vectorQuickSort}( k );\ \text{move the strings to the } \text{order of } k \cr 8 & \quad \textbf{foreach}\ \text{run } G' \text{ of equal } \lfloor k_i / 2^b \rfloor\ \textbf{do} \cr 9 & \qquad \mathrm{step}( G' , d + \lfloor ( 64 - b ) / 8 \rfloor ) \cr 10 & \textbf{procedure}\ \mathrm{prefixKeySort}( A ) \cr 11 & \quad \mathrm{step}( A , 0 ) \end{array}} \]
where a group of a step that keeps more than \( 3 / 4 \) of the strings, past \( 2 \log_2 n \) such steps, or one that splits nothing, is left to std::stable_sort().