This commit is contained in:
Andrey Gumirov
2024-01-08 02:39:45 +07:00
parent fdcba0b869
commit 606ce553ac
9 changed files with 1594 additions and 0 deletions

35
huffman_table.h Normal file
View File

@ -0,0 +1,35 @@
#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