17 std::vector<std::pair<CodeType, uliteral_t>> dictionary;
20 const auto reset_dictionary = [&] {
22 dictionary.reserve(reserve_dms);
24 const long int minc = std::numeric_limits<uliteral_t>::min();
25 const long int maxc = std::numeric_limits<uliteral_t>::max();
27 for (
long int c = minc; c <= maxc; ++c)
28 dictionary.push_back({dms, static_cast<uliteral_t> (c)});
31 const auto rebuild_string = [&](
CodeType k) ->
const std::vector<uliteral_t> * {
32 static std::vector<uliteral_t> s;
37 s.reserve(reserve_dms);
41 s.push_back(dictionary[k].second);
42 k = dictionary[k].first;
45 std::reverse(s.begin(), s.end());
54 bool corrupted =
false;
58 bool dictionary_reset =
false;
61 if (dictionary.size() == dms)
64 dictionary_reset =
true;
67 if (!next_code_callback(k, dictionary_reset, corrupted))
72 if (k > dictionary.size()) {
74 s <<
"invalid compressed code " << k;
75 throw std::runtime_error(s.str());
78 const std::vector<uliteral_t> *s;
80 if (k == dictionary.size())
82 dictionary.push_back({i, rebuild_string(i)->front()});
83 s = rebuild_string(k);
87 s = rebuild_string(k);
90 dictionary.push_back({i, s->front()});
93 out.write((
char*) &s->front(), s->size());
98 throw std::runtime_error(
"corrupted compressed file");
Contains the text compression and encoding framework.
void decode_step(F next_code_callback, std::ostream &out, const CodeType dms, const CodeType reserve_dms)
lz78::factorid_t CodeType
uint32_t factorid_t
Type for the factor indices, bounded by the number of LZ78 trie nodes.