33 #ifndef DOXYGEN_SHOULD_SKIP_THIS 41 template <
template <
typename >
class ALLOC >
42 template <
typename GUM_SCALAR,
template <
typename >
class XALLOC >
44 const DiscretizedVariable< GUM_SCALAR >& var,
45 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
46 std::size_t max_dico_entries,
54 __variable(var.name(), var.description()) {
56 if (var.domainSize() > max_dico_entries) {
58 "the dictionary induced by the variable is too large");
62 const auto& ticks = var.ticks();
63 for (
const auto tick : ticks) {
64 __variable.addTick((
float)tick);
68 const float lower_bound = (float)ticks[0];
69 const float upper_bound = (float)ticks.back();
77 const float missing_val = std::stof(*iter);
78 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
86 for (
const auto& label : var.labels()) {
101 __real_variable = var.clone();
108 template <
template <
typename >
class ALLOC >
109 template <
template <
typename >
class XALLOC >
111 const IDiscretizedVariable& var,
112 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
113 std::size_t max_dico_entries,
121 __variable(var.name(), var.description()) {
123 if (var.domainSize() > max_dico_entries) {
125 "the dictionary induced by the variable is too large");
129 const auto ticks = var.ticksAsDoubles();
130 for (
const auto tick : ticks) {
131 __variable.addTick((
float)tick);
135 const float lower_bound = float(ticks[0]);
136 const float upper_bound = float(ticks.back());
144 const float missing_val = std::stof(*iter);
145 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
152 std::size_t size = 0;
153 for (
const auto& label : var.labels()) {
168 __real_variable = var.clone();
175 template <
template <
typename >
class ALLOC >
176 template <
typename GUM_SCALAR >
178 const DiscretizedVariable< GUM_SCALAR >& var,
179 std::size_t max_dico_entries,
184 __variable(var.name(), var.description()) {
186 if (var.domainSize() > max_dico_entries) {
188 "the dictionary induced by the variable is too large");
192 const auto& ticks = var.ticks();
193 for (
const auto tick : ticks) {
194 __variable.addTick((
float)tick);
198 std::size_t size = 0;
199 for (
const auto& label : var.labels()) {
205 __real_variable = var.clone();
212 template <
template <
typename >
class ALLOC >
214 const IDiscretizedVariable& var,
215 std::size_t max_dico_entries,
220 __variable(var.name(), var.description()) {
222 if (var.domainSize() > max_dico_entries) {
224 "the dictionary induced by the variable is too large");
228 const auto ticks = var.ticksAsDoubles();
229 for (
const auto tick : ticks) {
230 __variable.addTick((
float)tick);
234 std::size_t size = 0;
235 for (
const auto& label : var.labels()) {
241 __real_variable = var.clone();
248 template <
template <
typename >
class ALLOC >
250 const DBTranslator4DiscretizedVariable< ALLOC >& from,
254 __variable(from.__variable) {
256 __real_variable = from.__real_variable->clone();
263 template <
template <
typename >
class ALLOC >
265 const DBTranslator4DiscretizedVariable< ALLOC >& from) :
270 template <
template <
typename >
class ALLOC >
272 DBTranslator4DiscretizedVariable< ALLOC >&& from,
276 __variable(
std::move(from.__variable)) {
278 __real_variable = from.__real_variable;
279 from.__real_variable =
nullptr;
286 template <
template <
typename >
class ALLOC >
288 DBTranslator4DiscretizedVariable< ALLOC >&& from) :
294 template <
template <
typename >
class ALLOC >
295 DBTranslator4DiscretizedVariable< ALLOC >*
299 ALLOC< DBTranslator4DiscretizedVariable< ALLOC > > allocator(alloc);
300 DBTranslator4DiscretizedVariable< ALLOC >* translator =
301 allocator.allocate(1);
303 allocator.construct(translator, *
this, alloc);
305 allocator.deallocate(translator, 1);
313 template <
template <
typename >
class ALLOC >
314 INLINE DBTranslator4DiscretizedVariable< ALLOC >*
321 template <
template <
typename >
class ALLOC >
324 if (__real_variable !=
nullptr)
delete __real_variable;
331 template <
template <
typename >
class ALLOC >
332 DBTranslator4DiscretizedVariable< ALLOC >&
334 operator=(
const DBTranslator4DiscretizedVariable< ALLOC >& from) {
337 __variable = from.__variable;
339 if (__real_variable !=
nullptr)
delete __real_variable;
340 __real_variable = from.__real_variable->clone();
348 template <
template <
typename >
class ALLOC >
349 DBTranslator4DiscretizedVariable< ALLOC >&
351 operator=(DBTranslator4DiscretizedVariable< ALLOC >&& from) {
354 __variable = std::move(from.__variable);
356 if (__real_variable !=
nullptr)
delete __real_variable;
357 __real_variable = from.__real_variable;
358 from.__real_variable =
nullptr;
366 template <
template <
typename >
class ALLOC >
368 const std::string& str) {
371 return DBTranslatedValue{std::size_t(__variable[str])};
375 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
387 <<
"\" cannot be translated because it is not a number");
390 "The translation of \"" << str <<
"\" could not be found");
398 template <
template <
typename >
class ALLOC >
400 const DBTranslatedValue translated_val)
const {
403 }
catch (Exception&) {
405 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
406 && !this->_missing_symbols.empty())
410 "The back translation of \"" << translated_val.discr_val
411 <<
"\" could not be found");
417 template <
template <
typename >
class ALLOC >
425 template <
template <
typename >
class ALLOC >
432 template <
template <
typename >
class ALLOC >
439 template <
template <
typename >
class ALLOC >
440 INLINE HashTable< std::size_t,
442 ALLOC< std::pair< std::size_t, std::size_t > > >
444 return HashTable< std::size_t,
446 ALLOC< std::pair< std::size_t, std::size_t > > >();
451 template <
template <
typename >
class ALLOC >
454 return __variable.domainSize();
459 template <
template <
typename >
class ALLOC >
460 INLINE
const IDiscretizedVariable*
462 return __real_variable;
467 template <
template <
typename >
class ALLOC >
468 INLINE DBTranslatedValue
470 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.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
DBTranslator4DiscretizedVariable< ALLOC > & operator=(const DBTranslator4DiscretizedVariable< ALLOC > &from)
copy operator
virtual DBTranslator4DiscretizedVariable< ALLOC > * clone() const
virtual copy constructor
virtual DBTranslatedValue translate(const std::string &str) final
returns the translation of a string
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
virtual ~DBTranslator4DiscretizedVariable()
destructor
The databases' cell translators for discretized variables.
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
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.
virtual const IDiscretizedVariable * variable() const final
returns the variable stored into the translator
virtual DBTranslatedValue missingValue() const final
returns the translation of a missing value
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
virtual bool hasEditableDictionary() const final
indicates that the translator is never in editable dictionary mode
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
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
DBTranslator4DiscretizedVariable(const DiscretizedVariable< GUM_SCALAR > &var, 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 with a discretized variable as translator
Base class for all aGrUM's exceptions.
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
virtual std::size_t domainSize() const final
returns the number of discretization intervals used for translations
virtual HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > reorder() final
returns an empty HashTable to indicate that no reordering is needed.
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
#define GUM_ERROR(type, msg)
virtual void setEditableDictionaryMode(bool new_mode) final
sets/unset the editable dictionary mode