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

The compressors of Lempel and Ziv (chapter 13, Dictionary-Based Compressors, of Ferragina's book, beyond the book of Montresor), whose dictionary is the text itself. More...

Files

file  ADS/compression/LempelZiv.h
 The compressors of Lempel and Ziv (chapter 13, Dictionary-Based Compressors, of Ferragina's book, beyond the book of Montresor), whose dictionary is the text itself: they replace a string seen before with a reference to it, and need no model of the source.

Classes

struct  ADS::LZ77Phrase
 a phrase of LZ77: copy length symbols from distance back, then next (none at the end of the text); distance is 0 when length is 0 More...
struct  ADS::LZSSPhrase
 a phrase of LZSS: a copy of length symbols from distance back, or the literal when distance is 0 More...
struct  ADS::LZ78Phrase
 a phrase of LZ78: the phrase id followed by next More...

Functions

std::vector< LZ77Phrase > ADS::lz77Parse (std::string_view s, std::size_t w=std::numeric_limits< std::size_t >::max())
 the parse of LZ77 of \( s \) with a window of \( w \) symbols, the closest among the longest copies
std::string ADS::lz77Decode (const std::vector< LZ77Phrase > &P)
 the text of the phrases \( P \) of LZ77
std::vector< LZSSPhrase > ADS::lzssParse (std::string_view s, std::size_t w=std::numeric_limits< std::size_t >::max(), std::size_t minLength=1)
 the parse of LZSS of \( s \) with a window of \( w \) symbols, a copy for the closest among the longest prefixes of length at least minLength, a literal otherwise, by searching the whole window
std::vector< LZSSPhrase > ADS::lzssParseHashed (std::string_view s, std::size_t w, std::size_t minLength=3, std::size_t maxChain=std::numeric_limits< std::size_t >::max())
 the parse of LZSS of \( s \) by the chains of the hashes of 3 symbols, as in gzip, following at most maxChain candidates
std::string ADS::lzssDecode (const std::vector< LZSSPhrase > &P)
 the text of the phrases \( P \) of LZSS
std::vector< LZ78Phrase > ADS::lz78Parse (std::string_view s, std::size_t maxPhrases=std::numeric_limits< std::size_t >::max())
 the parse of LZ78 of \( s \), the dictionary restarting when it has maxPhrases phrases besides the empty one
std::string ADS::lz78Decode (const std::vector< LZ78Phrase > &P, std::size_t maxPhrases=std::numeric_limits< std::size_t >::max())
 the text of the phrases \( P \) of LZ78
std::vector< std::uint32_t > ADS::lzwEncode (std::string_view s)
 the identifiers of the code of LZW of \( s \)
std::string ADS::lzwDecode (const std::vector< std::uint32_t > &I)
 the text of the code \( I \) of LZW

Detailed description

The compressors of Lempel and Ziv (chapter 13, Dictionary-Based Compressors, of Ferragina's book, beyond the book of Montresor), whose dictionary is the text itself.

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

Function documentation

◆ lz77Decode()

std::string ADS::lz77Decode ( const std::vector< LZ77Phrase > & P)
inlinenodiscard

the text of the phrases \( P \) of LZ77

In pseudocode (LZ77 of Ziv and Lempel, beyond the book), one symbol at a time, so that a copy may overlap the symbols it writes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lz77Decode}( P ) \cr 2 & \quad s \gets \text{an empty string} \cr 3 & \quad \textbf{foreach}\ \langle d , \ell , c \rangle \in P \ \textbf{do} \cr 4 & \qquad a \gets |s| - d \cr 5 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ \ell - 1\ \textbf{do}\ s \gets s \cdot s[ a + i ] \cr 6 & \qquad \textbf{if}\ c \text{ is not none}\ \textbf{then}\ s \gets s \cdot c \cr 7 & \quad \textbf{return}\ s \end{array}} \]

Precondition
each copy refers to symbols already decoded

◆ lz77Parse()

