30template <
typename Freq,
typename Symbol = u
int32_t>
38 template <
typename InputIt>
41 for (
auto it = first; it != last; ++it)
46 freq.push_back(Freq(1));
49 freq[pos->second] += Freq(1);
57 std::size_t
size()
const {
return freq.size(); }
65 if (it ==
indexMap.end())
throw std::out_of_range(
"Symbol not found");
90 template <
class InIter>
91 Alphabet(InIter first, InIter last, UInt maxRange=std::numeric_limits<UInt>::max())
92 : cumFreq(std::distance(first,last)+1)
96 std::partial_sum(first,last,cumFreq.begin()+1,std::plus<UInt>());
105 double scale = 0.9 * maxRange / tr;
106 for (
int i=1; i<cumFreq.size(); ++i)
107 cumFreq[i] = std::max(
static_cast<UInt
>(std::ceil(scale*cumFreq[i])),cumFreq[i-1]+1);
108std::cout <<
"alphabet rescaling cumulated frequencies from " << tr <<
" to " << cumFreq.back() <<
" < " << maxRange <<
"\n";
119 for (
int s=0; s<
size(); ++s)
120 std::fill(symbolLookup.begin()+cumFreq[s],symbolLookup.begin()+cumFreq[s+1],s);
131 template <
class InIter>
134 assert(std::distance(first,last)==
size());
135 assert(std::find(first,last,0)==last);
136 std::partial_sum(first,last,cumFreq.begin()+1,std::plus<UInt>());
144 return cumFreq.back();
152 return cumFreq.size()-1;
165 for (
int i=0; i<
size(); ++i)
167 double p = (cumFreq[i+1]-cumFreq[i]) / total;
178 if (!symbolLookup.empty())
179 return symbolLookup[value];
182 auto i = std::upper_bound(cumFreq.begin(),cumFreq.end(), value);
183 assert(i==cumFreq.end() || *i>value);
184 assert(i!=cumFreq.begin());
186 return i-cumFreq.begin()-1;
195 return std::make_pair(cumFreq[
symbol],cumFreq[
symbol+1]);
200 std::vector<UInt> cumFreq;
201 std::vector<int> symbolLookup;
A simple alphabet of symbols with frequencies to be used with the range coder.
double entropy() const
Returns the entropy of the alphabet.
UInt totalRange() const
Returns the total range, i.e. the maximum upper range bound of any symbol in the alphabet.
UInt symbol(UInt value) const
Returns the symbol that contains the given value.
UInt size() const
Returns the number of symbols in the alphabet.
Alphabet(InIter first, InIter last, UInt maxRange=std::numeric_limits< UInt >::max())
Constructor. Constructs an alphabet from a sequence of frequencies of symbols. Note that for each sym...
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.
std::map< Symbol, IndexType< Symbol > > indexMap
Frequencies(InputIt first, InputIt last)
Symbol originalSymbol(std::size_t i) const
std::vector< Freq > const & frequencies() const
Freq operator[](std::size_t i) const
IndexType< Symbol > remappedIndex(Symbol s) const
std::vector< Symbol > symbolTable