7#include <unordered_map>
21template <
class State,
class Symbol>
22using CodeBook = std::vector<std::pair<Symbol, std::pair<State,int>>>;
46template <
class State,
class Freq,
class Symbol>
51template <
class Symbol,
class State_=u
int64_t>
87 using SymbolBook = std::vector<SymbolEntry>;
90 std::variant<std::monostate, std::pair<Symbol,int>, SymbolBook> data;
97 : codebook(std::move(codebook_))
99 assert(!codebook.empty());
103 for (
auto const& [
symbol, codeinfo] : codebook)
104 maxLength = std::max(maxLength, codeinfo.second);
109 symbolBook.resize(256);
110 for (
auto const& [
symbol, codeinfo] : codebook)
112 State c = codeinfo.first;
113 int n = codeinfo.second;
114 enter(symbolBook, c, n, std::pair(
symbol, n));
130 std::pair<State,int>
code(Symbol s)
const
132 auto it = std::find_if(codebook.begin(), codebook.end(),
133 [&](
auto const& p) { return p.first == s; });
134 assert(it != codebook.end());
143 return std::any_of(codebook.begin(), codebook.end(),
144 [&](
auto const& p) { return p.first == s; });
153 return lookup(symbolBook,
code);
158 for (
auto const& [
symbol, codepair] : codebook) {
159 std::cout <<
" Symbol: " <<
static_cast<int>(
symbol)
160 <<
", Code: " << codepair.first
161 <<
", Length: " << codepair.second <<
'\n';
168 SymbolBook symbolBook;
172 static void enter(SymbolBook& symbolBook,
State c,
int n, std::pair<Symbol,int> value)
178 int const cShift = c<<(8-n);
182 for (
int i=0; i<(1<<(8-n)); ++i)
183 symbolBook[cShift+i].data = value;
189 int const cShift = c>>(n-8);
190 auto& var = symbolBook[cShift].data;
195 assert(var.index() != 1);
196 if (var.index() == 0)
197 var = SymbolBook(256);
198 assert(var.index() == 2);
203 enter(std::get<2>(var),c&mask,n-8,value);
209 static std::pair<Symbol,int> lookup(SymbolBook
const& symbolBook,
State code)
213 auto const& var = symbolBook[cShift].data;
219 return std::get<1>(var);
222 assert(var.index()==2);
223 return lookup(std::get<2>(var),
code<<8);
259template <
class Symbol,
class OIter,
class Output,
class State=u
int64_t>
262 static int const outputDigits = 8*
sizeof(Output);
263 static int const stateDigits = 8*
sizeof(State);
280 for (
int i=0; i<
sizeof(State)/
sizeof(Output); ++i)
282 *out =
static_cast<Output
>(state>>(stateDigits-outputDigits));
283 state <<= outputDigits;
297 auto [c,n] = code.
code(s);
315 assert(0<=length && n+length<=stateDigits);
320 state |=
static_cast<State
>(c) << (stateDigits-length);
324 while (length > outputDigits)
326 *out++ =
static_cast<Output
>(state>>(stateDigits-outputDigits));
327 state <<= outputDigits;
328 length -= outputDigits;
365template <class Symbol, class IIter, class Input=typename std::iterator_traits<IIter>::value_type,
class State=
size_t>
368 static int const inputDigits = 8*
sizeof(Input);
369 static int const stateDigits = 8*
sizeof(State);
390 auto [s,n] = code.
symbol(state);
412 State c = state >> (stateDigits - n);
434 while (length < stateDigits-inputDigits)
436 state |=
static_cast<State
>(*in++) << (stateDigits-inputDigits-length);
437 length += inputDigits;
451template <
class Symbol>
457 template <
class Freq>
463 template <
class Freq>
466 template <
class OutputByte>
467 void encode(std::vector<Symbol>
const& symbols, std::vector<OutputByte>& out)
const;
469 template <
class InputByte>
470 std::vector<Symbol>
decode(std::vector<InputByte>
const& data, std::size_t symbolCount)
const;
474 return codes.find(s) != codes.end();
477 std::unordered_map<Symbol, std::vector<uint8_t>>
const&
codeTable()
const
491 std::vector<Node> nodes;
492 std::unordered_map<Symbol, std::vector<uint8_t>> codes;
void build(Frequencies< Freq, Symbol > const &f)
void encode(std::vector< Symbol > const &symbols, std::vector< OutputByte > &out) const
HuffmanCodec(Frequencies< Freq, Symbol > const &f)
bool hasSymbol(Symbol s) const
std::vector< Symbol > decode(std::vector< InputByte > const &data, std::size_t symbolCount) const
std::unordered_map< Symbol, std::vector< uint8_t > > const & codeTable() const
void printCodebook() const
std::pair< Symbol, int > symbol(State code) const
For an encoded message fraction of bits ( being the maximum code length), return the pair of symbo...
PrefixCode(CodeBook< State, Symbol > &&codebook_)
int maxLen() const
Returns the maximum length of any codeword (in bits).
bool hasSymbol(Symbol s) const
Checks whether the given symbol exists in the codebook.
std::pair< State, int > code(Symbol s) const
For a symbol , return the pair of code word (in the least significant bits) and code length .
Symbol pop_front(PrefixCode< Symbol, State > const &code)
Extract a symbol from the head of the remaining encoded message.
State pop_front(int n)
Extract a code word of given length from the head of the remaining encoded message.
~PrefixEncoder()
Flushes the remaining state, i.e. writes the rest of the encoded message to the output.
void push_back(State c, int n)
Appends a given codeword directly.
PrefixEncoder(OIter out_)
void push_back(Symbol s, PrefixCode< Symbol, State > const &code)
Appends a symbol to the encoded message.
std::vector< std::pair< Symbol, std::pair< State, int > > > CodeBook
The type of a code book - a map from symbols to codes and their lengths.
CodeBook< State, Symbol > huffmanCodeBook(Frequencies< Freq, Symbol > const &f)
Creates a code book for a fixed-length code of minimal size.