std::vector< LZ77Phrase > ADS::lz77Parse ( std::string_view s,
std::size_t w = std::numeric_limits< std::size_t >::max() )
inlinenodiscard

the parse of LZ77 of \( s \) with a window of \( w \) symbols, the closest among the longest copies

In pseudocode (LZ77 of Ziv and Lempel, beyond the book), the copy found by trying each start in the window, from the closest one:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lz77Parse}( s , w ) \cr 2 & \quad p \gets 0;\ P \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ p < n\ \textbf{do} \cr 4 & \qquad ( d , \ell ) \gets \mathrm{longestCopy}( s , p , w ) \cr 5 & \qquad c \gets s[ p + \ell ] \text{ if } p + \ell < n \text{, none } \text{otherwise} \cr 6 & \qquad \text{append } \langle d , \ell , c \rangle \text{ to } P;\ p \gets p + \ell + 1 \cr 7 & \quad \textbf{return}\ P \cr 8 & \textbf{function}\ \mathrm{longestCopy}( s , p , w ) \cr 9 & \quad ( d , \ell ) \gets ( 0 , 0 ) \cr 10 & \quad \textbf{for}\ j \gets p - 1\ \textbf{downto}\ \max( 0 , p - w )\ \textbf{do} \cr 11 & \qquad l \gets \text{the largest } l \text{ with } s[ j , j + l ) = s[ p , p + l ) \text{ and } p + l \le n \cr 12 & \qquad \textbf{if}\ l > \ell\ \textbf{then}\ ( d , \ell ) \gets ( p - j , l ) \cr 13 & \quad \textbf{return}\ ( d , \ell ) \end{array}} \]

Precondition
\( w > 0 \)
Postcondition
lz77Decode( result ) == s (an audit)

◆ lz78Decode()

std::string ADS::lz78Decode ( const std::vector< LZ78Phrase > & P,
std::size_t maxPhrases = std::numeric_limits< std::size_t >::max() )
inlinenodiscard

the text of the phrases \( P \) of LZ78

In pseudocode (LZ78 of Ziv and Lempel, beyond the book), the dictionary \( D \) of the phrases rebuilt as the parse built it:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lz78Decode}( P , \mathit{maxPhrases} ) \cr 2 & \quad D \gets ( \varepsilon );\ s \gets \text{an empty string} \cr 3 & \quad \textbf{foreach}\ \langle \mathit{id} , c \rangle \in P \ \textbf{do} \cr 4 & \qquad \text{append } D[ \mathit{id} ] \cdot c \text{ to } D;\ s \gets s \cdot D[ \mathit{id} ] \cdot c \cr 5 & \qquad \textbf{if}\ |D| - 1 = \mathit{maxPhrases}\ \textbf{then}\ D \gets ( \varepsilon ) \cr 6 & \quad \textbf{return}\ s \end{array}} \]

Precondition
maxPhrases > 0, each identifier refers to a phrase

◆ lz78Parse()

std::vector< LZ78Phrase > ADS::lz78Parse ( std::string_view s,
std::size_t maxPhrases = std::numeric_limits< std::size_t >::max() )
inlinenodiscard

the parse of LZ78 of \( s \), the dictionary restarting when it has maxPhrases phrases besides the empty one

In pseudocode (LZ78 of Ziv and Lempel, beyond the book), the dictionary a trie whose root 0 is the empty phrase:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lz78Parse}( s , \mathit{maxPhrases} ) \cr 2 & \quad T \gets \text{the trie of the root } 0 \text{ alone};\ p \gets 0;\ P \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ p < n\ \textbf{do} \cr 4 & \qquad x \gets 0 \cr 5 & \qquad \textbf{while}\ p < n\ \textbf{and}\ x \text{ has a child } y \text{ by } s[ p ]\ \textbf{do}\ x \gets y;\ p \gets p + 1 \cr 6 & \qquad \textbf{if}\ p = n\ \textbf{then} \cr 7 & \qquad\quad \text{append } \langle \text{the parent of } x , \text{the symbol into } x \rangle \text{ to } P;\ \textbf{return}\ P \cr 8 & \qquad \text{append } \langle x , s[ p ] \rangle \text{ to } P; \ \text{add to } x \text{ a child by } s[ p ];\ p \gets p + 1 \cr 9 & \qquad \textbf{if}\ T \text{ has } \mathit{maxPhrases} \text{ nodes } \text{besides } 0\ \textbf{then}\ T \gets \text{the root alone} \cr 10 & \quad \textbf{return}\ P \end{array}} \]

Precondition
maxPhrases > 0
Postcondition
lz78Decode( result , maxPhrases ) == s (an audit)

◆ lzssDecode()

std::string ADS::lzssDecode ( const std::vector< LZSSPhrase > & P)
inlinenodiscard

the text of the phrases \( P \) of LZSS

In pseudocode (LZSS of Storer and Szymanski, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lzssDecode}( P ) \cr 2 & \quad s \gets \text{an empty string} \cr 3 & \quad \textbf{foreach}\ \text{phrase } f \in P\ \textbf{do} \cr 4 & \qquad \textbf{if}\ f \text{ is a literal } \langle 0 , c \rangle \ \textbf{then}\ s \gets s \cdot c \cr 5 & \qquad \textbf{else}\ \textbf{for}\ i \gets 0\ \textbf{to}\ \ell - 1 \ \textbf{do}\ s \gets s \cdot s[ |s| - d ]\quad \triangleright \ f = \langle d , \ell \rangle \cr 6 & \quad \textbf{return}\ s \end{array}} \]

Precondition
each copy refers to symbols already decoded

◆ lzssParse()

std::vector< LZSSPhrase > ADS::lzssParse ( std::string_view s,
std::size_t w = std::numeric_limits< std::size_t >::max(),
std::size_t minLength = 1 )
inlinenodiscard

the parse of LZSS of \( s \) with a window of \( w \) symbols, a copy for the closest among the longest prefixes of length at least minLength, a literal otherwise, by searching the whole window

In pseudocode (LZSS of Storer and Szymanski, beyond the book), with the longestCopy() of lz77Parse():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lzssParse}( s , w , \mathit{minLength} ) \cr 2 & \quad p \gets 0;\ P \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ p < n\ \textbf{do} \cr 4 & \qquad ( d , \ell ) \gets \mathrm{longestCopy}( s , p , w ) \cr 5 & \qquad \textbf{if}\ \ell \ge \mathit{minLength}\ \textbf{then} \ \text{append } \langle d , \ell \rangle \text{ to } P;\ p \gets p + \ell \cr 6 & \qquad \textbf{else}\ \text{append } \langle 0 , s[ p ] \rangle \text{ to } P;\ p \gets p + 1 \cr 7 & \quad \textbf{return}\ P \end{array}} \]

Precondition
\( w > 0 \), minLength > 0
Postcondition
lzssDecode( result ) == s (an audit)

◆ lzssParseHashed()

std::vector< LZSSPhrase > ADS::lzssParseHashed ( std::string_view s,
std::size_t w,
std::size_t minLength = 3,
std::size_t maxChain = std::numeric_limits< std::size_t >::max() )
inlinenodiscard

the parse of LZSS of \( s \) by the chains of the hashes of 3 symbols, as in gzip, following at most maxChain candidates

