ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::RearCodedDictionary Class Reference

a sorted dictionary of strings in the rear coding with buckets, in one string of bytes More...

#include <ADS/string/RearCoding.h>

Public member functions

 RearCodedDictionary (void)=default
 the empty dictionary
 RearCodedDictionary (const std::vector< std::string > &D, std::size_t b=16)
 the rear coding of \( D \) in buckets of \( b \) strings
std::size_t size (void) const
 the number \( n \) of strings
bool empty (void) const
 whether there is no string
std::size_t bucketSize (void) const
 the strings of a bucket, \( b \)
std::string access (std::size_t i) const
 the string \( i \), decoded from the first string of its bucket
std::size_t lookup (std::string_view Q) const
 the place of \( Q \), size() if it is not a string of the dictionary, by the search of lexicographicPosition(), which tells whether the string where it stops is \( Q \)
std::size_t lexicographicPosition (std::string_view Q) const
 the number of strings smaller than \( Q \)
std::size_t prefixEnd (std::string_view P) const
 the number of strings smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by it, by the search of lexicographicPosition() with that order
std::pair< std::size_t, std::size_t > prefixRange (std::string_view P) const
 the range \( [ l , r ) \) of the strings prefixed by \( P \), by lexicographicPosition() and prefixEnd()
std::size_t codeBytes (void) const
 the bytes of the coding, \( \mathit{RC}_H( D ) \) with the first strings and the lengths
std::size_t bytes (void) const
 all the bytes: the coding, the places and the samples of the buckets, and the codes with their tables

Detailed description

a sorted dictionary of strings in the rear coding with buckets, in one string of bytes

Constructor and destructor documentation

◆ RearCodedDictionary() [1/2]

ADS::RearCodedDictionary::RearCodedDictionary ( void )
default

the empty dictionary

◆ RearCodedDictionary() [2/2]

ADS::RearCodedDictionary::RearCodedDictionary ( const std::vector< std::string > & D,
std::size_t b = 16 )
explicit

the rear coding of \( D \) in buckets of \( b \) strings

In pseudocode (Martínez-Prieto et al. in the rear coding, beyond the book), \( \mathrm{code}( v ) \) the codeword of \( v \) in its code of Huffman, built first on the frequencies of the characters, of the rears and of the lengths:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( D , b ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 3 & \qquad \textbf{if}\ i \bmod b = 0\ \textbf{then} \cr 4 & \qquad\quad \text{append the length of } C \text{ to } P; \ \text{append } \mathrm{code}( | s_i | ) \cdot \mathrm{code}( s_i[ g + 16 .. | s_i | - 1 ] ) \text{ to } C \cr & \qquad\quad \triangleright\ g \text{ the prefix that all the } \text{strings share} \cr 5 & \qquad \textbf{else} \cr 6 & \qquad\quad \ell \gets \mathrm{commonPrefixLength}( s_{i-1} , s_i ) \cr 7 & \qquad\quad \text{append } \mathrm{code}( | s_{i-1} | - \ell ) \cdot \mathrm{code}( | s_i | - \ell ) \cdot \mathrm{code}( s_i[ \ell .. | s_i | - 1 ] ) \text{ to } C \cr 8 & \quad \text{keep } P \text{ by Elias-Fano, and the 16 bytes after } \text{the common prefix of each first string} \end{array}} \]

Precondition
\( D \) is sorted (audit), its strings shorter than \( 2^{30} \) bytes, and \( b \ge 1 \)

Member function documentation

◆ access()

std::string ADS::RearCodedDictionary::access ( std::size_t i) const
nodiscard

the string \( i \), decoded from the first string of its bucket

In pseudocode (Martínez-Prieto et al. in the rear coding, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{access}( i ) \cr 2 & \quad s \gets \text{the first string of the bucket } \lfloor i / b \rfloor \text{, from the place } P[ \lfloor i / b \rfloor ] \text{ of } C \cr 3 & \quad \textbf{for}\ j \gets b \lfloor i / b \rfloor + 1\ \textbf{to} \ i\ \textbf{do}\ s \gets s[ 0 .. |s| - r_j - 1 ] \cdot \hat s_j \cr 4 & \quad \textbf{return}\ s \end{array}} \]

Precondition
\( i < \) size()

◆ bucketSize()

std::size_t ADS::RearCodedDictionary::bucketSize ( void ) const
nodiscard

the strings of a bucket, \( b \)

◆ bytes()

std::size_t ADS::RearCodedDictionary::bytes ( void ) const
nodiscard

all the bytes: the coding, the places and the samples of the buckets, and the codes with their tables

◆ codeBytes()

std::size_t ADS::RearCodedDictionary::codeBytes ( void ) const
nodiscard

the bytes of the coding, \( \mathit{RC}_H( D ) \) with the first strings and the lengths

◆ empty()

bool ADS::RearCodedDictionary::empty ( void ) const
nodiscard

whether there is no string

◆ lexicographicPosition()

std::size_t ADS::RearCodedDictionary::lexicographicPosition ( std::string_view Q) const
nodiscard

the number of strings smaller than \( Q \)

In pseudocode (the two-level search of chapter 9, Searching Strings by Prefix, of Ferragina's book, in the rear coding), \( \lambda \) the prefix shared by \( s \) and \( Q \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lexicographicPosition}( Q ) \cr 2 & \quad k \gets \text{the buckets whose first string is smaller than } Q\text{, by a binary search} \cr 3 & \quad \textbf{if}\ k = 0\ \textbf{then return}\ 0 \cr 4 & \quad i \gets b ( k - 1 );\ s \gets \text{the first string of the } \text{bucket } k - 1;\ \lambda \gets \mathrm{commonPrefixLength}( s , Q ) \cr 5 & \quad \textbf{while}\ i + 1 < \min\{ b k , n \}\ \textbf{do}\quad \triangleright\ s < Q \text{ here} \cr 6 & \qquad \kappa \gets | s | - r_{i+1};\ s \gets s[ 0 .. \kappa - 1 ] \cdot \hat s_{i+1} \cr 7 & \qquad \textbf{if}\ \kappa \le \lambda\ \textbf{then}\ \lambda \gets \kappa + \mathrm{commonPrefixLength}( \hat s_{i+1} , Q[ \kappa .. ] ) \cr 8 & \qquad\quad \textbf{if}\ s \ge Q\ \textbf{then return}\ i + 1\quad \triangleright\ \text{by the character after } \lambda \cr 9 & \qquad i \gets i + 1\quad \triangleright\ \kappa > \lambda \text{: } s \text{ still smaller than } Q \cr 10 & \quad \textbf{return}\ \min\{ b k , n \} \end{array}} \]

◆ lookup()

std::size_t ADS::RearCodedDictionary::lookup ( std::string_view Q) const
nodiscard

the place of \( Q \), size() if it is not a string of the dictionary, by the search of lexicographicPosition(), which tells whether the string where it stops is \( Q \)

◆ prefixEnd()

std::size_t ADS::RearCodedDictionary::prefixEnd ( std::string_view P) const
nodiscard

the number of strings smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by it, by the search of lexicographicPosition() with that order

◆ prefixRange()

std::pair< std::size_t, std::size_t > ADS::RearCodedDictionary::prefixRange ( std::string_view P) const
nodiscard

the range \( [ l , r ) \) of the strings prefixed by \( P \), by lexicographicPosition() and prefixEnd()

◆ size()

std::size_t ADS::RearCodedDictionary::size ( void ) const
nodiscard

the number \( n \) of strings


The documentation for this class was generated from the following file: