36 #ifndef DOXYGEN_SHOULD_SKIP_THIS 44 template <
template <
typename >
class ALLOC >
45 template <
typename GUM_SCALAR,
template <
typename >
class XALLOC >
47 const DiscretizedVariable< GUM_SCALAR >& var,
48 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
49 std::size_t max_dico_entries,
57 __variable(var.name(), var.description()) {
59 if (var.domainSize() > max_dico_entries) {
61 "the dictionary induced by the variable is too large");
65 const auto& ticks = var.ticks();
66 for (
const auto tick : ticks) {
67 __variable.addTick((
float)tick);
71 const float lower_bound = (float)ticks[0];
72 const float upper_bound = (float)ticks.back();
80 const float missing_val = std::stof(*iter);
81 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
89 for (
const auto& label : var.labels()) {
104 __real_variable = var.clone();
111 template <
template <
typename >
class ALLOC >
112 template <
template <
typename >
class XALLOC >
114 const IDiscretizedVariable& var,
115 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
116 std::size_t max_dico_entries,
124 __variable(var.name(), var.description()) {
126 if (var.domainSize() > max_dico_entries) {
128 "the dictionary induced by the variable is too large");
132 const auto ticks = var.ticksAsDoubles();
133 for (
const auto tick : ticks) {
134 __variable.addTick((
float)tick);
138 const float lower_bound = float(ticks[0]);
139 const float upper_bound = float(ticks.back());
147 const float missing_val = std::stof(*iter);
148 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
155 std::size_t size = 0;
156 for (
const auto& label : var.labels()) {
171 __real_variable = var.clone();
178 template <
template <
typename >
class ALLOC >
179 template <
typename GUM_SCALAR >
181 const DiscretizedVariable< GUM_SCALAR >& var,
182 std::size_t max_dico_entries,
187 __variable(var.name(), var.description()) {
189 if (var.domainSize() > max_dico_entries) {
191 "the dictionary induced by the variable is too large");
195 const auto& ticks = var.ticks();
196 for (
const auto tick : ticks) {
197 __variable.addTick((
float)tick);
201 std::size_t size = 0;
202 for (
const auto& label : var.labels()) {
208 __real_variable = var.clone();
215 template <
template <
typename >
class ALLOC >
217 const IDiscretizedVariable& var,
218 std::size_t max_dico_entries,
223 __variable(var.name(), var.description()) {
225 if (var.domainSize() > max_dico_entries) {
227 "the dictionary induced by the variable is too large");
231 const auto ticks = var.ticksAsDoubles();
232 for (
const auto tick : ticks) {
233 __variable.addTick((
float)tick);
237 std::size_t size = 0;
238 for (
const auto& label : var.labels()) {
244 __real_variable = var.clone();
251 template <
template <
typename >
class ALLOC >
253 const DBTranslator4DiscretizedVariable< ALLOC >& from,
257 __variable(from.__variable) {
259 __real_variable = from.__real_variable->clone();
266 template <
template <
typename >
class ALLOC >
268 const DBTranslator4DiscretizedVariable< ALLOC >& from) :
273 template <
template <
typename >
class ALLOC >
275 DBTranslator4DiscretizedVariable< ALLOC >&& from,
279 __variable(
std::move(from.__variable)) {
281 __real_variable = from.__real_variable;
282 from.__real_variable =
nullptr;
289 template <
template <
typename >
class ALLOC >
291 DBTranslator4DiscretizedVariable< ALLOC >&& from) :
297 template <
template <
typename >
class ALLOC >
298 DBTranslator4DiscretizedVariable< ALLOC >*
302 ALLOC< DBTranslator4DiscretizedVariable< ALLOC > > allocator(alloc);
303 DBTranslator4DiscretizedVariable< ALLOC >* translator =
304 allocator.allocate(1);
306 allocator.construct(translator, *
this, alloc);
308 allocator.deallocate(translator, 1);
316 template <
template <
typename >
class ALLOC >
317 INLINE DBTranslator4DiscretizedVariable< ALLOC >*
324 template <
template <
typename >
class ALLOC >
327 if (__real_variable !=
nullptr)
delete __real_variable;
334 template <
template <
typename >
class ALLOC >
335 DBTranslator4DiscretizedVariable< ALLOC >&
337 operator=(
const DBTranslator4DiscretizedVariable< ALLOC >& from) {
340 __variable = from.__variable;
342 if (__real_variable !=
nullptr)
delete __real_variable;
343 __real_variable = from.__real_variable->clone();
351 template <
template <
typename >
class ALLOC >
352 DBTranslator4DiscretizedVariable< ALLOC >&
354 operator=(DBTranslator4DiscretizedVariable< ALLOC >&& from) {
357 __variable = std::move(from.__variable);
359 if (__real_variable !=
nullptr)
delete __real_variable;
360 __real_variable = from.__real_variable;
361 from.__real_variable =
nullptr;
369 template <
template <
typename >
class ALLOC >
371 const std::string& str) {
374 return DBTranslatedValue{std::size_t(__variable[str])};
378 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
390 <<
"\" cannot be translated because it is not a number");
393 "The translation of \"" << str <<
"\" could not be found");
401 template <
template <
typename >
class ALLOC >
403 const DBTranslatedValue translated_val)
const {
406 }
catch (Exception&) {
408 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
409 && !this->_missing_symbols.empty())
413 "The back translation of \"" << translated_val.discr_val
414 <<
"\" could not be found");
420 template <
template <
typename >
class ALLOC >
428 template <
template <
typename >
class ALLOC >
435 template <
template <
typename >
class ALLOC >
442 template <
template <
typename >
class ALLOC >
443 INLINE HashTable< std::size_t,
445 ALLOC< std::pair< std::size_t, std::size_t > > >
447 return HashTable< std::size_t,
449 ALLOC< std::pair< std::size_t, std::size_t > > >();
454 template <
template <
typename >
class ALLOC >
457 return __variable.domainSize();
462 template <
template <
typename >
class ALLOC >
463 INLINE
const IDiscretizedVariable*
465 return __real_variable;
470 template <
template <
typename >
class ALLOC >
471 INLINE DBTranslatedValue
473 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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
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
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
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).
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