In pseudocode (the search of gzip, beyond the book), with \( \mathit{head}[ h ] \) the last position of hash \( h \) and \( \mathit{prev}[ j ] \) the one before \( j \) with the same hash:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lzssParseHashed}( s , w , \mathit{minLength} , \mathit{maxChain} ) \cr 2 & \quad p \gets 0;\ P \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ p < n\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ i < p \text{ not inserted yet, with } i + 3 \le n\ \textbf{do} \cr 5 & \qquad\quad h \gets \mathrm{hash}( s[ i , i + 3 ) ); \ \mathit{prev}[ i ] \gets \mathit{head}[ h ];\ \mathit{head}[ h ] \gets i \cr 6 & \qquad ( d , \ell ) \gets ( 0 , 0 ) \cr 7 & \qquad \textbf{if}\ p + 3 \le n\ \textbf{then} \cr 8 & \qquad\quad j \gets \mathit{head}[ \mathrm{hash}( s[ p , p + 3 ) ) ] \cr 9 & \qquad\quad \textbf{while}\ j \ne \textbf{nil}\ \textbf{and}\ p - j \le w\ \textbf{and}\ \text{fewer than } \mathit{maxChain} \text{ } \text{tried}\ \textbf{do} \cr 10 & \qquad\qquad l \gets \text{the largest } l \text{ with } s[ j , j + l ) = s[ p , p + l ) \text{ and } p + l \le n \cr 11 & \qquad\qquad \textbf{if}\ l > \ell\ \textbf{then}\ ( d , \ell ) \gets ( p - j , l ) \cr 12 & \qquad\qquad j \gets \mathit{prev}[ j ] \cr 13 & \qquad \textbf{if}\ \ell \ge \mathit{minLength}\ \textbf{then} \ \text{append } \langle d , \ell \rangle \text{ to } P;\ p \gets p + \ell \cr 14 & \qquad \textbf{else}\ \text{append } \langle 0 , s[ p ] \rangle \text{ to } P;\ p \gets p + 1 \cr 15 & \quad \textbf{return}\ P \end{array}} \]

Precondition
\( w > 0 \), minLength >= 3, maxChain > 0
Postcondition
lzssDecode( result ) == s (an audit); with no bound on maxChain, the result of lzssParse( s , w , minLength )

◆ lzwDecode()

std::string ADS::lzwDecode ( const std::vector< std::uint32_t > & I)
inlinenodiscard

the text of the code \( I \) of LZW

In pseudocode (LZW of Welch, beyond the book), each phrase added one step late, and an identifier not yet in \( D \) being the phrase \( f f[ 1 ] \) about to be added:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lzwDecode}( I ) \cr 2 & \quad \textbf{if}\ I \text{ is empty}\ \textbf{then return} \ \text{an empty string} \cr 3 & \quad D \gets \text{the 256 phrases of one byte};\ f \gets D[ I[ 0 ] ];\ s \gets f \cr 4 & \quad \textbf{for}\ k \gets 1\ \textbf{to}\ |I| - 1\ \textbf{do} \cr 5 & \qquad \textbf{if}\ I[ k ] < |D|\ \textbf{then}\ g \gets D[ I[ k ] ] \ \textbf{else}\ g \gets f \cdot f[ 1 ] \cr 6 & \qquad s \gets s \cdot g;\ \text{add } f \cdot g[ 1 ] \text{ to } D; \ f \gets g \cr 7 & \quad \textbf{return}\ s \end{array}} \]

Precondition
each identifier is a phrase, or the one being built

◆ lzwEncode()

std::vector< std::uint32_t > ADS::lzwEncode ( std::string_view s)
inlinenodiscard

the identifiers of the code of LZW of \( s \)

In pseudocode (LZW of Welch, beyond the book), the phrases of one byte numbered by the byte:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lzwEncode}( s ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ \text{an empty list} \cr 3 & \quad D \gets \text{the 256 phrases of one byte};\ f \gets s[ 0 ];\ I \gets \text{an empty list} \cr 4 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad c \gets s[ i ] \cr 6 & \qquad \textbf{if}\ f c \in D\ \textbf{then}\ f \gets f c \cr 7 & \qquad \textbf{else}\ \text{append } \mathit{id}( f ) \text{ to } I; \ \text{add } f c \text{ to } D;\ f \gets c \cr 8 & \quad \text{append } \mathit{id}( f ) \text{ to } I;\ \textbf{return} \ I \end{array}} \]

Postcondition
lzwDecode( result ) == s (an audit)