17 using ulong =
unsigned long;
20 using len_fixup_t = uint32_t;
26 Meta m(
"coder",
"arithmetic",
"Simple range encoding");
37 std::vector<len_fixup_t> C;
40 ulong upper_bound=std::numeric_limits<ulong>::max();
43 len_t literal_count = 0;
44 len_t literal_counter = 0;
45 ulong min_range=std::numeric_limits<len_fixup_t>::max();
53 std::vector<len_fixup_t> count_alphabet_literals(T&& input) {
54 std::vector<len_fixup_t> C;
57 while(input.has_next()) {
60 DCHECK_LT(C[static_cast<uliteral_t>(c)], std::numeric_limits<len_fixup_t>::max());
72 void build_intervals(std::vector<len_fixup_t> &c) {
76 len_t min=std::numeric_limits<len_fixup_t>::max();
81 min=std::min(min,
len_t(c[i]));
95 template<
typename value_t>
96 inline void setNewBounds(value_t v) {
97 ulong range = upper_bound-lower_bound;
100 writeCode(lower_bound);
102 upper_bound=std::numeric_limits<ulong>::max();
103 range = upper_bound-lower_bound;
105 DCHECK_NE(lower_bound,upper_bound);
110 const ulong offset_upper = range <= literal_count ? (range*C[(int) v])/literal_count : (range/literal_count)*C[(
int) v];
111 upper_bound=lower_bound+offset_upper;
113 const ulong offset_lower = range <= literal_count ? (range*C[(int) v-1])/literal_count : (range/literal_count)*C[(
int) v-1];
114 lower_bound=lower_bound+offset_lower;
119 inline void writeCodebook() {
128 m_out->write_int<len_fixup_t>(literal_count);
135 m_out->write_int(C[0]);
140 m_out->write_int(C[i]);
146 inline void writeCode(ulong code){
147 m_out->write_int(code);
151 void postProcessing() {
152 writeCode(lower_bound);
154 m_out->write_int(std::numeric_limits<ulong>::max());
158 template<
typename literals_t>
161 C(count_alphabet_literals(
std::move(literals))) {
168 template<
typename value_t>
173 if(literal_counter==literal_count){
182 std::vector<std::pair<uliteral_t ,int>> literals;
185 len_t literal_count = 0;
186 len_t literal_counter = 0;
187 ulong literals_read = 0;
188 ulong min_range=std::numeric_limits<ulong>::max();
190 void decode(ulong code) {
191 ulong lower_bound = 0;
192 ulong upper_bound = std::numeric_limits<ulong>::max();
193 std::ostringstream os;
194 ulong interval_parts = literals[codebook_size -1].second;
197 ulong range = upper_bound - lower_bound;
200 while(min_range<=range && literal_counter<literal_count) {
201 ulong interval_lower_bound = lower_bound;
203 for(
int i = 0; i < codebook_size ; i++) {
204 const std::pair<uliteral_t, int>& pair=literals[i];
205 const ulong offset = range <= interval_parts ? range*pair.second/interval_parts : range/interval_parts*pair.second;
206 upper_bound = lower_bound + offset;
207 if(code < upper_bound) {
210 lower_bound = interval_lower_bound;
213 interval_lower_bound = upper_bound;
216 range = upper_bound - lower_bound;
226 literal_count = m_in->read_int<len_fixup_t>();
228 literals.resize(codebook_size);
231 for (
int i =0; i<codebook_size; i++) {
233 int val = m_in->read_int<
int>();
234 literals[i]=std::pair<uliteral_t, int>(c, val);
237 min_range=literals[codebook_size-1].second;
242 template<
typename value_t>
245 if(!decoded.size()) {
246 ulong code = m_in->read_int<ulong>();
247 if(code!=std::numeric_limits<ulong>::max()) {
252 value_t val = decoded[literals_read++];
255 if(literals_read == decoded.size()) {
256 ulong code = m_in->read_int<ulong>();
257 if(code!=std::numeric_limits<ulong>::max()) {
Contains the text compression and encoding framework.
Represents the range of valid tdc::uliteral_t values.
Encoder(Env &&env, Output &out, literals_t &&literals)
uint8_t uliteral_t
Type to represent signed single literals.
void encode(value_t v, const LiteralRange &)
Decodes data from an Arithmetic character stream.
value_t decode(const LiteralRange &)
constexpr size_t ULITERAL_MAX
The maximum value of uliteral_t.
Env & env()
Provides access to the environment that the algorithm works in.
An abstraction layer for algorithm output.
Defines data encoding to and decoding from a stream of ASCII characters.
fast_t< len_compact_t > len_t
Type to represent an length value.
Encodes data to an ASCII character stream.
std::shared_ptr< BitOStream > m_out
The underlying bit output stream.
value_t decode(const Range &r)
Decodes an arbitrary-range integer value.
static Meta meta()
Yields the coder's meta information.
Local environment for a compression/encoding/decompression call.
void encode(value_t v, const Range &r)
Encodes an arbitrary-range integer value.
Interface for algorithms.