18 template<
typename coder_t,
typename text_t,
typename factor_t = FactorBuffer>
19 inline void encode_text(coder_t& coder,
const text_t& text,
const factor_t& factors) {
20 assert(factors.is_sorted());
22 const auto n = text.size();
25 const auto flen_min = factors.shortest_factor();
26 const auto flen_max = factors.longest_factor();
32 for(
auto it = factors.begin(); it != factors.end(); ++it) {
33 const Factor& factor = *it;
34 fdist_max = std::max(fdist_max, factor.
pos - p);
35 p = factor.
pos + factor.
len;
38 fdist_max = std::max(fdist_max, n - p);
42 const Range text_r(n);
44 const Range fdist_r(fdist_max);
47 coder.encode(n,
len_r);
48 coder.encode(flen_min, text_r);
49 coder.encode(flen_max, text_r);
50 coder.encode(fdist_max, text_r);
54 for(
auto it = factors.begin(); it != factors.end(); ++it) {
55 const Factor& factor = *it;
59 coder.encode(
false,
bit_r);
62 coder.encode(
true,
bit_r);
65 DCHECK_LE(p, factor.
pos);
66 DCHECK_LE(factor.
pos - p, fdist_max);
67 coder.encode(factor.
pos - p, fdist_r);
71 while(p < factor.
pos) {
76 DCHECK_LT(factor.
src + factor.
len, n);
77 coder.encode(factor.
src, text_r);
78 coder.encode(factor.
len, flen_r);
80 p += size_t(factor.
len);
84 coder.encode(
true,
bit_r);
85 coder.encode(n - p, fdist_r);
94 template<
typename coder_t,
typename decode_buffer_t>
95 inline void decode_text(coder_t& decoder, std::ostream& outs) {
97 auto text_len = decoder.template decode<len_t>(
len_r);
98 Range text_r(text_len);
101 auto flen_min = decoder.template decode<len_t>(text_r);
102 auto flen_max = decoder.template decode<len_t>(text_r);
106 auto fdist_max = decoder.template decode<len_t>(text_r);
107 Range fdist_r(fdist_max);
110 decode_buffer_t buffer(text_len);
113 while(!decoder.eof()) {
116 auto b = decoder.template decode<bool>(
bit_r);
117 if(b) num = decoder.template decode<len_t>(fdist_r);
122 auto c = decoder.template decode<uliteral_t>(
literal_r);
123 buffer.decode_literal(c);
128 auto src = decoder.template decode<len_t>(text_r);
129 auto len = decoder.template decode<len_t>(flen_r);
131 buffer.decode_factor(
src, len);
139 buffer.write_to(outs);
Represents a generic range of positive integers.
void decode_text(coder_t &decoder, std::ostream &outs)
Contains the text compression and encoding framework.
constexpr auto bit_r
Global predefined range for bits (0 or 1).
void encode_text(coder_t &coder, const text_t &text, const factor_t &factors)
Represents a range of positive integers that tend to be distributed towards the minimum.
constexpr auto literal_r
Global predefined reange for literals.
fast_t< len_compact_t > len_t
Type to represent an length value.
static void log(const char *key, const T &value)
Logs a user statistic for the current phase.
constexpr auto len_r
Global predefined range for len_t.