35 #ifndef DOXYGEN_SHOULD_SKIP_THIS 43 template <
template <
typename >
class ALLOC >
44 template <
template <
typename >
class XALLOC >
46 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
47 std::size_t max_dico_entries,
55 __variable(
"var",
"", 0) {
61 template <
template <
typename >
class ALLOC >
63 std::size_t max_dico_entries,
68 __variable(
"var",
"", 0) {
74 template <
template <
typename >
class ALLOC >
75 template <
template <
typename >
class XALLOC >
77 const LabelizedVariable& var,
78 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
79 const bool editable_dictionary,
80 std::size_t max_dico_entries,
90 if (var.domainSize() > max_dico_entries) {
92 "the dictionary induced by the variable is too large");
97 for (
const auto& label : var.labels()) {
114 template <
template <
typename >
class ALLOC >
116 const LabelizedVariable& var,
117 const bool editable_dictionary,
118 std::size_t max_dico_entries,
127 if (var.domainSize() > max_dico_entries) {
129 "the dictionary induced by the variable is too large");
133 std::size_t size = 0;
134 for (
const auto& label : var.labels()) {
145 template <
template <
typename >
class ALLOC >
147 const DBTranslator4LabelizedVariable< ALLOC >& from,
151 __variable(from.__variable) {
157 template <
template <
typename >
class ALLOC >
159 const DBTranslator4LabelizedVariable< ALLOC >& from) :
164 template <
template <
typename >
class ALLOC >
166 DBTranslator4LabelizedVariable< ALLOC >&& from,
170 __variable(
std::move(from.__variable)) {
176 template <
template <
typename >
class ALLOC >
178 DBTranslator4LabelizedVariable< ALLOC >&& from) :
184 template <
template <
typename >
class ALLOC >
185 DBTranslator4LabelizedVariable< ALLOC >*
189 ALLOC< DBTranslator4LabelizedVariable< ALLOC > > allocator(alloc);
190 DBTranslator4LabelizedVariable< ALLOC >* translator = allocator.allocate(1);
192 allocator.construct(translator, *
this, alloc);
194 allocator.deallocate(translator, 1);
202 template <
template <
typename >
class ALLOC >
203 INLINE DBTranslator4LabelizedVariable< ALLOC >*
210 template <
template <
typename >
class ALLOC >
218 template <
template <
typename >
class ALLOC >
219 DBTranslator4LabelizedVariable< ALLOC >&
221 operator=(
const DBTranslator4LabelizedVariable< ALLOC >& from) {
224 __variable = from.__variable;
232 template <
template <
typename >
class ALLOC >
233 DBTranslator4LabelizedVariable< ALLOC >&
235 operator=(DBTranslator4LabelizedVariable< ALLOC >&& from) {
238 __variable = std::move(from.__variable);
246 template <
template <
typename >
class ALLOC >
254 return DBTranslatedValue{std::size_t(__variable[str])};
258 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
263 const std::size_t size = __variable.domainSize();
266 "String \"" << str <<
"\" cannot be translated " 267 <<
"because the dictionary is already full");
268 __variable.addLabel(str);
270 return DBTranslatedValue{size};
273 "The translation of \"" << str <<
"\" could not be found");
279 template <
template <
typename >
class ALLOC >
281 const DBTranslatedValue translated_val)
const {
284 }
catch (Exception&) {
286 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
287 && !this->_missing_symbols.empty())
291 "The back translation of \"" << translated_val.discr_val
292 <<
"\" could not be found");
298 template <
template <
typename >
class ALLOC >
301 const auto& labels = __variable.labels();
302 float last_number = std::numeric_limits< float >::lowest();
304 bool only_numbers =
true;
305 for (
const auto& label : labels) {
307 only_numbers =
false;
310 number = std::stof(label);
311 if (number < last_number)
return true;
312 last_number = number;
318 const std::size_t size = labels.size();
319 for (std::size_t i = 1; i < size; ++i) {
320 if (labels[i] < labels[i - 1])
return true;
329 template <
template <
typename >
class ALLOC >
330 HashTable< std::size_t,
332 ALLOC< std::pair< std::size_t, std::size_t > > >
336 const auto& labels = __variable.labels();
337 const std::size_t size = labels.size();
339 bool only_numbers =
true;
340 for (
const auto& label : labels) {
342 only_numbers =
false;
348 std::vector< std::pair< std::size_t, std::string >,
349 ALLOC< std::pair< std::size_t, std::string > > >
351 xlabels.reserve(size);
352 for (std::size_t i = std::size_t(0); i < size; ++i)
353 xlabels.push_back(std::make_pair(i, labels[i]));
357 std::sort(xlabels.begin(),
359 [](
const std::pair< std::size_t, std::string >& a,
360 const std::pair< std::size_t, std::string >& b) ->
bool {
361 return std::stof(a.second) < std::stof(b.second);
364 std::sort(xlabels.begin(),
366 [](
const std::pair< std::size_t, std::string >& a,
367 const std::pair< std::size_t, std::string >& b) ->
bool {
368 return a.second < b.second;
372 bool modifications =
false;
373 for (std::size_t i = std::size_t(0); i < size; ++i) {
374 if (xlabels[i].first != i) {
375 modifications =
true;
381 if (!modifications) {
382 return HashTable< std::size_t,
384 ALLOC< std::pair< std::size_t, std::size_t > > >();
389 __variable.eraseLabels();
390 for (
auto& label : xlabels)
391 __variable.addLabel(label.second);
396 HashTable< std::size_t,
398 ALLOC< std::pair< std::size_t, std::size_t > > >
400 for (std::size_t i = std::size_t(0); i < size; ++i) {
401 mapping.insert(xlabels[i].first, i);
410 template <
template <
typename >
class ALLOC >
413 return __variable.domainSize();
418 template <
template <
typename >
class ALLOC >
419 INLINE
const LabelizedVariable*
426 template <
template <
typename >
class ALLOC >
427 INLINE DBTranslatedValue
429 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.
virtual const LabelizedVariable * variable() const final
returns the variable stored into the translator
virtual DBTranslatedValue missingValue() const final
returns the translation of a missing value
void clear()
Removes all the associations from the gum::Bijection.
virtual ~DBTranslator4LabelizedVariable()
destructor
std::size_t _max_dico_entries
the maximum number of entries that the dictionary is allowed to contain
DBTranslator4LabelizedVariable(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
virtual std::size_t domainSize() const final
returns the domain size of a variable corresponding to the translations
void erase(const Key &k)
Erases an element from the set.
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
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.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
DBTranslator4LabelizedVariable< ALLOC > & operator=(const DBTranslator4LabelizedVariable< ALLOC > &from)
copy operator
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
virtual bool needsReordering() const final
indicates whether a reordering is needed to make the translations sorted
Base class for all aGrUM's exceptions.
virtual DBTranslatedValue translate(const std::string &str) final
returns the translation of a string
virtual DBTranslator4LabelizedVariable< ALLOC > * clone() const
virtual copy constructor
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
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...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
typename DBTranslator< ALLOC >::allocator_type allocator_type
type for the allocators passed in arguments of methods
#define GUM_ERROR(type, msg)