|
ADS
Algorithms and Data Structures
|
The rear coding of a sorted dictionary of strings, beyond the book, and a compact dictionary on it (see Dictionaries searched by prefix): the front coding [see FrontCoding.h] writes the string \( s_i \) as the length \( \ell_i \) of the prefix it shares with \( s_{i-1} \) and the rest \( \hat s_i \); the rear coding writes instead \( r_i = | s_{i-1} | - \ell_i \), the characters to take off the end of \( s_{i-1} \) before appending \( \hat s_i \). More...
#include "ADS/Contract.h"#include "ADS/compression/HuffmanCoder.h"#include "ADS/memory/Prefetch.h"#include "ADS/string/FrontCoding.h"#include "ADS/succinct/BitVector.h"#include "ADS/succinct/EliasFano.h"#include <array>#include <cstddef>#include <cstdint>#include <string>#include <string_view>#include <utility>#include <vector>#include "ADS/string/impl/RearCoding.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RearCodedDictionary |
| a sorted dictionary of strings in the rear coding with buckets, in one string of bytes More... | |
Namespaces | |
| namespace | ADS |
Typedefs | |
| using | ADS::RearPair = std::pair< std::size_t , std::string > |
| a pair \( ( r , \hat s ) \) of the rear coding, the characters taken off the end of the string before and the rest | |
Functions | |
| std::vector< RearPair > | ADS::rearCode (const std::vector< std::string > &D) |
| the rear coding of \( D \), a pair per string | |
| std::vector< std::string > | ADS::rearDecode (const std::vector< RearPair > &R) |
| the strings of the rear coding \( R \) | |
The rear coding of a sorted dictionary of strings, beyond the book, and a compact dictionary on it (see Dictionaries searched by prefix): the front coding [see FrontCoding.h] writes the string \( s_i \) as the length \( \ell_i \) of the prefix it shares with \( s_{i-1} \) and the rest \( \hat s_i \); the rear coding writes instead \( r_i = | s_{i-1} | - \ell_i \), the characters to take off the end of \( s_{i-1} \) before appending \( \hat s_i \).
Both decode a string from the one before, and they carry the same information; but \( r_i \) is the smaller number where the strings share long prefixes (the URLs, the paths, the words of a language), and a variable-length code of it takes fewer bytes. The rear coding is the variant of the front coding on which Ferragina, Grossi, Gupta, Shah and Vitter build their compressed dictionaries of strings for the external memory.
RearCodedDictionary is the front coding with buckets of the practical compressed string dictionaries of Martínez-Prieto, Brisaboa, Cánovas, Claude and Navarro, in the rear coding and with its characters entropy-coded as in their HTFC, by a code of Huffman instead of their code of Hu and Tucker (whose order the search here does not need, since it decodes the strings it compares): one string of bits in which each bucket of \( b \) strings begins with its first string (its length, then its characters past the prefix that all the strings share and past the 16 bytes after it, which are kept apart in two words) and goes on with a triple \( ( r_i , | \hat s_i | , \hat s_i ) \) per string, the characters in a canonical code of Huffman of their frequencies, the rears and the lengths in two more, of their own frequencies (255 escaping the larger values, written in 30 bits), all the codewords of at most 11 bits [see limitedHuffmanLengths()], so that a table of \( 2^{11} \) entries decodes a symbol by one read of 11 bits, and one of \( 2^{12} \) entries up to 3 characters by one of 12; the places of the buckets in the string are kept by Elias-Fano [see EliasFano.h]. With \( \mathit{RC}_H( D ) \) the bits of the coding, within one bit per symbol of the entropy of each of the three streams, the dictionary takes
\[ \mathit{RC}_H( D ) + \frac{n}{b} \Big( 130 + \Big\lceil \log_2 \frac{b \, \mathit{RC}_H( D )}{n} \Big\rceil \Big) \text{ bits} \]
besides the directory of Elias-Fano and the three tables of decoding, the 128 bits being those of the sample of each bucket below.
A search is a binary search on the first strings of the buckets and the scan of one bucket. The binary search reads, for each bucket, the 16 bytes of its first string kept apart, and decodes the rest of the first string only when those bytes do not decide, and then only up to the first character where it differs from the pattern. In the scan no string is rebuilt: each one keeps the first \( | s_{i-1} | - r_i \) characters of the one before, its rest is decoded while it is compared with the pattern \( Q \), and the comparison resumes from where it stopped: with \( \lambda \) the prefix that the current string shares with \( Q \), a next string that keeps more than \( \lambda \) characters has the same character as the current one where it differs from \( Q \), hence the same order with respect to \( Q \), and is passed with no comparison; one that keeps fewer is compared from its rest. A search costs \( O( | Q | \log( n / b ) ) \) for the binary search and \( O( | Q | + \sum_i | \hat s_i | ) \) for the bucket.
The contracts [see Contract.h]: the dictionary sorted, an audit, the strings and the coding shorter than \( 2^{30} \) and \( 2^{63} \) bytes, \( b \ge 1 \), and a pair of rearDecode() taking at most the characters of the string before; the decoding of the coding gives the strings back, an audit. The functions are defined in RearCoding.tpp.