|
ADS
Algorithms and Data Structures
|
The transform of Burrows and Wheeler and the compressor built on it (chapter 14, Block-Sorting Compression, of Ferragina's book, beyond the book of Montresor). More...
Files | |
| file | ADS/compression/BurrowsWheeler.h |
| The transform of Burrows and Wheeler and the compressor built on it (chapter 14, Block-Sorting Compression, of Ferragina's book, beyond the book of Montresor). | |
Classes | |
| struct | ADS::BWT |
| the transform of Burrows and Wheeler: the last column of \( M' \) without the \( \$ \), and the row of the \( \$ \) More... | |
Functions | |
| BWT | ADS::bwt (std::string_view s, std::size_t wideFrom=bwtWideFrom) |
| std::vector< std::size_t > | ADS::lfMapping (const BWT &b) |
| the LF-mapping of the \( n + 1 \) rows of the transform \( b \) | |
| std::string | ADS::inverseBwt (const BWT &b) |
| the text of the transform \( b \) | |
| std::vector< std::uint8_t > | ADS::moveToFront (std::string_view s, std::string_view alphabet) |
| the places of the symbols of \( s \) in the list alphabet, each moved to the front after its use | |
| std::vector< std::uint8_t > | ADS::moveToFront (std::string_view s) |
| the move-to-front of moveToFront() above, with the 256 bytes in their order | |
| std::string | ADS::inverseMoveToFront (const std::vector< std::uint8_t > &x, std::string_view alphabet) |
| the symbols of the places \( x \) in the list alphabet | |
| std::string | ADS::inverseMoveToFront (const std::vector< std::uint8_t > &x) |
| the inverse of inverseMoveToFront() above, with the 256 bytes in their order | |
| std::vector< std::pair< std::size_t, char > > | ADS::runLengthEncode (std::string_view s) |
| the maximal runs of \( s \) as pairs \( ( \ell , c ) \) of a length and a symbol | |
| std::string | ADS::runLengthDecode (const std::vector< std::pair< std::size_t, char > > &R) |
| the text of the runs \( R \) | |
| std::vector< std::uint32_t > | ADS::zeroRunEncode (const std::vector< std::uint8_t > &x) |
| the RLE0 of bzip: the integers of \( x \) but 0 grown by 1, each run of \( m \) zeros in the code of Wheeler, the digits of \( m + 1 \) after the leading 1 | |
| std::vector< std::uint8_t > | ADS::zeroRunDecode (const std::vector< std::uint32_t > &y) |
| the integers of an RLE0 | |
| std::vector< std::uint8_t > | ADS::bzipCompress (std::string_view s, std::size_t block=900000) |
| the bzip of \( s \): blocks of at most block symbols, each transformed, moved to front, coded by RLE0 and by its canonical code of Huffman | |
| std::string | ADS::bzipDecompress (std::span< const std::uint8_t > code) |
| the text of a bzip | |
Variables | |
| constexpr std::size_t | ADS::bwtWideFrom = ( std::size_t( 1 ) << 31 ) - 1 |
| the transform of \( s \) | |
The transform of Burrows and Wheeler and the compressor built on it (chapter 14, Block-Sorting Compression, of Ferragina's book, beyond the book of Montresor).
The definitions, the invariants and the costs of the area are in Theory of compression.
|
inlinenodiscard |
The suffix array is of entries of 64 bits for \( n \ge \) wideFrom [see bwtWideFrom] and of 32 bits below; a smaller wideFrom takes the path of 64 bits on a short text, with the same result.
|
inlinenodiscard |
the bzip of \( s \): blocks of at most block symbols, each transformed, moved to front, coded by RLE0 and by its canonical code of Huffman
In pseudocode (the chain of bzip2, beyond the book), the lengths of the code limited to 20 bits and written in a preamble of 5 bits each:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bzipCompress}( s , \mathit{block} ) \cr 2 & \quad \text{write } n \cr 3 & \quad \textbf{foreach}\ t \text{ block of } \mathit{block} \text{ } \text{symbols of } s \text{, in order}\ \textbf{do} \cr 4 & \qquad ( L , r ) \gets \mathrm{bwt}( t );\ y \gets \mathrm{zeroRunEncode}( \mathrm{moveToFront}( L ) ) \cr 5 & \qquad H \gets \text{the canonical code of Huffman of the } \text{frequencies of } y \cr 6 & \qquad \text{write } |t| , r , |y| \text{ and the lengths of } H \cr 7 & \qquad \textbf{foreach}\ v \in y\ \textbf{do}\ \text{write } H( v ) \cr 8 & \quad \textbf{return}\ \text{the bits written} \end{array}} \]
|
inlinenodiscard |
the text of a bzip
In pseudocode (the chain of bzip2, beyond the book), each block undone in the opposite order:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bzipDecompress}( \mathit{code} ) \cr 2 & \quad \text{read } n;\ s \gets \text{an empty string} \cr 3 & \quad \textbf{while}\ |s| < n\ \textbf{do} \cr 4 & \qquad \text{read } m , r , k \text{ and the lengths of the code } H \cr 5 & \qquad y \gets \text{the } k \text{ symbols decoded by } H \cr 6 & \qquad L \gets \mathrm{inverseMoveToFront}( \mathrm{zeroRunDecode}( y ) ) \cr 7 & \qquad s \gets s \cdot \mathrm{inverseBwt}( L , r ) \cr 8 & \quad \textbf{return}\ s \end{array}} \]
|
inlinenodiscard |
the text of the transform \( b \)
In pseudocode (the inverse transform of Burrows and Wheeler, beyond the book), each step of \( \mathit{LF} \) going one symbol to the left in \( s \) from the row 0, which is \( \$ s \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{inverseBwt}( L , r ) \cr 2 & \quad \mathit{LF} \gets \mathrm{lfMapping}( L , r );\ k \gets 0 \cr 3 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \cr 4 & \qquad s[ i ] \gets \text{the symbol of the row } k \text{ of } L \cr 5 & \qquad k \gets \mathit{LF}[ k ] \cr 6 & \quad \textbf{return}\ s \end{array}} \]
|
inlinenodiscard |
the inverse of inverseMoveToFront() above, with the 256 bytes in their order
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{inverseMoveToFront}( x ) \cr 2 & \quad \textbf{return}\ \mathrm{inverseMoveToFront}( x , \text{the } \text{bytes } 0 , \ldots , 255 ) \end{array}} \]
|
inlinenodiscard |
the symbols of the places \( x \) in the list alphabet
In pseudocode (the move-to-front of Bentley, Sleator, Tarjan and Wei, beyond the book), the list moved as moveToFront() moves it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{inverseMoveToFront}( x , \Sigma ) \cr 2 & \quad \mathit{list} \gets \text{the symbols of } \Sigma \text{ in } \text{their order} \cr 3 & \quad \textbf{foreach}\ p \in x\ \textbf{do} \cr 4 & \qquad c \gets \mathit{list}[ p ];\ s \gets s \cdot c \cr 5 & \qquad \text{move } c \text{ to the front of } \mathit{list} \cr 6 & \quad \textbf{return}\ s \end{array}} \]
|
inlinenodiscard |
the LF-mapping of the \( n + 1 \) rows of the transform \( b \)
In pseudocode (the LF-mapping of Burrows and Wheeler, beyond the book), with the \( \$ \) at the row \( r \) the smallest symbol; the rows with the same last symbol keep their order in the first column:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lfMapping}( L , r ) \cr 2 & \quad C[ c ] \gets \text{the number of the symbols of } L\$ \text{ } \text{smaller than } c \text{, for each } c \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad c \gets \text{the symbol of the row } i\quad \triangleright \ \text{the } \$ \text{ if } i = r \cr 5 & \qquad \mathit{LF}[ i ] \gets C[ c ];\ C[ c ] \gets C[ c ] + 1 \cr 6 & \quad \textbf{return}\ \mathit{LF} \end{array}} \]
|
inlinenodiscard |
the move-to-front of moveToFront() above, with the 256 bytes in their order
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{moveToFront}( s ) \cr 2 & \quad \textbf{return}\ \mathrm{moveToFront}( s , \text{the bytes } 0 , \ldots , 255 ) \end{array}} \]
|
inlinenodiscard |
the places of the symbols of \( s \) in the list alphabet, each moved to the front after its use
In pseudocode (the move-to-front of Bentley, Sleator, Tarjan and Wei, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{moveToFront}( s , \Sigma ) \cr 2 & \quad \mathit{list} \gets \text{the symbols of } \Sigma \text{ in } \text{their order} \cr 3 & \quad \textbf{foreach}\ c \in s\ \textbf{do} \cr 4 & \qquad p \gets \text{the place of } c \text{ in } \mathit{list}; \ \text{append } p \text{ to } x \cr 5 & \qquad \text{move } c \text{ to the front of } \mathit{list} \cr 6 & \quad \textbf{return}\ x \end{array}} \]
|
inlinenodiscard |
the text of the runs \( R \)
In pseudocode (the run-length encoding, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{runLengthDecode}( R ) \cr 2 & \quad s \gets \text{an empty string} \cr 3 & \quad \textbf{foreach}\ ( \ell , c ) \in R\ \textbf{do}\ s \gets s \cdot c^{\ell} \cr 4 & \quad \textbf{return}\ s \end{array}} \]
|
inlinenodiscard |
the maximal runs of \( s \) as pairs \( ( \ell , c ) \) of a length and a symbol
In pseudocode (the run-length encoding, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{runLengthEncode}( s ) \cr 2 & \quad i \gets 0;\ R \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ i < n\ \textbf{do} \cr 4 & \qquad j \gets i \cr 5 & \qquad \textbf{while}\ j < n\ \textbf{and}\ s[ j ] = s[ i ] \ \textbf{do}\ j \gets j + 1 \cr 6 & \qquad \text{append } ( j - i , s[ i ] ) \text{ to } R;\ i \gets j \cr 7 & \quad \textbf{return}\ R \end{array}} \]
|
inlinenodiscard |
the integers of an RLE0
In pseudocode (the RLE0 of bzip2, beyond the book), a maximal run of the digits 0 and 1 read as a binary number with a leading 1:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{zeroRunDecode}( y ) \cr 2 & \quad i \gets 0;\ x \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ i < |y|\ \textbf{do} \cr 4 & \qquad \textbf{if}\ y[ i ] \ge 2\ \textbf{then} \cr 5 & \qquad\quad \text{append } y[ i ] - 1 \text{ to } x;\ i \gets i + 1; \ \textbf{continue} \cr 6 & \qquad v \gets 1 \cr 7 & \qquad \textbf{while}\ i < |y|\ \textbf{and}\ y[ i ] < 2\ \textbf{do} \ v \gets 2 v + y[ i ];\ i \gets i + 1 \cr 8 & \qquad \text{append } v - 1 \text{ zeros to } x \cr 9 & \quad \textbf{return}\ x \end{array}} \]
|
inlinenodiscard |
the RLE0 of bzip: the integers of \( x \) but 0 grown by 1, each run of \( m \) zeros in the code of Wheeler, the digits of \( m + 1 \) after the leading 1
In pseudocode (the RLE0 of bzip2, beyond the book), the digits of a run written from the most significant one:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{zeroRunEncode}( x ) \cr 2 & \quad i \gets 0;\ y \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ i < |x|\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x[ i ] \ne 0\ \textbf{then} \cr 5 & \qquad\quad \text{append } x[ i ] + 1 \text{ to } y;\ i \gets i + 1; \ \textbf{continue} \cr 6 & \qquad j \gets i \cr 7 & \qquad \textbf{while}\ j < |x|\ \textbf{and}\ x[ j ] = 0\ \textbf{do} \ j \gets j + 1 \cr 8 & \qquad \text{append the binary digits of } j - i + 1 \text{, but its } \text{leading 1, to } y \cr 9 & \qquad i \gets j \cr 10 & \quad \textbf{return}\ y \end{array}} \]
|
inlineconstexpr |
the transform of \( s \)
In pseudocode (the transform of Burrows and Wheeler, beyond the book), on the suffix array of \( s\$ \), whose row \( i \) ends with the symbol before the suffix \( \mathit{SA}[ i ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bwt}( s ) \cr 2 & \quad \mathit{SA} \gets \text{the suffix array of } s\$ \text{, with } \mathit{SA}[ 0 ] = n \cr 3 & \quad L \gets \text{an empty string} \cr 4 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad \textbf{if}\ \mathit{SA}[ i ] = 0\ \textbf{then}\ r \gets i \cr 6 & \qquad \textbf{else}\ L \gets L \cdot s[ \mathit{SA}[ i ] - 1 ] \cr 7 & \quad \textbf{return}\ ( L , r ) \end{array}} \]
the length of the text from which bwt() and the indices built on it keep the suffix array in entries of 64 bits, \( 2^{31} - 1 \), below which those of 32 bits hold every index of \( s\$ \)