25 #ifndef DOXYGEN_SHOULD_SKIP_THIS 40 template <
template <
typename >
class ALLOC >
41 template <
template <
typename >
class XALLOC >
43 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
52 __variable(
"var",
""), __fit_range(fit_range) {
76 __variable.setLowerBound(std::numeric_limits< float >::infinity());
79 __real_variable = __variable.clone();
86 template <
template <
typename >
class ALLOC >
93 __variable(
"var",
""), __fit_range(fit_range) {
98 __variable.setLowerBound(std::numeric_limits< float >::infinity());
101 __real_variable = __variable.clone();
108 template <
template <
typename >
class ALLOC >
109 template <
typename GUM_SCALAR,
template <
typename >
class XALLOC >
111 const ContinuousVariable< GUM_SCALAR >& var,
112 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
113 const bool fit_range,
121 __variable(var.name(), var.description()), __fit_range(fit_range) {
123 const float lower_bound = float(var.lowerBound());
124 const float upper_bound = float(var.upperBound());
125 __variable.setLowerBound(lower_bound);
126 __variable.setUpperBound(upper_bound);
130 bool non_float_symbol_found =
false;
135 const float missing_val = std::stof(*iter);
136 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
139 __status_float_missing_symbols.insert(*iter,
false);
140 }
else if (!non_float_symbol_found) {
141 non_float_symbol_found =
true;
142 __nonfloat_missing_symbol = *iter;
147 __real_variable = var.clone();
154 template <
template <
typename >
class ALLOC >
155 template <
typename GUM_SCALAR >
157 const ContinuousVariable< GUM_SCALAR >& var,
158 const bool fit_range,
163 __variable(var.name(), var.description()), __fit_range(fit_range) {
165 const float lower_bound = float(var.lowerBound());
166 const float upper_bound = float(var.upperBound());
167 __variable.setLowerBound(lower_bound);
168 __variable.setUpperBound(upper_bound);
171 __real_variable = var.clone();
178 template <
template <
typename >
class ALLOC >
179 template <
template <
typename >
class XALLOC >
181 const IContinuousVariable& var,
182 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
183 const bool fit_range,
191 __variable(var.name(), var.description()), __fit_range(fit_range) {
193 const float lower_bound = float(var.lowerBoundAsDouble());
194 const float upper_bound = float(var.upperBoundAsDouble());
195 __variable.setLowerBound(lower_bound);
196 __variable.setUpperBound(upper_bound);
200 bool non_float_symbol_found =
false;
205 const float missing_val = std::stof(*iter);
206 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
209 __status_float_missing_symbols.insert(*iter,
false);
210 }
else if (!non_float_symbol_found) {
211 non_float_symbol_found =
true;
212 __nonfloat_missing_symbol = *iter;
217 __real_variable = var.clone();
224 template <
template <
typename >
class ALLOC >
226 const IContinuousVariable& var,
227 const bool fit_range,
232 __variable(var.name(), var.description()), __fit_range(fit_range) {
234 const float lower_bound = var.lowerBoundAsDouble();
235 const float upper_bound = var.upperBoundAsDouble();
236 __variable.setLowerBound(lower_bound);
237 __variable.setUpperBound(upper_bound);
240 __real_variable = var.clone();
247 template <
template <
typename >
class ALLOC >
249 const DBTranslator4ContinuousVariable< ALLOC >& from,
253 __variable(from.__variable),
254 __status_float_missing_symbols(from.__status_float_missing_symbols),
255 __nonfloat_missing_symbol(from.__nonfloat_missing_symbol),
256 __fit_range(from.__fit_range) {
258 __real_variable = from.__real_variable->clone();
265 template <
template <
typename >
class ALLOC >
267 const DBTranslator4ContinuousVariable< ALLOC >& from) :
272 template <
template <
typename >
class ALLOC >
274 DBTranslator4ContinuousVariable< ALLOC >&& from,
278 __variable(
std::move(from.__variable)),
279 __status_float_missing_symbols(
280 std::move(from.__status_float_missing_symbols)),
281 __nonfloat_missing_symbol(
std::move(from.__nonfloat_missing_symbol)),
282 __fit_range(from.__fit_range) {
284 __real_variable = from.__real_variable;
285 from.__real_variable =
nullptr;
292 template <
template <
typename >
class ALLOC >
294 DBTranslator4ContinuousVariable< ALLOC >&& from) :
300 template <
template <
typename >
class ALLOC >
301 DBTranslator4ContinuousVariable< ALLOC >*
305 ALLOC< DBTranslator4ContinuousVariable< ALLOC > > allocator(alloc);
306 DBTranslator4ContinuousVariable< ALLOC >* translator = allocator.allocate(1);
308 allocator.construct(translator, *
this, alloc);
310 allocator.deallocate(translator, 1);
318 template <
template <
typename >
class ALLOC >
319 INLINE DBTranslator4ContinuousVariable< ALLOC >*
326 template <
template <
typename >
class ALLOC >
329 if (__real_variable !=
nullptr)
delete __real_variable;
336 template <
template <
typename >
class ALLOC >
337 DBTranslator4ContinuousVariable< ALLOC >&
339 operator=(
const DBTranslator4ContinuousVariable< ALLOC >& from) {
342 __variable = from.__variable;
343 __status_float_missing_symbols = from.__status_float_missing_symbols;
344 __nonfloat_missing_symbol = from.__nonfloat_missing_symbol;
345 __fit_range = from.__fit_range;
347 if (__real_variable !=
nullptr)
delete __real_variable;
348 __real_variable = from.__real_variable->clone();
356 template <
template <
typename >
class ALLOC >
357 DBTranslator4ContinuousVariable< ALLOC >&
359 operator=(DBTranslator4ContinuousVariable< ALLOC >&& from) {
362 __variable = std::move(from.__variable);
363 __status_float_missing_symbols =
364 std::move(from.__status_float_missing_symbols);
365 __nonfloat_missing_symbol = std::move(from.__nonfloat_missing_symbol);
366 __fit_range = from.__fit_range;
368 if (__real_variable !=
nullptr)
delete __real_variable;
369 __real_variable = from.__real_variable;
370 from.__real_variable =
nullptr;
378 template <
template <
typename >
class ALLOC >
380 const std::string& str) {
384 return DBTranslatedValue{std::numeric_limits< float >::max()};
389 <<
"\" cannot be translated because it is not a number");
393 const float number = std::stof(str);
396 if (__variable.belongs(number))
return DBTranslatedValue{number};
400 if (!__status_float_missing_symbols[str]) {
401 __status_float_missing_symbols[str] =
true;
403 return DBTranslatedValue{std::numeric_limits< float >::max()};
410 <<
"\" cannot be translated because it is " 411 "out of the domain of the continuous variable");
421 if (__variable.lowerBound() == std::numeric_limits< float >::infinity()) {
422 __variable.setLowerBound(number);
423 __variable.setUpperBound(number);
424 return DBTranslatedValue{number};
431 const float lower_bound = __variable.lowerBound();
432 const float upper_bound = __variable.upperBound();
433 if (number < lower_bound) {
436 for (
const auto& missing : __status_float_missing_symbols) {
437 if (missing.second) {
438 const float miss_val = std::stof(missing.first);
439 if ((miss_val >= number) && (miss_val <= upper_bound)) {
442 << str <<
"\" cannot be translated because " 443 <<
"it would induce a new domain containing an already " 444 <<
"translated missing symbol");
451 for (
auto iter = __status_float_missing_symbols.beginSafe();
452 iter != __status_float_missing_symbols.endSafe();
454 if (iter.val() ==
false) {
455 const float miss_val = std::stof(iter.key());
456 if ((miss_val >= number) && (miss_val <= upper_bound)) {
458 __status_float_missing_symbols.erase(iter);
464 __variable.setLowerBound(number);
466 return DBTranslatedValue{number};
470 for (
const auto& missing : __status_float_missing_symbols) {
471 if (missing.second) {
472 const float miss_val = std::stof(missing.first);
473 if ((miss_val >= lower_bound) && (miss_val <= number)) {
476 << str <<
"\" cannot be translated because " 477 <<
"it would induce a new domain containing an already " 478 <<
"translated missing symbol");
485 for (
auto iter = __status_float_missing_symbols.beginSafe();
486 iter != __status_float_missing_symbols.endSafe();
488 if (iter.val() ==
false) {
489 const float miss_val = std::stof(iter.key());
490 if ((miss_val >= lower_bound) && (miss_val <= number)) {
492 __status_float_missing_symbols.erase(iter);
498 __variable.setUpperBound(number);
500 return DBTranslatedValue{number};
506 template <
template <
typename >
class ALLOC >
508 const DBTranslatedValue translated_val)
const {
509 if (translated_val.cont_val == std::numeric_limits< float >::max()) {
510 if (!__nonfloat_missing_symbol.empty())
return __nonfloat_missing_symbol;
515 if ((translated_val.cont_val < __variable.lowerBound())
516 || (translated_val.cont_val > __variable.upperBound())) {
518 "The back translation of " 519 << translated_val.cont_val
520 <<
" could not be found because the value is outside the " 521 <<
"domain of the continuous variable");
525 sprintf(buffer,
"%g", translated_val.cont_val);
526 return std::string(buffer);
531 template <
template <
typename >
class ALLOC >
538 template <
template <
typename >
class ALLOC >
539 INLINE HashTable< std::size_t,
541 ALLOC< std::pair< std::size_t, std::size_t > > >
543 return HashTable< std::size_t,
545 ALLOC< std::pair< std::size_t, std::size_t > > >();
550 template <
template <
typename >
class ALLOC >
553 return std::numeric_limits< std::size_t >::max();
558 template <
template <
typename >
class ALLOC >
559 INLINE
const IContinuousVariable*
562 __real_variable->setUpperBoundFromDouble(__variable.upperBound());
563 return __real_variable;
568 template <
template <
typename >
class ALLOC >
569 INLINE DBTranslatedValue
571 return DBTranslatedValue{std::numeric_limits< float >::max()};
virtual DBTranslatedValue translate(const std::string &str) final
returns the translation of a string
virtual void setLowerBoundFromDouble(const double new_bound)=0
updates the lower bound of the domain of the variable
virtual ~DBTranslator4ContinuousVariable()
destructor
bool empty() const noexcept
Indicates whether the set is the empty set.
typename DBTranslator< ALLOC >::allocator_type allocator_type
type for the allocators passed in arguments of methods
virtual DBTranslator4ContinuousVariable< ALLOC > * clone() const
virtual copy constructor
The databases' cell translators for continuous variables.
void erase(const Key &k)
Erases an element from the set.
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
iterator begin() const
The usual unsafe begin iterator to parse the set.
DBTranslator4ContinuousVariable< ALLOC > & operator=(const DBTranslator4ContinuousVariable< ALLOC > &from)
copy operator
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
virtual std::size_t domainSize() const final
returns std::numeric_limits<std::size_t>::max ()
DBTranslator4ContinuousVariable(const std::vector< std::string, XALLOC< std::string > > &missing_symbols, const bool fit_range=false, const allocator_type &alloc=allocator_type())
default constructor without any initial variable
virtual bool needsReordering() const final
indicates that the translations should never be reordered
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).
static bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
Set< std::string, ALLOC< std::string > > _missing_symbols
the set of missing symbols
DBTranslator< ALLOC > & operator=(const DBTranslator< ALLOC > &from)
copy operator
virtual HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > reorder() final
returns an empty mapping, indicating that old tanslations are equal to the newly reordered ones...
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
bool isMissingSymbol(const std::string &str) const
indicates whether a string corresponds to a missing symbol
#define GUM_ERROR(type, msg)
virtual const IContinuousVariable * variable() const final
returns the variable stored into the translator
virtual DBTranslatedValue missingValue() const final
returns the translation of a missing value