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

The front coding of a sorted dictionary of strings, beyond the book. More...

Files

file  ADS/string/FrontCoding.h
 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 \).

Classes

class  ADS::FrontCodedDictionary
 a sorted dictionary of strings in the front coding, with anchors More...

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.

The definitions, the invariants and the costs of the area are in Theory of strings.

Typedef documentation

◆ FrontPair

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

Function documentation

◆ decodeLength()

std::uint32_t ADS::decodeLength ( std::string_view in,
std::size_t & pos )
inlinenodiscard

the length coded at in[ pos ], with pos moved past its code

In pseudocode (the byte-aligned code of chapter 9, Searching Strings by Prefix, of Ferragina's book, beyond the book), \( x \) the first byte:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decodeLength}( \mathit{in} , \mathit{pos} ) \cr 2 & \quad x \gets \mathit{in}[ \mathit{pos} ];\ k \gets \lfloor x / 64 \rfloor + 1;\ v \gets x \bmod 64 \cr 3 & \quad \textbf{for}\ j \gets 1\ \textbf{to}\ k - 1\ \textbf{do}\ v \gets 256 v + \mathit{in}[ \mathit{pos} + j ] \cr 4 & \quad \mathit{pos} \gets \mathit{pos} + k;\ \textbf{return}\ v \end{array}} \]

Precondition
a code begins at pos and is within in

◆ encodeLength()

void ADS::encodeLength ( std::uint32_t l,
std::string & out )
inline

appends to out the code of \( \ell \) in 1 to 4 bytes, the first two bits giving the number of bytes minus 1

In pseudocode (the byte-aligned code of chapter 9, Searching Strings by Prefix, of Ferragina's book, beyond the book), the most significant byte first:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{encodeLength}( \ell , \mathit{out} ) \cr 2 & \quad k \gets \min\{ k \ge 1 : \ell < 2^{8 k - 2} \text{ or } k = 4 \} \cr 3 & \quad v \gets ( k - 1 ) \cdot 2^{8 k - 2} + \ell \cr 4 & \quad \textbf{foreach}\ j = k - 1 , \ldots , 0\ \textbf{do} \ \text{append the byte } \lfloor v / 2^{8 j} \rfloor \bmod 256 \text{ to } \mathit{out} \end{array}} \]

Precondition
\( \ell < 2^{30} \)
Postcondition
decodeLength() reads \( \ell \) back (audit)

◆ frontCode()

std::vector< FrontPair > ADS::frontCode ( const std::vector< std::string > & D)
inlinenodiscard

the front coding of \( D \), a pair per string

In pseudocode (the front coding of chapter 9, Searching Strings by Prefix, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{frontCode}( D ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 3 & \qquad \ell \gets 0 \text{ if } i = 0 \text{, else } \mathrm{commonPrefixLength}( s_{i-1} , s_i ) \cr 4 & \qquad F[ i ] \gets ( \ell , s_i[ \ell .. |s_i| - 1 ] ) \cr 5 & \quad \textbf{return}\ F \end{array}} \]

Postcondition
frontDecode() of the result is \( D \) (audit)

◆ frontDecode()

std::vector< std::string > ADS::frontDecode ( const std::vector< FrontPair > & F)
inlinenodiscard

the strings of the front coding \( F \)

In pseudocode (the front coding of chapter 9, Searching Strings by Prefix, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{frontDecode}( F ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ |F| - 1\ \textbf{do} \cr 3 & \qquad ( \ell , \hat s ) \gets F[ i ] \cr 4 & \qquad s_i \gets s_{i-1}[ 0 .. \ell - 1 ] \cdot \hat s\quad \triangleright\ \text{the empty prefix for } i = 0 \cr 5 & \quad \textbf{return}\ ( s_0 , \ldots , s_{|F| - 1} ) \end{array}} \]

Precondition
the first pair has \( \ell = 0 \), and each \( \ell \) is at most the length of the string before