34 #ifndef DOXYGEN_SHOULD_SKIP_THIS 42 template <
template <
typename >
class ALLOC >
43 template <
template <
typename >
class XALLOC >
45 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
46 std::size_t max_dico_entries,
53 __variable(
"var",
"", 1, 0) {
58 bool non_int_symbol_found =
false;
61 __status_int_missing_symbols.insert(symbol,
false);
62 }
else if (!non_int_symbol_found) {
63 non_int_symbol_found =
true;
64 __nonint_missing_symbol = symbol;
73 template <
template <
typename >
class ALLOC >
75 std::size_t max_dico_entries,
79 __variable(
"var",
"", 1, 0) {
85 template <
template <
typename >
class ALLOC >
86 template <
template <
typename >
class XALLOC >
88 const RangeVariable& var,
89 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
90 const bool editable_dictionary,
91 std::size_t max_dico_entries,
100 const long lower_bound = var.minVal();
101 const long upper_bound = var.maxVal();
104 if ((upper_bound >= lower_bound)
105 && (std::size_t(upper_bound - lower_bound + 1)
108 "the dictionary induced by the variable is too large");
114 if (lower_bound <= upper_bound) {
115 for (
auto iter = this->_missing_symbols.beginSafe();
116 iter != this->_missing_symbols.endSafe();
119 const long missing_val = std::stol(*iter);
120 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
121 this->_missing_symbols.erase(iter);
128 std::size_t size = 0;
129 for (
const auto& label : var.labels()) {
139 bool non_int_symbol_found =
false;
140 for (
const auto& symbol : this->_missing_symbols) {
142 __status_int_missing_symbols.insert(symbol,
false);
143 }
else if (!non_int_symbol_found) {
144 non_int_symbol_found =
true;
145 __nonint_missing_symbol = symbol;
154 template <
template <
typename >
class ALLOC >
156 const RangeVariable& var,
157 const bool editable_dictionary,
158 std::size_t max_dico_entries,
166 const long lower_bound = var.minVal();
167 const long upper_bound = var.maxVal();
170 if ((upper_bound >= lower_bound)
171 && (std::size_t(upper_bound - lower_bound + 1)
174 "the dictionary induced by the variable is too large");
178 std::size_t size = 0;
179 for (
const auto& label : var.labels()) {
190 template <
template <
typename >
class ALLOC >
192 const DBTranslator4RangeVariable< ALLOC >& from,
195 __variable(from.__variable),
196 __status_int_missing_symbols(from.__status_int_missing_symbols),
197 __translated_int_missing_symbols(from.__translated_int_missing_symbols),
198 __nonint_missing_symbol(from.__nonint_missing_symbol) {
204 template <
template <
typename >
class ALLOC >
206 const DBTranslator4RangeVariable< ALLOC >& from) :
211 template <
template <
typename >
class ALLOC >
213 DBTranslator4RangeVariable< ALLOC >&& from,
216 __variable(
std::move(from.__variable)),
217 __status_int_missing_symbols(
std::move(from.__status_int_missing_symbols)),
218 __translated_int_missing_symbols(
219 std::move(from.__translated_int_missing_symbols)),
220 __nonint_missing_symbol(
std::move(from.__nonint_missing_symbol)) {
226 template <
template <
typename >
class ALLOC >
228 DBTranslator4RangeVariable< ALLOC >&& from) :
234 template <
template <
typename >
class ALLOC >
235 DBTranslator4RangeVariable< ALLOC >*
239 ALLOC< DBTranslator4RangeVariable< ALLOC > > allocator(alloc);
240 DBTranslator4RangeVariable< ALLOC >* translator = allocator.allocate(1);
242 allocator.construct(translator, *
this, alloc);
244 allocator.deallocate(translator, 1);
252 template <
template <
typename >
class ALLOC >
253 INLINE DBTranslator4RangeVariable< ALLOC >*
260 template <
template <
typename >
class ALLOC >
267 template <
template <
typename >
class ALLOC >
269 operator=(
const DBTranslator4RangeVariable< ALLOC >& from) {
272 __variable = from.__variable;
273 __status_int_missing_symbols = from.__status_int_missing_symbols;
274 __translated_int_missing_symbols = from.__translated_int_missing_symbols;
275 __nonint_missing_symbol = from.__nonint_missing_symbol;
283 template <
template <
typename >
class ALLOC >
285 operator=(DBTranslator4RangeVariable< ALLOC >&& from) {
288 __variable = std::move(from.__variable);
289 __status_int_missing_symbols =
290 std::move(from.__status_int_missing_symbols);
291 __translated_int_missing_symbols =
292 std::move(from.__translated_int_missing_symbols);
293 __nonint_missing_symbol = std::move(from.__nonint_missing_symbol);
301 template <
template <
typename >
class ALLOC >
314 const bool is_str_translated = __status_int_missing_symbols[str];
315 if (!is_str_translated) {
316 __status_int_missing_symbols[str] =
true;
317 __translated_int_missing_symbols.insert(std::stol(str));
320 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
326 "The translation of String \"" << str
327 <<
"\" could not be found");
333 "String \"" << str <<
"\" cannot be translated because " 334 <<
"it cannot be converted into an integer");
336 const long new_value = std::stol(str);
340 if (__translated_int_missing_symbols.exists(new_value)) {
344 << str <<
"\" cannot be translated because " 345 <<
"it corresponds to an already translated missing symbol");
355 if (__variable.minVal() > __variable.maxVal()) {
358 "String \"" << str <<
"\" cannot be translated because " 359 <<
"the dictionary is already full");
361 __variable.setMinVal(new_value);
362 __variable.setMaxVal(new_value);
364 return DBTranslatedValue{std::size_t(0)};
371 const long lower_bound = __variable.minVal();
372 const long upper_bound = __variable.maxVal();
374 std::size_t size = upper_bound - lower_bound + 1;
376 if (new_value < __variable.minVal()) {
379 "String \"" << str <<
"\" cannot be translated because " 380 <<
"the dictionary is already full");
384 for (
const auto& missing : __translated_int_missing_symbols) {
385 if ((missing >= new_value) && (missing <= upper_bound)) {
388 << str <<
"\" cannot be translated " 389 <<
"because it would induce a new range containing " 390 <<
"an already translated missing symbol");
396 for (
auto iter = __status_int_missing_symbols.beginSafe();
397 iter != __status_int_missing_symbols.endSafe();
399 if (iter.val() ==
false) {
400 const long missing = std::stol(iter.key());
401 if ((missing >= new_value) && (missing <= upper_bound)) {
402 this->_missing_symbols.erase(iter.key());
403 __status_int_missing_symbols.erase(iter);
409 const std::size_t index = size;
410 for (
long i = new_value; i < __variable.minVal(); ++i) {
414 __variable.setMinVal(new_value);
416 return DBTranslatedValue{index};
420 "String \"" << str <<
"\" cannot be translated because " 421 <<
"the dictionary is already full");
425 for (
const auto& missing : __translated_int_missing_symbols) {
426 if ((missing <= new_value) && (missing >= lower_bound)) {
429 << str <<
"\" cannot be translated " 430 <<
"because it would induce a new range containing " 431 <<
"an already translated missing symbol");
437 for (
auto iter = __status_int_missing_symbols.beginSafe();
438 iter != __status_int_missing_symbols.endSafe();
440 if (iter.val() ==
false) {
441 const long missing = std::stol(iter.key());
442 if ((missing <= new_value) && (missing >= lower_bound)) {
443 this->_missing_symbols.erase(iter.key());
444 __status_int_missing_symbols.erase(iter);
450 for (
long i = __variable.maxVal() + 1; i <= new_value; ++i) {
454 __variable.setMaxVal(new_value);
456 return DBTranslatedValue{size - std::size_t(1)};
463 template <
template <
typename >
class ALLOC >
465 const DBTranslatedValue translated_val)
const {
468 }
catch (Exception&) {
470 if (translated_val.discr_val
471 == std::numeric_limits< std::size_t >::max()) {
472 if (!__nonint_missing_symbol.empty())
return __nonint_missing_symbol;
473 if (this->_missing_symbols.empty())
474 return *(this->_missing_symbols.begin());
478 "The back translation of \"" << translated_val.discr_val
479 <<
"\" could not be found");
485 template <
template <
typename >
class ALLOC >
488 const auto& labels = __variable.labels();
489 std::size_t last_number = std::numeric_limits< std::size_t >::lowest();
491 for (
const auto& label : labels) {
493 if (number < last_number)
return true;
494 last_number = number;
502 template <
template <
typename >
class ALLOC >
503 INLINE HashTable< std::size_t,
505 ALLOC< std::pair< std::size_t, std::size_t > > >
508 const auto& labels = __variable.labels();
509 const std::size_t size = labels.size();
510 std::vector< std::pair< std::size_t, std::string >,
511 ALLOC< std::pair< std::size_t, std::string > > >
513 xlabels.reserve(size);
514 bool modifications =
false;
515 for (std::size_t i = std::size_t(0); i < size; ++i) {
517 xlabels.push_back(std::make_pair(old_val, labels[i]));
518 if (old_val != i) modifications =
true;
523 if (!modifications) {
524 return HashTable< std::size_t,
526 ALLOC< std::pair< std::size_t, std::size_t > > >();
532 HashTable< std::size_t,
534 ALLOC< std::pair< std::size_t, std::size_t > > >
536 for (std::size_t i = std::size_t(0); i < size; ++i) {
537 mapping.insert(xlabels[i].first, i);
546 template <
template <
typename >
class ALLOC >
548 return __variable.domainSize();
553 template <
template <
typename >
class ALLOC >
554 INLINE
const RangeVariable*
561 template <
template <
typename >
class ALLOC >
562 INLINE DBTranslatedValue
564 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
The databases' cell translators for range variables.
DBTranslator4RangeVariable< ALLOC > & operator=(const DBTranslator4RangeVariable< ALLOC > &from)
copy operator
gum is the global namespace for all aGrUM entities
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
The class representing the original values of the cells of databases.
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)