32 #include <agrum/tools/database/DBTranslator4DiscretizedVariable.h> 33 #include <agrum/tools/database/DBCell.h> 35 #ifndef DOXYGEN_SHOULD_SKIP_THIS 43 template <
template <
typename >
class ALLOC >
44 template <
typename GUM_SCALAR,
template <
typename >
class XALLOC >
45 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
46 const DiscretizedVariable< GUM_SCALAR >& var,
47 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
48 std::size_t max_dico_entries,
49 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
50 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
56 _variable_(var.name(), var.description()) {
58 if (var.domainSize() > max_dico_entries) {
59 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
63 const auto& ticks = var.ticks();
64 for (
const auto tick: ticks) {
65 _variable_.addTick((
float)tick);
69 const float lower_bound = (
float)ticks[0];
70 const float upper_bound = (
float)ticks.back();
74 for (
auto iter =
this->missing_symbols_.beginSafe(); iter !=
this->missing_symbols_.endSafe();
76 if (DBCell::isReal(*iter)) {
77 const float missing_val = std::stof(*iter);
78 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
79 this->missing_symbols_.erase(iter);
86 for (
const auto& label: var.labels()) {
92 if (
this->missing_symbols_.exists(label)) {
this->missing_symbols_.erase(label); }
94 this->back_dico_.insert(size, label);
99 _real_variable_ = var.clone();
101 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
106 template <
template <
typename >
class ALLOC >
107 template <
template <
typename >
class XALLOC >
108 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
109 const IDiscretizedVariable& var,
110 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
111 std::size_t max_dico_entries,
112 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
113 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
119 _variable_(var.name(), var.description()) {
121 if (var.domainSize() > max_dico_entries) {
122 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
126 const auto ticks = var.ticksAsDoubles();
127 for (
const auto tick: ticks) {
128 _variable_.addTick((
float)tick);
132 const float lower_bound =
float(ticks[0]);
133 const float upper_bound =
float(ticks.back());
137 for (
auto iter =
this->missing_symbols_.beginSafe(); iter !=
this->missing_symbols_.endSafe();
139 if (DBCell::isReal(*iter)) {
140 const float missing_val = std::stof(*iter);
141 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
142 this->missing_symbols_.erase(iter);
148 std::size_t size = 0;
149 for (
const auto& label: var.labels()) {
155 if (
this->missing_symbols_.exists(label)) {
this->missing_symbols_.erase(label); }
157 this->back_dico_.insert(size, label);
162 _real_variable_ = var.clone();
164 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
169 template <
template <
typename >
class ALLOC >
170 template <
typename GUM_SCALAR >
171 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
172 const DiscretizedVariable< GUM_SCALAR >& var,
173 std::size_t max_dico_entries,
174 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
175 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
180 _variable_(var.name(), var.description()) {
182 if (var.domainSize() > max_dico_entries) {
183 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
187 const auto& ticks = var.ticks();
188 for (
const auto tick: ticks) {
189 _variable_.addTick((
float)tick);
193 std::size_t size = 0;
194 for (
const auto& label: var.labels()) {
195 this->back_dico_.insert(size, label);
200 _real_variable_ = var.clone();
202 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
207 template <
template <
typename >
class ALLOC >
208 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
209 const IDiscretizedVariable& var,
210 std::size_t max_dico_entries,
211 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
212 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
217 _variable_(var.name(), var.description()) {
219 if (var.domainSize() > max_dico_entries) {
220 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
224 const auto ticks = var.ticksAsDoubles();
225 for (
const auto tick: ticks) {
226 _variable_.addTick((
float)tick);
230 std::size_t size = 0;
231 for (
const auto& label: var.labels()) {
232 this->back_dico_.insert(size, label);
237 _real_variable_ = var.clone();
239 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
244 template <
template <
typename >
class ALLOC >
245 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
246 const DBTranslator4DiscretizedVariable< ALLOC >& from,
247 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
248 DBTranslator< ALLOC >(from, alloc),
249 _variable_(from._variable_) {
251 _real_variable_ = from._real_variable_->clone();
253 GUM_CONS_CPY(DBTranslator4DiscretizedVariable);
258 template <
template <
typename >
class ALLOC >
259 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
260 const DBTranslator4DiscretizedVariable< ALLOC >& from) :
261 DBTranslator4DiscretizedVariable< ALLOC >(from, from.getAllocator()) {}
265 template <
template <
typename >
class ALLOC >
266 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
267 DBTranslator4DiscretizedVariable< ALLOC >&& from,
268 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
269 DBTranslator< ALLOC >(std::move(from), alloc),
270 _variable_(std::move(from._variable_)) {
272 _real_variable_ = from._real_variable_;
273 from._real_variable_ =
nullptr;
275 GUM_CONS_MOV(DBTranslator4DiscretizedVariable);
280 template <
template <
typename >
class ALLOC >
281 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
282 DBTranslator4DiscretizedVariable< ALLOC >&& from) :
283 DBTranslator4DiscretizedVariable< ALLOC >(std::move(from), from.getAllocator()) {}
287 template <
template <
typename >
class ALLOC >
288 DBTranslator4DiscretizedVariable< ALLOC >* DBTranslator4DiscretizedVariable< ALLOC >::clone(
289 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc)
const {
290 ALLOC< DBTranslator4DiscretizedVariable< ALLOC > > allocator(alloc);
291 DBTranslator4DiscretizedVariable< ALLOC >* translator = allocator.allocate(1);
293 allocator.construct(translator, *
this, alloc);
295 allocator.deallocate(translator, 1);
303 template <
template <
typename >
class ALLOC >
304 INLINE DBTranslator4DiscretizedVariable< ALLOC >*
305 DBTranslator4DiscretizedVariable< ALLOC >::clone()
const {
306 return clone(
this->getAllocator());
311 template <
template <
typename >
class ALLOC >
312 INLINE DBTranslator4DiscretizedVariable< ALLOC >::~DBTranslator4DiscretizedVariable() {
313 if (_real_variable_ !=
nullptr)
delete _real_variable_;
315 GUM_DESTRUCTOR(DBTranslator4DiscretizedVariable);
320 template <
template <
typename >
class ALLOC >
321 DBTranslator4DiscretizedVariable< ALLOC >& DBTranslator4DiscretizedVariable< ALLOC >::operator=(
322 const DBTranslator4DiscretizedVariable< ALLOC >& from) {
324 DBTranslator< ALLOC >::operator=(from);
325 _variable_ = from._variable_;
327 if (_real_variable_ !=
nullptr)
delete _real_variable_;
328 _real_variable_ = from._real_variable_->clone();
336 template <
template <
typename >
class ALLOC >
337 DBTranslator4DiscretizedVariable< ALLOC >& DBTranslator4DiscretizedVariable< ALLOC >::operator=(
338 DBTranslator4DiscretizedVariable< ALLOC >&& from) {
340 DBTranslator< ALLOC >::operator=(std::move(from));
341 _variable_ = std::move(from._variable_);
343 if (_real_variable_ !=
nullptr)
delete _real_variable_;
344 _real_variable_ = from._real_variable_;
345 from._real_variable_ =
nullptr;
353 template <
template <
typename >
class ALLOC >
354 INLINE DBTranslatedValue
355 DBTranslator4DiscretizedVariable< ALLOC >::translate(
const std::string& str) {
358 return DBTranslatedValue{std::size_t(_variable_[str])};
359 }
catch (gum::Exception&) {
361 if (
this->isMissingSymbol(str))
362 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
368 return DBTranslatedValue{
this->back_dico_.first(str)};
369 }
catch (gum::Exception&) {
370 if (!DBCell::isReal(str)) {
372 "String \"" << str <<
"\" cannot be translated because it is not a number");
374 GUM_ERROR(UnknownLabelInDatabase,
375 "The translation of \"" << str <<
"\" could not be found")
383 template <
template <
typename >
class ALLOC >
384 INLINE std::string DBTranslator4DiscretizedVariable< ALLOC >::translateBack(
385 const DBTranslatedValue translated_val)
const {
387 return this->back_dico_.second(translated_val.discr_val);
388 }
catch (Exception&) {
390 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
391 && !
this->missing_symbols_.empty())
392 return *(
this->missing_symbols_.begin());
394 GUM_ERROR(UnknownLabelInDatabase,
395 "The back translation of \"" << translated_val.discr_val
396 <<
"\" could not be found");
402 template <
template <
typename >
class ALLOC >
403 INLINE
bool DBTranslator4DiscretizedVariable< ALLOC >::hasEditableDictionary()
const {
409 template <
template <
typename >
class ALLOC >
410 INLINE
void DBTranslator4DiscretizedVariable< ALLOC >::setEditableDictionaryMode(
bool) {}
414 template <
template <
typename >
class ALLOC >
415 bool DBTranslator4DiscretizedVariable< ALLOC >::needsReordering()
const {
421 template <
template <
typename >
class ALLOC >
422 INLINE HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > >
423 DBTranslator4DiscretizedVariable< ALLOC >::reorder() {
424 return HashTable< std::size_t,
426 ALLOC< std::pair< std::size_t, std::size_t > > >();
431 template <
template <
typename >
class ALLOC >
432 INLINE std::size_t DBTranslator4DiscretizedVariable< ALLOC >::domainSize()
const {
433 return _variable_.domainSize();
438 template <
template <
typename >
class ALLOC >
439 INLINE
const IDiscretizedVariable* DBTranslator4DiscretizedVariable< ALLOC >::variable()
const {
440 return _real_variable_;
445 template <
template <
typename >
class ALLOC >
446 INLINE DBTranslatedValue DBTranslator4DiscretizedVariable< ALLOC >::missingValue()
const {
447 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};