37 #ifndef DOXYGEN_SHOULD_SKIP_THIS 45 template <
template <
typename >
class ALLOC >
46 template <
template <
typename >
class XALLOC >
48 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
49 std::size_t max_dico_entries,
56 __variable(
"var",
"", 1, 0) {
61 bool non_int_symbol_found =
false;
64 __status_int_missing_symbols.insert(symbol,
false);
65 }
else if (!non_int_symbol_found) {
66 non_int_symbol_found =
true;
67 __nonint_missing_symbol = symbol;
76 template <
template <
typename >
class ALLOC >
78 std::size_t max_dico_entries,
82 __variable(
"var",
"", 1, 0) {
88 template <
template <
typename >
class ALLOC >
89 template <
template <
typename >
class XALLOC >
91 const RangeVariable& var,
92 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
93 const bool editable_dictionary,
94 std::size_t max_dico_entries,
103 const long lower_bound = var.minVal();
104 const long upper_bound = var.maxVal();
107 if ((upper_bound >= lower_bound)
108 && (std::size_t(upper_bound - lower_bound + 1)
111 "the dictionary induced by the variable is too large");
117 if (lower_bound <= upper_bound) {
118 for (
auto iter = this->_missing_symbols.beginSafe();
119 iter != this->_missing_symbols.endSafe();
122 const long missing_val = std::stol(*iter);
123 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
124 this->_missing_symbols.erase(iter);
131 std::size_t size = 0;
132 for (
const auto& label: var.labels()) {
142 bool non_int_symbol_found =
false;
143 for (
const auto& symbol: this->_missing_symbols) {
145 __status_int_missing_symbols.insert(symbol,
false);
146 }
else if (!non_int_symbol_found) {
147 non_int_symbol_found =
true;
148 __nonint_missing_symbol = symbol;
157 template <
template <
typename >
class ALLOC >
159 const RangeVariable& var,
160 const bool editable_dictionary,
161 std::size_t max_dico_entries,
169 const long lower_bound = var.minVal();
170 const long upper_bound = var.maxVal();
173 if ((upper_bound >= lower_bound)
174 && (std::size_t(upper_bound - lower_bound + 1)
177 "the dictionary induced by the variable is too large");
181 std::size_t size = 0;
182 for (
const auto& label: var.labels()) {
193 template <
template <
typename >
class ALLOC >
195 const DBTranslator4RangeVariable< ALLOC >& from,
198 __variable(from.__variable),
199 __status_int_missing_symbols(from.__status_int_missing_symbols),
200 __translated_int_missing_symbols(from.__translated_int_missing_symbols),
201 __nonint_missing_symbol(from.__nonint_missing_symbol) {
207 template <
template <
typename >
class ALLOC >
209 const DBTranslator4RangeVariable< ALLOC >& from) :
214 template <
template <
typename >
class ALLOC >
216 DBTranslator4RangeVariable< ALLOC >&& from,
219 __variable(
std::move(from.__variable)),
220 __status_int_missing_symbols(
std::move(from.__status_int_missing_symbols)),
221 __translated_int_missing_symbols(
222 std::move(from.__translated_int_missing_symbols)),
223 __nonint_missing_symbol(
std::move(from.__nonint_missing_symbol)) {
229 template <
template <
typename >
class ALLOC >
231 DBTranslator4RangeVariable< ALLOC >&& from) :
237 template <
template <
typename >
class ALLOC >
238 DBTranslator4RangeVariable< ALLOC >*
242 ALLOC< DBTranslator4RangeVariable< ALLOC > > allocator(alloc);
243 DBTranslator4RangeVariable< ALLOC >* translator = allocator.allocate(1);
245 allocator.construct(translator, *
this, alloc);
247 allocator.deallocate(translator, 1);
255 template <
template <
typename >
class ALLOC >
256 INLINE DBTranslator4RangeVariable< ALLOC >*
263 template <
template <
typename >
class ALLOC >
270 template <
template <
typename >
class ALLOC >
271 DBTranslator4RangeVariable< ALLOC >&
273 const DBTranslator4RangeVariable< ALLOC >& from) {
276 __variable = from.__variable;
277 __status_int_missing_symbols = from.__status_int_missing_symbols;
278 __translated_int_missing_symbols = from.__translated_int_missing_symbols;
279 __nonint_missing_symbol = from.__nonint_missing_symbol;
287 template <
template <
typename >
class ALLOC >
288 DBTranslator4RangeVariable< ALLOC >&
290 DBTranslator4RangeVariable< ALLOC >&& from) {
293 __variable = std::move(from.__variable);
294 __status_int_missing_symbols =
295 std::move(from.__status_int_missing_symbols);
296 __translated_int_missing_symbols =
297 std::move(from.__translated_int_missing_symbols);
298 __nonint_missing_symbol = std::move(from.__nonint_missing_symbol);
306 template <
template <
typename >
class ALLOC >
319 const bool is_str_translated = __status_int_missing_symbols[str];
320 if (!is_str_translated) {
321 __status_int_missing_symbols[str] =
true;
322 __translated_int_missing_symbols.insert(std::stol(str));
325 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
331 "The translation of String \"" << str
332 <<
"\" could not be found");
338 "String \"" << str <<
"\" cannot be translated because " 339 <<
"it cannot be converted into an integer");
341 const long new_value = std::stol(str);
345 if (__translated_int_missing_symbols.exists(new_value)) {
349 << str <<
"\" cannot be translated because " 350 <<
"it corresponds to an already translated missing symbol");
360 if (__variable.minVal() > __variable.maxVal()) {
363 "String \"" << str <<
"\" cannot be translated because " 364 <<
"the dictionary is already full");
366 __variable.setMinVal(new_value);
367 __variable.setMaxVal(new_value);
369 return DBTranslatedValue{std::size_t(0)};
376 const long lower_bound = __variable.minVal();
377 const long upper_bound = __variable.maxVal();
379 std::size_t size = upper_bound - lower_bound + 1;
381 if (new_value < __variable.minVal()) {
384 "String \"" << str <<
"\" cannot be translated because " 385 <<
"the dictionary is already full");
389 for (
const auto& missing: __translated_int_missing_symbols) {
390 if ((missing >= new_value) && (missing <= upper_bound)) {
393 << str <<
"\" cannot be translated " 394 <<
"because it would induce a new range containing " 395 <<
"an already translated missing symbol");
401 for (
auto iter = __status_int_missing_symbols.beginSafe();
402 iter != __status_int_missing_symbols.endSafe();
404 if (iter.val() ==
false) {
405 const long missing = std::stol(iter.key());
406 if ((missing >= new_value) && (missing <= upper_bound)) {
407 this->_missing_symbols.erase(iter.key());
408 __status_int_missing_symbols.erase(iter);
414 const std::size_t index = size;
415 for (
long i = new_value; i < __variable.minVal(); ++i) {
419 __variable.setMinVal(new_value);
421 return DBTranslatedValue{index};
425 "String \"" << str <<
"\" cannot be translated because " 426 <<
"the dictionary is already full");
430 for (
const auto& missing: __translated_int_missing_symbols) {
431 if ((missing <= new_value) && (missing >= lower_bound)) {
434 << str <<
"\" cannot be translated " 435 <<
"because it would induce a new range containing " 436 <<
"an already translated missing symbol");
442 for (
auto iter = __status_int_missing_symbols.beginSafe();
443 iter != __status_int_missing_symbols.endSafe();
445 if (iter.val() ==
false) {
446 const long missing = std::stol(iter.key());
447 if ((missing <= new_value) && (missing >= lower_bound)) {
448 this->_missing_symbols.erase(iter.key());
449 __status_int_missing_symbols.erase(iter);
455 for (
long i = __variable.maxVal() + 1; i <= new_value; ++i) {
459 __variable.setMaxVal(new_value);
461 return DBTranslatedValue{size - std::size_t(1)};
468 template <
template <
typename >
class ALLOC >
470 const DBTranslatedValue translated_val)
const {
473 }
catch (Exception&) {
475 if (translated_val.discr_val
476 == std::numeric_limits< std::size_t >::max()) {
477 if (!__nonint_missing_symbol.empty())
return __nonint_missing_symbol;
478 if (this->_missing_symbols.empty())
479 return *(this->_missing_symbols.begin());
483 "The back translation of \"" << translated_val.discr_val
484 <<
"\" could not be found");
490 template <
template <
typename >
class ALLOC >
493 const auto& labels = __variable.labels();
494 std::size_t last_number = std::numeric_limits< std::size_t >::lowest();
496 for (
const auto& label: labels) {
498 if (number < last_number)
return true;
499 last_number = number;
507 template <
template <
typename >
class ALLOC >
508 INLINE HashTable< std::size_t,
510 ALLOC< std::pair< std::size_t, std::size_t > > >
513 const auto& labels = __variable.labels();
514 const std::size_t size = labels.size();
515 std::vector< std::pair< std::size_t, std::string >,
516 ALLOC< std::pair< std::size_t, std::string > > >
518 xlabels.reserve(size);
519 bool modifications =
false;
520 for (std::size_t i = std::size_t(0); i < size; ++i) {
522 xlabels.push_back(std::make_pair(old_val, labels[i]));
523 if (old_val != i) modifications =
true;
528 if (!modifications) {
529 return HashTable< std::size_t,
531 ALLOC< std::pair< std::size_t, std::size_t > > >();
537 HashTable< std::size_t,
539 ALLOC< std::pair< std::size_t, std::size_t > > >
541 for (std::size_t i = std::size_t(0); i < size; ++i) {
542 mapping.insert(xlabels[i].first, i);
551 template <
template <
typename >
class ALLOC >
553 return __variable.domainSize();
558 template <
template <
typename >
class ALLOC >
559 INLINE
const RangeVariable*
566 template <
template <
typename >
class ALLOC >
567 INLINE DBTranslatedValue
569 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
Bijection< std::size_t, std::string, ALLOC< std::pair< float, std::string > > > _back_dico
the bijection relating back translated values and their original strings.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
virtual bool needsReordering() const final
indicates whether a reordering is needed to make the translations sorted by increasing numbers ...
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
void clear()
Removes all the associations from the gum::Bijection.
virtual const RangeVariable * variable() const final
returns the variable stored into the translator
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
std::size_t _max_dico_entries
the maximum number of entries that the dictionary is allowed to contain
static bool isInteger(const std::string &str)
determines whether a string corresponds precisely to an integer
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
DBTranslator4RangeVariable< ALLOC > & operator=(const DBTranslator4RangeVariable< ALLOC > &from)
copy operator
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
DBTranslator(DBTranslatedValueType val_type, const std::vector< std::string, XALLOC< std::string > > &missing_symbols, const bool editable_dictionary=true, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max(), const allocator_type &alloc=allocator_type())
default constructor
virtual ~DBTranslator4RangeVariable()
destructor
virtual DBTranslatedValue missingValue() const final
returns the translation of a missing value
allocator_type getAllocator() const
returns the allocator used by the translator
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
std::string to_string(const Formula &f)
Set< std::string, ALLOC< std::string > > _missing_symbols
the set of missing symbols
DBTranslator< ALLOC > & operator=(const DBTranslator< ALLOC > &from)
copy operator
virtual DBTranslatedValue translate(const std::string &str) final
returns the translation of a string
virtual HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > reorder() final
performs a reordering of the dictionary and returns a mapping from the old translated values to the n...
Base class for all aGrUM's exceptions.
virtual std::size_t domainSize() const final
returns the domain size of a variable corresponding to the translations
virtual bool hasEditableDictionary() const
indicates whether the translator has an editable dictionary or not
bool isMissingSymbol(const std::string &str) const
indicates whether a string corresponds to a missing symbol
typename DBTranslator< ALLOC >::allocator_type allocator_type
type for the allocators passed in arguments of methods
virtual DBTranslator4RangeVariable< ALLOC > * clone() const
virtual copy constructor
std::size_t Size
In aGrUM, hashed values are unsigned long int.
DBTranslator4RangeVariable(const std::vector< std::string, XALLOC< std::string > > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max(), const allocator_type &alloc=allocator_type())
default constructor without any initial variable
#define GUM_ERROR(type, msg)