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&
51 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
56 variable__(var.name(), var.description()) {
58 if (var.domainSize() > max_dico_entries) {
60 "the dictionary induced by the variable is too large");
64 const auto& ticks = var.ticks();
65 for (
const auto tick: ticks) {
66 variable__.addTick((
float)tick);
70 const float lower_bound = (
float)ticks[0];
71 const float upper_bound = (
float)ticks.back();
75 for (
auto iter =
this->missing_symbols_.beginSafe();
76 iter !=
this->missing_symbols_.endSafe();
78 if (DBCell::isReal(*iter)) {
79 const float missing_val = std::stof(*iter);
80 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
81 this->missing_symbols_.erase(iter);
88 for (
const auto& label: var.labels()) {
94 if (
this->missing_symbols_.exists(label)) {
95 this->missing_symbols_.erase(label);
98 this->back_dico_.insert(size, label);
103 real_variable__ = var.clone();
105 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
110 template <
template <
typename >
class ALLOC >
111 template <
template <
typename >
class XALLOC >
112 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
113 const IDiscretizedVariable& var,
114 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
115 std::size_t max_dico_entries,
116 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
118 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
123 variable__(var.name(), var.description()) {
125 if (var.domainSize() > max_dico_entries) {
127 "the dictionary induced by the variable is too large");
131 const auto ticks = var.ticksAsDoubles();
132 for (
const auto tick: ticks) {
133 variable__.addTick((
float)tick);
137 const float lower_bound =
float(ticks[0]);
138 const float upper_bound =
float(ticks.back());
142 for (
auto iter =
this->missing_symbols_.beginSafe();
143 iter !=
this->missing_symbols_.endSafe();
145 if (DBCell::isReal(*iter)) {
146 const float missing_val = std::stof(*iter);
147 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
148 this->missing_symbols_.erase(iter);
154 std::size_t size = 0;
155 for (
const auto& label: var.labels()) {
161 if (
this->missing_symbols_.exists(label)) {
162 this->missing_symbols_.erase(label);
165 this->back_dico_.insert(size, label);
170 real_variable__ = var.clone();
172 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
177 template <
template <
typename >
class ALLOC >
178 template <
typename GUM_SCALAR >
179 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
180 const DiscretizedVariable< GUM_SCALAR >& var,
181 std::size_t max_dico_entries,
182 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
184 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
188 variable__(var.name(), var.description()) {
190 if (var.domainSize() > max_dico_entries) {
192 "the dictionary induced by the variable is too large");
196 const auto& ticks = var.ticks();
197 for (
const auto tick: ticks) {
198 variable__.addTick((
float)tick);
202 std::size_t size = 0;
203 for (
const auto& label: var.labels()) {
204 this->back_dico_.insert(size, label);
209 real_variable__ = var.clone();
211 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
216 template <
template <
typename >
class ALLOC >
217 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
218 const IDiscretizedVariable& var,
219 std::size_t max_dico_entries,
220 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
222 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
226 variable__(var.name(), var.description()) {
228 if (var.domainSize() > max_dico_entries) {
230 "the dictionary induced by the variable is too large");
234 const auto ticks = var.ticksAsDoubles();
235 for (
const auto tick: ticks) {
236 variable__.addTick((
float)tick);
240 std::size_t size = 0;
241 for (
const auto& label: var.labels()) {
242 this->back_dico_.insert(size, label);
247 real_variable__ = var.clone();
249 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
254 template <
template <
typename >
class ALLOC >
255 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
256 const DBTranslator4DiscretizedVariable< ALLOC >& from,
257 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
259 DBTranslator< ALLOC >(from, alloc),
260 variable__(from.variable__) {
262 real_variable__ = from.real_variable__->clone();
264 GUM_CONS_CPY(DBTranslator4DiscretizedVariable);
269 template <
template <
typename >
class ALLOC >
270 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
271 const DBTranslator4DiscretizedVariable< ALLOC >& from) :
272 DBTranslator4DiscretizedVariable< ALLOC >(from, from.getAllocator()) {}
276 template <
template <
typename >
class ALLOC >
277 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
278 DBTranslator4DiscretizedVariable< ALLOC >&& from,
279 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
281 DBTranslator< ALLOC >(std::move(from), alloc),
282 variable__(std::move(from.variable__)) {
284 real_variable__ = from.real_variable__;
285 from.real_variable__ =
nullptr;
287 GUM_CONS_MOV(DBTranslator4DiscretizedVariable);
292 template <
template <
typename >
class ALLOC >
293 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
294 DBTranslator4DiscretizedVariable< ALLOC >&& from) :
295 DBTranslator4DiscretizedVariable< ALLOC >(std::move(from),
296 from.getAllocator()) {}
300 template <
template <
typename >
class ALLOC >
301 DBTranslator4DiscretizedVariable< ALLOC >*
302 DBTranslator4DiscretizedVariable< ALLOC >::clone(
303 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
305 ALLOC< DBTranslator4DiscretizedVariable< ALLOC > > allocator(alloc);
306 DBTranslator4DiscretizedVariable< ALLOC >* translator
307 = allocator.allocate(1);
309 allocator.construct(translator, *
this, alloc);
311 allocator.deallocate(translator, 1);
319 template <
template <
typename >
class ALLOC >
320 INLINE DBTranslator4DiscretizedVariable< ALLOC >*
321 DBTranslator4DiscretizedVariable< ALLOC >::clone()
const {
322 return clone(
this->getAllocator());
327 template <
template <
typename >
class ALLOC >
328 INLINE DBTranslator4DiscretizedVariable<
329 ALLOC >::~DBTranslator4DiscretizedVariable() {
330 if (real_variable__ !=
nullptr)
delete real_variable__;
332 GUM_DESTRUCTOR(DBTranslator4DiscretizedVariable);
337 template <
template <
typename >
class ALLOC >
338 DBTranslator4DiscretizedVariable< ALLOC >&
339 DBTranslator4DiscretizedVariable< ALLOC >::operator=(
340 const DBTranslator4DiscretizedVariable< ALLOC >& from) {
342 DBTranslator< ALLOC >::operator=(from);
343 variable__ = from.variable__;
345 if (real_variable__ !=
nullptr)
delete real_variable__;
346 real_variable__ = from.real_variable__->clone();
354 template <
template <
typename >
class ALLOC >
355 DBTranslator4DiscretizedVariable< ALLOC >&
356 DBTranslator4DiscretizedVariable< ALLOC >::operator=(
357 DBTranslator4DiscretizedVariable< ALLOC >&& from) {
359 DBTranslator< ALLOC >::operator=(std::move(from));
360 variable__ = std::move(from.variable__);
362 if (real_variable__ !=
nullptr)
delete real_variable__;
363 real_variable__ = from.real_variable__;
364 from.real_variable__ =
nullptr;
372 template <
template <
typename >
class ALLOC >
373 INLINE DBTranslatedValue DBTranslator4DiscretizedVariable< ALLOC >::translate(
374 const std::string& str) {
377 return DBTranslatedValue{std::size_t(variable__[str])};
378 }
catch (gum::Exception&) {
380 if (
this->isMissingSymbol(str))
381 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
387 return DBTranslatedValue{
this->back_dico_.first(str)};
388 }
catch (gum::Exception&) {
389 if (!DBCell::isReal(str)) {
393 <<
"\" cannot be translated because it is not a number");
395 GUM_ERROR(UnknownLabelInDatabase,
396 "The translation of \"" << str <<
"\" could not be found");
404 template <
template <
typename >
class ALLOC >
405 INLINE std::string DBTranslator4DiscretizedVariable< ALLOC >::translateBack(
406 const DBTranslatedValue translated_val)
const {
408 return this->back_dico_.second(translated_val.discr_val);
409 }
catch (Exception&) {
411 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
412 && !
this->missing_symbols_.empty())
413 return *(
this->missing_symbols_.begin());
415 GUM_ERROR(UnknownLabelInDatabase,
416 "The back translation of \"" << translated_val.discr_val
417 <<
"\" could not be found");
423 template <
template <
typename >
class ALLOC >
425 DBTranslator4DiscretizedVariable< ALLOC >::hasEditableDictionary()
const {
431 template <
template <
typename >
class ALLOC >
433 DBTranslator4DiscretizedVariable< ALLOC >::setEditableDictionaryMode(
bool) {
438 template <
template <
typename >
class ALLOC >
439 bool DBTranslator4DiscretizedVariable< ALLOC >::needsReordering()
const {
445 template <
template <
typename >
class ALLOC >
446 INLINE HashTable< std::size_t,
448 ALLOC< std::pair< std::size_t, std::size_t > > >
449 DBTranslator4DiscretizedVariable< ALLOC >::reorder() {
450 return HashTable< std::size_t,
452 ALLOC< std::pair< std::size_t, std::size_t > > >();
457 template <
template <
typename >
class ALLOC >
459 DBTranslator4DiscretizedVariable< ALLOC >::domainSize()
const {
460 return variable__.domainSize();
465 template <
template <
typename >
class ALLOC >
466 INLINE
const IDiscretizedVariable*
467 DBTranslator4DiscretizedVariable< ALLOC >::variable()
const {
468 return real_variable__;
473 template <
template <
typename >
class ALLOC >
474 INLINE DBTranslatedValue
475 DBTranslator4DiscretizedVariable< ALLOC >::missingValue()
const {
476 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};