32 #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,
44 std::size_t max_dico_entries,
52 __variable(
"var",
"", 0) {
58 template <
template <
typename >
class ALLOC >
60 std::size_t max_dico_entries,
65 __variable(
"var",
"", 0) {
71 template <
template <
typename >
class ALLOC >
72 template <
template <
typename >
class XALLOC >
74 const LabelizedVariable& var,
75 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
76 const bool editable_dictionary,
77 std::size_t max_dico_entries,
87 if (var.domainSize() > max_dico_entries) {
89 "the dictionary induced by the variable is too large");
94 for (
const auto& label : var.labels()) {
111 template <
template <
typename >
class ALLOC >
113 const LabelizedVariable& var,
114 const bool editable_dictionary,
115 std::size_t max_dico_entries,
124 if (var.domainSize() > max_dico_entries) {
126 "the dictionary induced by the variable is too large");
130 std::size_t size = 0;
131 for (
const auto& label : var.labels()) {
142 template <
template <
typename >
class ALLOC >
144 const DBTranslator4LabelizedVariable< ALLOC >& from,
148 __variable(from.__variable) {
154 template <
template <
typename >
class ALLOC >
156 const DBTranslator4LabelizedVariable< ALLOC >& from) :
161 template <
template <
typename >
class ALLOC >
163 DBTranslator4LabelizedVariable< ALLOC >&& from,
167 __variable(
std::move(from.__variable)) {
173 template <
template <
typename >
class ALLOC >
175 DBTranslator4LabelizedVariable< ALLOC >&& from) :
181 template <
template <
typename >
class ALLOC >
182 DBTranslator4LabelizedVariable< ALLOC >*
186 ALLOC< DBTranslator4LabelizedVariable< ALLOC > > allocator(alloc);
187 DBTranslator4LabelizedVariable< ALLOC >* translator = allocator.allocate(1);
189 allocator.construct(translator, *
this, alloc);
191 allocator.deallocate(translator, 1);
199 template <
template <
typename >
class ALLOC >
200 INLINE DBTranslator4LabelizedVariable< ALLOC >*
207 template <
template <
typename >
class ALLOC >
215 template <
template <
typename >
class ALLOC >
216 DBTranslator4LabelizedVariable< ALLOC >&
218 operator=(
const DBTranslator4LabelizedVariable< ALLOC >& from) {
221 __variable = from.__variable;
229 template <
template <
typename >
class ALLOC >
230 DBTranslator4LabelizedVariable< ALLOC >&
232 operator=(DBTranslator4LabelizedVariable< ALLOC >&& from) {
235 __variable = std::move(from.__variable);
243 template <
template <
typename >
class ALLOC >
251 return DBTranslatedValue{std::size_t(__variable[str])};
255 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
260 const std::size_t size = __variable.domainSize();
263 "String \"" << str <<
"\" cannot be translated " 264 <<
"because the dictionary is already full");
265 __variable.addLabel(str);
267 return DBTranslatedValue{size};
270 "The translation of \"" << str <<
"\" could not be found");
276 template <
template <
typename >
class ALLOC >
278 const DBTranslatedValue translated_val)
const {
281 }
catch (Exception&) {
283 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
284 && !this->_missing_symbols.empty())
288 "The back translation of \"" << translated_val.discr_val
289 <<
"\" could not be found");
295 template <
template <
typename >
class ALLOC >
298 const auto& labels = __variable.labels();
299 float last_number = std::numeric_limits< float >::lowest();
301 bool only_numbers =
true;
302 for (
const auto& label : labels) {
304 only_numbers =
false;
307 number = std::stof(label);
308 if (number < last_number)
return true;
309 last_number = number;
315 const std::size_t size = labels.size();
316 for (std::size_t i = 1; i < size; ++i) {
317 if (labels[i] < labels[i - 1])
return true;
326 template <
template <
typename >
class ALLOC >
327 HashTable< std::size_t,
329 ALLOC< std::pair< std::size_t, std::size_t > > >
333 const auto& labels = __variable.labels();
334 const std::size_t size = labels.size();
336 bool only_numbers =
true;
337 for (
const auto& label : labels) {
339 only_numbers =
false;
345 std::vector< std::pair< std::size_t, std::string >,
346 ALLOC< std::pair< std::size_t, std::string > > >
348 xlabels.reserve(size);
349 for (std::size_t i = std::size_t(0); i < size; ++i)
350 xlabels.push_back(std::make_pair(i, labels[i]));
354 std::sort(xlabels.begin(),
356 [](
const std::pair< std::size_t, std::string >& a,
357 const std::pair< std::size_t, std::string >& b) ->
bool {
358 return std::stof(a.second) < std::stof(b.second);
361 std::sort(xlabels.begin(),
363 [](
const std::pair< std::size_t, std::string >& a,
364 const std::pair< std::size_t, std::string >& b) ->
bool {
365 return a.second < b.second;
369 bool modifications =
false;
370 for (std::size_t i = std::size_t(0); i < size; ++i) {
371 if (xlabels[i].first != i) {
372 modifications =
true;
378 if (!modifications) {
379 return HashTable< std::size_t,
381 ALLOC< std::pair< std::size_t, std::size_t > > >();
386 __variable.eraseLabels();
387 for (
auto& label : xlabels)
388 __variable.addLabel(label.second);
393 HashTable< std::size_t,
395 ALLOC< std::pair< std::size_t, std::size_t > > >
397 for (std::size_t i = std::size_t(0); i < size; ++i) {
398 mapping.insert(xlabels[i].first, i);
407 template <
template <
typename >
class ALLOC >
410 return __variable.domainSize();
415 template <
template <
typename >
class ALLOC >
416 INLINE
const LabelizedVariable*
423 template <
template <
typename >
class ALLOC >
424 INLINE DBTranslatedValue
426 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
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.
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
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 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.
The databases' cell translators for labelized variables.
typename DBTranslator< ALLOC >::allocator_type allocator_type
type for the allocators passed in arguments of methods
#define GUM_ERROR(type, msg)