ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FrontCoding.h File Reference

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

Detailed description

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:

  • bucketed() is the front coding with bucketing \( \mathit{FC}_B \) of the same chapter: an anchor every \( b \) strings, i.e., buckets of \( b \) strings coded on their own; a larger \( b \) compresses better and scans more (up to \( \Theta( b^2 ) \) characters for a bucket like \( a , aa , aaa , \ldots \));
  • localityPreserving() is the locality-preserving front coding of Bender, Farach-Colton and Kuszmaul: \( s_i \) is front coded only if the anchor before it begins within the last \( c | s_i | \) characters of the coding, else it is stored whole; a string is then decoded by reading at most \( ( c + 1 ) | s_i | \) characters, the optimal \( O( | s_i | / B ) \) transfers of blocks, and the coding takes at most \( ( 1 + 2 / ( c - 2 ) ) \, \mathit{FC}( D ) \) characters for \( c > 2 \), \( \mathit{FC}( D ) \) being those of the front coding (each string stored whole is paid for by the \( c | s_i | \) characters coded since the anchor before it, as Bender, Farach-Colton and Kuszmaul prove): the time controls the space.

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.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 9, Searching Strings by Prefix, doi:10.1017/9781009128933
I. H. Witten, A. Moffat, T. C. Bell, Managing Gigabytes, 2nd ed., Morgan Kaufmann, 1999 (the front coding of a lexicon)
M. A. Bender, M. Farach-Colton, B. C. Kuszmaul, Cache-oblivious string B-trees, Proceedings of PODS 2006, 233-242, 2006, doi:10.1145/1142351.1142385
P. Ferragina, R. Grossi, The string B-tree: a new data structure for string search in external memory and its applications, Journal of the ACM 46(2), 236-280, 1999, doi:10.1145/301970.301973
D. R. Morrison, PATRICIA - practical algorithm to retrieve information coded in alphanumeric, Journal of the ACM 15(4), 514-534, 1968, doi:10.1145/321479.321481
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa