|
ADS
Algorithms and Data Structures
|
The front coding of a sorted dictionary of strings, beyond the book: adjacent strings share a prefix, and frontCode() writes the string \( s_i \) as the pair \( ( \ell_i , \hat s_i ) \), \( \ell_i \) being the length of the longest common prefix of \( s_i \) and \( s_{i-1} \) (0 for the first one) and \( \hat s_i \) the rest of \( s_i \); frontDecode() copies \( \ell_i \) characters of the string before and appends \( \hat s_i \). More...
#include "ADS/Contract.h"#include "ADS/string/PrefixSearch.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <iterator>#include <string>#include <string_view>#include <utility>#include <vector>#include "ADS/string/impl/FrontCoding.tpp"Go to the source code of this file.
Classes | |
| class | ADS::FrontCodedDictionary |
| a sorted dictionary of strings in the front coding, with anchors More... | |
| class | ADS::FrontCodedDictionary::const_iterator |
| a constant view of the strings in their order, decoded one after the other, forward; *it is the string decoded in the iterator More... | |
Namespaces | |
| namespace | ADS |
Typedefs | |
| using | ADS::FrontPair = std::pair< std::size_t , std::string > |
| a pair \( ( \ell , \hat s ) \) of the front coding, the length of the prefix shared with the string before and the rest | |
Functions | |
| std::vector< FrontPair > | ADS::frontCode (const std::vector< std::string > &D) |
| the front coding of \( D \), a pair per string | |
| std::vector< std::string > | ADS::frontDecode (const std::vector< FrontPair > &F) |
| the strings of the front coding \( F \) | |
| void | ADS::encodeLength (std::uint32_t l, std::string &out) |
| appends to out the code of \( \ell \) in 1 to 4 bytes, the first two bits giving the number of bytes minus 1 | |
| std::uint32_t | ADS::decodeLength (std::string_view in, std::size_t &pos) |
| the length coded at in[ pos ], with pos moved past its code | |
The front coding of a sorted dictionary of strings, beyond the book: adjacent strings share a prefix, and frontCode() writes the string \( s_i \) as the pair \( ( \ell_i , \hat s_i ) \), \( \ell_i \) being the length of the longest common prefix of \( s_i \) and \( s_{i-1} \) (0 for the first one) and \( \hat s_i \) the rest of \( s_i \); frontDecode() copies \( \ell_i \) characters of the string before and appends \( \hat s_i \).
The length goes in the byte-aligned code of chapter 9, Searching Strings by Prefix, of Ferragina's book [see encodeLength()]: the binary of \( \ell \) padded to a whole number of bytes, the first two bits of the padding giving the number of bytes, so that \( \ell < 2^{30} \) takes 1 to 4 bytes instead of the \( \ell \) characters of the prefix. On the dictionaries of URLs the shared prefixes are up to 70% of the characters.
A string is decoded from the last one stored whole, and with no more of them the decoding of \( s_i \) may scan the whole dictionary before it; FrontCodedDictionary stores some strings whole (the anchors) and decodes a string from the anchor before it, copying only the \( \ell \) characters that each pair takes from the previous one:
Both search by prefix as the two-level scheme of chapter 9, Searching Strings by Prefix, of Ferragina's book: a binary search on the anchors, which are whole, and the scan of the one bucket where the pattern falls, decoded on the fly; the binary search may be replaced by a Patricia trie on the anchors [see TwoLevelDictionary.h]. The string B-tree of Ferragina and Grossi and its cache-oblivious version by Bender, Farach-Colton and Kuszmaul, which also works on the locality-preserving coding, bring the search in external memory to \( O( ( p + N_{occ} ) / B + \log_B n ) \) transfers.
The contracts [see Contract.h]: \( \ell < 2^{30} \) for encodeLength(), a pair that takes at most the characters of the string before for frontDecode(), the dictionary sorted for FrontCodedDictionary (an audit), \( b \ge 1 \) and \( c > 0 \); the decoding of the coding gives the strings back, an audit, and the locality-preserving coding is within its bounds of space and time, a postcondition for \( c > 2 \). The functions are defined in FrontCoding.tpp.