|
ADS
Algorithms and Data Structures
|
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]. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/sort/Sorted.h"#include "ADS/sort/VectorQuickSort.h"#include <algorithm>#include <bit>#include <cstddef>#include <cstdint>#include <cstring>#include <memory>#include <new>#include <string>#include <string_view>#include <utility>#include <vector>#include "ADS/sort/impl/StringSort.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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: 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].
A group of \( n \) strings that agree on their first \( d \) characters is sorted in one step:
Each step reads the characters of each string of the group once, at the place of the string, in the order of the vector, and moves each string once, while a radix sort reads one character per pass and a comparison sort two strings per comparison, at random places of the memory once the strings have moved. A step that leaves a group of more than \( 3 / 4 \) of its strings counts against a budget of \( 2 \log_2 n \) such steps, past which a group is sorted by std::stable_sort() on the characters from \( d \): so the steps are \( O( \log n ) \) deep and they take \( O( D + n \log^2 n ) \) time, \( D \) the total length of the distinguishing prefixes, besides those sorts. The sort is stable, and takes \( n \) keys of 64 bits and the room of \( n \) strings besides the vector.
The strings ending inside a key are padded with 0, which a string holding the character 0 shares: such strings may agree on every key of every step, and a step that splits no group sends it to std::stable_sort() at once, which compares them whole.
The contracts [see Contract.h]: the vector ends with the same strings sorted, an audit. The function is defined in StringSort.tpp.