66 static int const digits = std::numeric_limits<UInt>::digits;
76 static UInt
const top =
static_cast<UInt
>(1) << (
digits-8);
108 assert(std::numeric_limits<UInt>::is_specialized);
109 assert(!std::numeric_limits<UInt>::is_signed);
152 for(
int i=0; i<digits/8; i++)
154 out.write((
char const*)buf,fill);
168 void push(std::pair<UInt,UInt> s, UInt totalRange)
171 assert(s.first<s.second);
178 assert(
range >= bottom);
184 range *= s.second-s.first;
192 assert(
range >= bottom);
204 static int const bufsize = 64;
205 unsigned char buf[bufsize];
213 out.write((
char const*)buf,bufsize);
215 throw std::ios_base::failure(
"write error");
217 this->
count += bufsize;
221 buf[fill] =
low >> (digits-8);
262 : code(0), in(input_)
265 in.exceptions(std::ios_base::eofbit | in.exceptions());
268 for(
int i=0; i<digits/8; i++) {
269 code = (code << 8) | in.get();
270 if (!in)
throw std::ios_base::failure(
"read error");
284 UInt
const&
low = this->
low;
286 return (code-
low)/(
range/totalRange);
307 void pop(std::pair<UInt,UInt> s, UInt totalRange) {
311 assert(s.first<=
front(totalRange)
312 &&
front(totalRange)<s.second);
316 range *= s.second-s.first;
321 code = code<<8 | in.get();
349 template <
class InIter>
351 : cumFreq(std::
distance(first,last)+1)
354 std::partial_sum(first,last,cumFreq.begin()+1,std::plus<UInt>());
364 template <
class InIter>
368 assert(std::find(first,last,0)==last);
369 std::partial_sum(first,last,cumFreq.begin()+1,std::plus<UInt>());
377 return cumFreq.back();
385 return cumFreq.size()-1;
393 typename std::vector<UInt>::const_iterator i
394 = std::upper_bound(cumFreq.begin(),cumFreq.end(), value);
395 assert(i==cumFreq.end() || *i>value);
396 assert(i!=cumFreq.begin());
397 return i-cumFreq.begin()-1;
406 return std::make_pair(cumFreq[
symbol],cumFreq[
symbol+1]);
412 std::vector<UInt> cumFreq;
423template <
class UInt,
class Symbol>
A simple alphabet of symbols with frequencies to be used with the range coder.
UInt totalRange() const
Returns the total range, i.e. the maximum upper range bound of any symbol in the alphabet.
Alphabet(InIter first, InIter last)
UInt symbol(UInt value) const
Returns the symbol that contains the given value.
UInt size() const
Returns the number of symbols in the alphabet.
void update(InIter first, InIter last)
Modifies the symbols' half-open ranges to represent the symbols' frequencies given in the range [firs...
std::pair< UInt, UInt > range(UInt symbol) const
Returns the symbol's half-open cumulative frequency range.
Base class for entropy coding with range encoder and decoder.
static const UInt maxRange
Maximal total range of alphabets.
size_t size() const
Number of processed encoded bytes.
Entropy coding with range decoder.
RangeDecoder(std::istream &input_)
static UInt const maxRange
void pop(std::pair< UInt, UInt > s, UInt totalRange)
UInt front(UInt totalRange) const
Entropy coding with range encoder.
RangeEncoder(std::ostream &out_)
static UInt const maxRange
~RangeEncoder()
Destructor writes the remaining data to the output stream.
void push(std::pair< UInt, UInt > s, UInt totalRange)
Encodes one symbol.
UInt decodeSymbol(RangeDecoder< UInt > &decoder, Alphabet< UInt > const &alphabet)
A convenience function that retrieves and pops the current symbol from a range decoder.
void encodeSymbol(RangeEncoder< UInt > &encoder, Alphabet< UInt > const &alphabet, Symbol const &s)
A convenience function that encodes a symbol in a range encoder.
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Scalar distance(Point< Scalar, dim > const &first, Point< Scalar, dim > const &second)