Files
mashzip/huffman_table.h
Andrey Gumirov b6a619303d WIP: MVP
2024-01-11 02:44:53 +07:00

41 lines
1.1 KiB
C++

#include <istream>
#include <unordered_map>
#include "bitstream.h"
#define HEADER_SIZE 128
#define MAX_LEN 16
#ifndef HUFFMAN_TABLE
#define HUFFMAN_TABLE
class HuffmanTable
{
private:
std::unordered_map<char, std::pair<int, short> > huffmanCodes;
std::vector<char> symbols;
std::array<int, 16> counts;
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(const char *header);
// Build from input stream
HuffmanTable(std::basic_istream<char> &is);
char *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