36 lines
988 B
C++
36 lines
988 B
C++
#include <istream>
|
|
#include <unordered_map>
|
|
|
|
#include "bitstream.h"
|
|
|
|
#ifndef HUFFMAN_TABLE
|
|
#define HUFFMAN_TABLE
|
|
|
|
class HuffmanTable
|
|
{
|
|
private:
|
|
std::unordered_map<char, std::pair<int, short> > huffmanCodes;
|
|
public:
|
|
// Given the list of code lengths length[0..n-1] representing a canonical
|
|
// Huffman code for n symbols, construct the tables required to decode those
|
|
// codes.
|
|
HuffmanTable(uint8_t *header);
|
|
|
|
// Build from input stream
|
|
HuffmanTable(std::basic_istream<char> &is);
|
|
|
|
uint8_t *to_header();
|
|
|
|
std::pair<int, short> operator[](const char &c);
|
|
|
|
void write_symbol(obitstream &os, const char &c);
|
|
|
|
// Decode a code from the stream s using huffman table h. Return the symbol or
|
|
// a negative value if there is an error. If all of the lengths are zero, i.e.
|
|
// an empty code, or if the code is incomplete and an invalid code is received,
|
|
// then IG_BAD_CODE_ERR is returned after reading MAXBITS bits.
|
|
int decode_one_symbol(ibitstream &bs);
|
|
};
|
|
|
|
#endif
|