22 #include "field_codec_arithmetic.h"
23 #include "dccl/field_codec_manager.h"
26 using namespace dccl::logger;
28 std::map<std::string, dccl::arith::Model> dccl::arith::ModelManager::arithmetic_models_;
29 const dccl::arith::Model::symbol_type dccl::arith::Model::OUT_OF_RANGE_SYMBOL;
30 const dccl::arith::Model::symbol_type dccl::arith::Model::EOF_SYMBOL;
31 const dccl::arith::Model::symbol_type dccl::arith::Model::MIN_SYMBOL;
32 const int dccl::arith::Model::CODE_VALUE_BITS;
33 const int dccl::arith::Model::FREQUENCY_BITS;
34 const dccl::arith::Model::freq_type dccl::arith::Model::MAX_FREQUENCY;
35 std::map<std::string, std::map<std::string, dccl::Bitset> > dccl::arith::Model::last_bits_map;
42 dccl_arithmetic_load(
dccl);
50 FieldCodecManager::add<ArithmeticFieldCodec<int32> >(
"_arithmetic");
51 FieldCodecManager::add<ArithmeticFieldCodec<int64> >(
"_arithmetic");
52 FieldCodecManager::add<ArithmeticFieldCodec<uint32> >(
"_arithmetic");
53 FieldCodecManager::add<ArithmeticFieldCodec<uint64> >(
"_arithmetic");
54 FieldCodecManager::add<ArithmeticFieldCodec<double> >(
"_arithmetic");
55 FieldCodecManager::add<ArithmeticFieldCodec<float> >(
"_arithmetic");
56 FieldCodecManager::add<ArithmeticFieldCodec<bool> >(
"_arithmetic");
57 FieldCodecManager::add<ArithmeticFieldCodec<const google::protobuf::EnumValueDescriptor*> >(
"_arithmetic");
59 FieldCodecManager::add<ArithmeticFieldCodec<int32> >(
"dccl.arithmetic");
60 FieldCodecManager::add<ArithmeticFieldCodec<int64> >(
"dccl.arithmetic");
61 FieldCodecManager::add<ArithmeticFieldCodec<uint32> >(
"dccl.arithmetic");
62 FieldCodecManager::add<ArithmeticFieldCodec<uint64> >(
"dccl.arithmetic");
63 FieldCodecManager::add<ArithmeticFieldCodec<double> >(
"dccl.arithmetic");
64 FieldCodecManager::add<ArithmeticFieldCodec<float> >(
"dccl.arithmetic");
65 FieldCodecManager::add<ArithmeticFieldCodec<bool> >(
"dccl.arithmetic");
66 FieldCodecManager::add<ArithmeticFieldCodec<const google::protobuf::EnumValueDescriptor*> >(
"dccl.arithmetic");
71 dccl_arithmetic_unload(
dccl);
79 FieldCodecManager::remove<ArithmeticFieldCodec<int32> >(
"_arithmetic");
80 FieldCodecManager::remove<ArithmeticFieldCodec<int64> >(
"_arithmetic");
81 FieldCodecManager::remove<ArithmeticFieldCodec<uint32> >(
"_arithmetic");
82 FieldCodecManager::remove<ArithmeticFieldCodec<uint64> >(
"_arithmetic");
83 FieldCodecManager::remove<ArithmeticFieldCodec<double> >(
"_arithmetic");
84 FieldCodecManager::remove<ArithmeticFieldCodec<float> >(
"_arithmetic");
85 FieldCodecManager::remove<ArithmeticFieldCodec<bool> >(
"_arithmetic");
86 FieldCodecManager::remove<ArithmeticFieldCodec<const google::protobuf::EnumValueDescriptor*> >(
"_arithmetic");
88 FieldCodecManager::remove<ArithmeticFieldCodec<int32> >(
"dccl.arithmetic");
89 FieldCodecManager::remove<ArithmeticFieldCodec<int64> >(
"dccl.arithmetic");
90 FieldCodecManager::remove<ArithmeticFieldCodec<uint32> >(
"dccl.arithmetic");
91 FieldCodecManager::remove<ArithmeticFieldCodec<uint64> >(
"dccl.arithmetic");
92 FieldCodecManager::remove<ArithmeticFieldCodec<double> >(
"dccl.arithmetic");
93 FieldCodecManager::remove<ArithmeticFieldCodec<float> >(
"dccl.arithmetic");
94 FieldCodecManager::remove<ArithmeticFieldCodec<bool> >(
"dccl.arithmetic");
95 FieldCodecManager::remove<ArithmeticFieldCodec<const google::protobuf::EnumValueDescriptor*> >(
"dccl.arithmetic");
100 dccl::arith::Model::symbol_type dccl::arith::Model::value_to_symbol(value_type value)
const
102 if(value < *user_model_.value_bound().begin() || value > *(user_model_.value_bound().end()-1))
103 return Model::OUT_OF_RANGE_SYMBOL;
106 google::protobuf::RepeatedField<double>::const_iterator upper_it =
107 std::upper_bound(user_model_.value_bound().begin(),
108 user_model_.value_bound().end(),
112 google::protobuf::RepeatedField<double>::const_iterator lower_it =
113 (upper_it == user_model_.value_bound().begin()) ? upper_it : upper_it - 1;
115 double lower_diff = std::abs((*lower_it)*(*lower_it) - value*value);
116 double upper_diff = std::abs((*upper_it)*(*upper_it) - value*value);
122 symbol_type symbol = ((lower_diff < upper_diff) ? lower_it : upper_it)
123 - user_model_.value_bound().begin();
131 dccl::arith::Model::value_type dccl::arith::Model::symbol_to_value(symbol_type symbol)
const
134 if(symbol == EOF_SYMBOL)
135 throw(
Exception(
"EOF symbol has no value."));
137 value_type value = (symbol == Model::OUT_OF_RANGE_SYMBOL) ?
138 std::numeric_limits<value_type>::quiet_NaN() :
139 user_model_.value_bound(symbol);
145 std::pair<dccl::arith::Model::freq_type, dccl::arith::Model::freq_type> dccl::arith::Model::symbol_to_cumulative_freq(symbol_type symbol, ModelState state)
const
147 const boost::bimap<symbol_type, freq_type>& c_freqs = (state == ENCODER) ?
148 encoder_cumulative_freqs_ :
149 decoder_cumulative_freqs_;
151 boost::bimap<symbol_type, freq_type>::left_map::const_iterator c_freq_it =
152 c_freqs.left.find(symbol);
153 std::pair<freq_type, freq_type> c_freq_range;
154 c_freq_range.second = c_freq_it->second;
155 if(c_freq_it == c_freqs.left.begin())
157 c_freq_range.first = 0;
162 c_freq_range.first = c_freq_it->second;
168 std::pair<dccl::arith::Model::symbol_type, dccl::arith::Model::symbol_type> dccl::arith::Model::cumulative_freq_to_symbol(std::pair<freq_type, freq_type> c_freq_pair, ModelState state)
const
171 const boost::bimap<symbol_type, freq_type>& c_freqs = (state == ENCODER) ?
172 encoder_cumulative_freqs_ :
173 decoder_cumulative_freqs_;
175 std::pair<symbol_type, symbol_type> symbol_pair;
184 symbol_pair.first = c_freqs.right.upper_bound(c_freq_pair.first)->second;
187 if(symbol_pair.first == c_freqs.left.rbegin()->first)
188 symbol_pair.second = symbol_pair.first;
189 else if(c_freqs.left.find(symbol_pair.first)->second > c_freq_pair.second)
190 symbol_pair.second = symbol_pair.first;
192 symbol_pair.second = symbol_pair.first + 1;
200 void dccl::arith::Model::update_model(symbol_type symbol, ModelState state)
202 if(!user_model_.is_adaptive())
205 boost::bimap<symbol_type, freq_type>& c_freqs = (state == ENCODER) ?
206 encoder_cumulative_freqs_ :
207 decoder_cumulative_freqs_;
211 dlog <<
"Model was: " << std::endl;
212 for(symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
214 boost::bimap<symbol_type, freq_type>::left_iterator it =
215 c_freqs.left.find(i);
216 if(it != c_freqs.left.end())
217 dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second << std::endl;
222 for(symbol_type i = max_symbol(), n = symbol; i >= n; --i)
224 boost::bimap<symbol_type, freq_type>::left_iterator it =
225 c_freqs.left.find(i);
226 if(it != c_freqs.left.end())
227 c_freqs.left.replace_data(it, it->second + 1);
232 dlog <<
"Model is now: " << std::endl;
233 for(symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
235 boost::bimap<symbol_type, freq_type>::left_iterator it =
236 c_freqs.left.find(i);
237 if(it != c_freqs.left.end())
238 dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second << std::endl;
242 dlog.
is(DEBUG3) && dlog <<
"total freq: " << total_freq(state) << std::endl;