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 >
272 operator=(
const DBTranslator4RangeVariable< ALLOC >& from) {
275 __variable = from.__variable;
276 __status_int_missing_symbols = from.__status_int_missing_symbols;
277 __translated_int_missing_symbols = from.__translated_int_missing_symbols;
278 __nonint_missing_symbol = from.__nonint_missing_symbol;
286 template <
template <
typename >
class ALLOC >
288 operator=(DBTranslator4RangeVariable< ALLOC >&& from) {
291 __variable = std::move(from.__variable);
292 __status_int_missing_symbols =
293 std::move(from.__status_int_missing_symbols);
294 __translated_int_missing_symbols =
295 std::move(from.__translated_int_missing_symbols);
296 __nonint_missing_symbol = std::move(from.__nonint_missing_symbol);
304 template <
template <
typename >
class ALLOC >
317 const bool is_str_translated = __status_int_missing_symbols[str];
318 if (!is_str_translated) {
319 __status_int_missing_symbols[str] =
true;
320 __translated_int_missing_symbols.insert(std::stol(str));
323 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
329 "The translation of String \"" << str
330 <<
"\" could not be found");
336 "String \"" << str <<
"\" cannot be translated because " 337 <<
"it cannot be converted into an integer");
339 const long new_value = std::stol(str);
343 if (__translated_int_missing_symbols.exists(new_value)) {
347 << str <<
"\" cannot be translated because " 348 <<
"it corresponds to an already translated missing symbol");
358 if (__variable.minVal() > __variable.maxVal()) {
361 "String \"" << str <<
"\" cannot be translated because " 362 <<
"the dictionary is already full");
364 __variable.setMinVal(new_value);
365 __variable.setMaxVal(new_value);
367 return DBTranslatedValue{std::size_t(0)};
374 const long lower_bound = __variable.minVal();
375 const long upper_bound = __variable.maxVal();
377 std::size_t size = upper_bound - lower_bound + 1;
379 if (new_value < __variable.minVal()) {
382 "String \"" << str <<
"\" cannot be translated because " 383 <<
"the dictionary is already full");
387 for (
const auto& missing : __translated_int_missing_symbols) {
388 if ((missing >= new_value) && (missing <= upper_bound)) {
391 << str <<
"\" cannot be translated " 392 <<
"because it would induce a new range containing " 393 <<
"an already translated missing symbol");
399 for (
auto iter = __status_int_missing_symbols.beginSafe();
400 iter != __status_int_missing_symbols.endSafe();
402 if (iter.val() ==
false) {
403 const long missing = std::stol(iter.key());
404 if ((missing >= new_value) && (missing <= upper_bound)) {
405 this->_missing_symbols.erase(iter.key());
406 __status_int_missing_symbols.erase(iter);
412 const std::size_t index = size;
413 for (
long i = new_value; i < __variable.minVal(); ++i) {
417 __variable.setMinVal(new_value);
419 return DBTranslatedValue{index};
423 "String \"" << str <<
"\" cannot be translated because " 424 <<
"the dictionary is already full");
428 for (
const auto& missing : __translated_int_missing_symbols) {
429 if ((missing <= new_value) && (missing >= lower_bound)) {
432 << str <<
"\" cannot be translated " 433 <<
"because it would induce a new range containing " 434 <<
"an already translated missing symbol");
440 for (
auto iter = __status_int_missing_symbols.beginSafe();
441 iter != __status_int_missing_symbols.endSafe();
443 if (iter.val() ==
false) {
444 const long missing = std::stol(iter.key());
445 if ((missing <= new_value) && (missing >= lower_bound)) {
446 this->_missing_symbols.erase(iter.key());
447 __status_int_missing_symbols.erase(iter);
453 for (
long i = __variable.maxVal() + 1; i <= new_value; ++i) {
457 __variable.setMaxVal(new_value);
459 return DBTranslatedValue{size - std::size_t(1)};
466 template <
template <
typename >
class ALLOC >
468 const DBTranslatedValue translated_val)
const {
471 }
catch (Exception&) {
473 if (translated_val.discr_val
474 == std::numeric_limits< std::size_t >::max()) {
475 if (!__nonint_missing_symbol.empty())
return __nonint_missing_symbol;
476 if (this->_missing_symbols.empty())
477 return *(this->_missing_symbols.begin());
481 "The back translation of \"" << translated_val.discr_val
482 <<
"\" could not be found");
488 template <
template <
typename >
class ALLOC >
491 const auto& labels = __variable.labels();
492 std::size_t last_number = std::numeric_limits< std::size_t >::lowest();
494 for (
const auto& label : labels) {
496 if (number < last_number)
return true;
497 last_number = number;
505 template <
template <
typename >
class ALLOC >
506 INLINE HashTable< std::size_t,
508 ALLOC< std::pair< std::size_t, std::size_t > > >
511 const auto& labels = __variable.labels();
512 const std::size_t size = labels.size();
513 std::vector< std::pair< std::size_t, std::string >,
514 ALLOC< std::pair< std::size_t, std::string > > >
516 xlabels.reserve(size);
517 bool modifications =
false;
518 for (std::size_t i = std::size_t(0); i < size; ++i) {
520 xlabels.push_back(std::make_pair(old_val, labels[i]));
521 if (old_val != i) modifications =
true;
526 if (!modifications) {
527 return HashTable< std::size_t,
529 ALLOC< std::pair< std::size_t, std::size_t > > >();
535 HashTable< std::size_t,
537 ALLOC< std::pair< std::size_t, std::size_t > > >
539 for (std::size_t i = std::size_t(0); i < size; ++i) {
540 mapping.insert(xlabels[i].first, i);
549 template <
template <
typename >
class ALLOC >
551 return __variable.domainSize();
556 template <
template <
typename >
class ALLOC >
557 INLINE
const RangeVariable*
564 template <
template <
typename >
class ALLOC >
565 INLINE DBTranslatedValue
567 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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
DBTranslator4RangeVariable< ALLOC > & operator=(const DBTranslator4RangeVariable< ALLOC > &from)
copy operator
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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)