ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BurrowsWheeler.h File Reference

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...

#include "ADS/Contract.h"
#include "ADS/compression/CanonicalHuffman.h"
#include "ADS/compression/PackedBits.h"
#include "ADS/string/SuffixArray.h"
#include "ADS/string/SuffixSorting.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <map>
#include <numeric>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "ADS/compression/impl/BurrowsWheeler.tpp"

Go to the source code of this file.

Classes

struct  ADS::BWT
 the transform of Burrows and Wheeler: the last column of \( M' \) without the \( \$ \), and the row of the \( \$ \) More...

Namespaces

namespace  ADS

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 \)

Detailed description

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 rows of the matrix \( M' \) are the rotations of \( s\$ \), with \( \$ \) a sentinel smaller than every symbol, in sorted order; the transform bwt() is its last column \( L \) without the \( \$ \), with the row \( r \) of the \( \$ \). Since the rows that start with a string \( w \) are contiguous, the symbols that precede the occurrences of \( w \) are a substring of \( L \), and on a text whose symbols depend on their contexts, as those of a language do, \( L \) is made of runs of few distinct symbols. The rows of \( M' \) are the suffixes of \( s\$ \) in order, so that \( L[ i ] = s[ \mathit{SA}[ i ] - 1 ] \); the suffix array \( \mathit{SA} \) of \( s \) is built by the induced sorting SA-IS of Nong, Zhang and Chan in \( O( n ) \) time [see SuffixSorting.h], on entries of 32 bits below \( 2^{31} - 1 \) bytes, and the suffix \( \$ \) is the row 0, before it, by the arithmetic of the indices (the FM-index of the book [see FMIndex.h] keeps the suffix array of SuffixArray.h). The inverse transform inverseBwt() uses the LF-mapping: the \( k \)-th occurrence of \( c \) in \( L \) and the \( k \)-th in the first column \( F \) are the same symbol of \( s \), since the rows that end with \( c \) and those that start with it are in the same order, that of the rest of their rotations; hence \( \mathit{LF}[ i ] = C[ L[ i ] ] + \mathrm{rank}_{L[ i ]}( L , i ) \), with \( C[ c ] \) the number of the symbols smaller than \( c \). From the row 0, which is \( \$ s \) and ends with \( s[ n - 1 ] \), each step of \( \mathit{LF} \) moves one symbol to the left in \( s \), and \( n \) steps take \( O( n ) \) time, each step a likely miss of the cache.

The transforms that follow are described in the same chapter. The move-to-front of Bentley, Sleator, Tarjan and Wei replaces each symbol with its place in a list of the alphabet and moves it to the front, so that the runs of few symbols of \( L \) become runs of small integers, in \( O( |\Sigma| ) \) time per symbol here; its \( \gamma \)-code takes at most \( 2 H_0 + 1 \) bits per symbol, since the place of a symbol is at most its distance from its previous occurrence and the logarithm is concave. The run-length encoding replaces a run of \( \ell \) symbols \( c \) with \( \langle \ell , c \rangle \). The bzip chains them: the transform of each block, the move-to-front of \( L \), then the run-length encoding RLE0 of the runs of 0 alone, the other integers grown by 1 and a run of \( m \) zeros written in the code of Wheeler (the binary digits of \( m + 1 \) after its leading 1, as the symbols 0 and 1), and at last an entropy code, here the canonical code of Huffman of the block with lengths up to 20 [see CanonicalHuffman.h], as in bzip2 (which writes the digits of the runs from the least significant one and splits the block in groups coded by up to 6 tables). Coding \( L \) by contexts gives the entropy of order \( k \) for every \( k \) at once (the boosting of Ferragina, Giancarlo, Manzini and Sciortino), and Manzini bounds the chain of the transform, MTF, RLE and an arithmetic code by \( ( 5 + \epsilon ) n H_k^*( s ) \) bits plus lower terms, \( H_k^* \) a variant of the entropy of order \( k \) [see Entropy.h]. The transform is also an index [see FMIndex.h].

The contracts [see Contract.h]: the row \( r \) is that of a transform, each symbol of a move-to-front is in its alphabet of distinct symbols, a run is positive, the integers of RLE0 are at most 256, a block is positive; the inverse of each transform gives back its input, an audit. The functions are defined in BurrowsWheeler.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 14, Block-Sorting Compression, doi:10.1017/9781009128933
M. Burrows, D. J. Wheeler, A block-sorting lossless data compression algorithm, Technical Report 124, Digital Systems Research Center, 1994
J. L. Bentley, D. D. Sleator, R. E. Tarjan, V. K. Wei, A locally adaptive data compression scheme, Communications of the ACM 29(4), 320-330, 1986, doi:10.1145/5684.5688
G. Manzini, An analysis of the Burrows-Wheeler transform, Journal of the ACM 48(3), 407-430, 2001, doi:10.1145/382780.382782
P. Ferragina, R. Giancarlo, G. Manzini, M. Sciortino, Boosting textual compression in optimal linear time, Journal of the ACM 52(4), 688-713, 2005, doi:10.1145/1082036.1082043
G. Nong, S. Zhang, W. H. Chan, Two efficient algorithms for linear time suffix array construction, IEEE Transactions on Computers 60(10), 1471-1484, 2011, doi:10.1109/TC.2010.188
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa