aGrUM  0.16.0
DBTranslator_tpl.h
Go to the documentation of this file.
1 
29 
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 namespace gum {
33 
34  namespace learning {
35 
36 
38  template < template < typename > class ALLOC >
41  return *this;
42  }
43 
44 
46  template < template < typename > class ALLOC >
48  return _val_type;
49  }
50 
51 
53  template < template < typename > class ALLOC >
54  template < template < typename > class XALLOC >
56  DBTranslatedValueType val_type,
57  const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
58  const bool dynamic_dictionary,
59  std::size_t max_dico_entries,
60  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
61  DBTranslator< ALLOC >::allocator_type(alloc),
62  _is_dictionary_dynamic(dynamic_dictionary),
63  _max_dico_entries(max_dico_entries), _val_type(val_type) {
64  const std::size_t size = missing_symbols.size();
65 
66  if (size) {
67  // save the set of symbols representing the missing values
68  _missing_symbols.resize((Size)missing_symbols.size());
69  for (const auto& symbol : missing_symbols) {
70  _missing_symbols.insert(symbol);
71  }
72  }
73 
74  GUM_CONSTRUCTOR(DBTranslator);
75  }
76 
77 
79  template < template < typename > class ALLOC >
81  DBTranslatedValueType val_type,
82  const bool dynamic_dictionary,
83  std::size_t max_dico_entries,
84  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
85  DBTranslator< ALLOC >::allocator_type(alloc),
86  _is_dictionary_dynamic(dynamic_dictionary),
87  _max_dico_entries(max_dico_entries), _val_type(val_type) {
88  GUM_CONSTRUCTOR(DBTranslator);
89  }
90 
91 
93  template < template < typename > class ALLOC >
95  const DBTranslator< ALLOC >& from,
96  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
97  DBTranslator< ALLOC >::allocator_type(alloc),
101  _val_type(from._val_type) {
102  GUM_CONS_CPY(DBTranslator);
103  }
104 
105 
107  template < template < typename > class ALLOC >
108  INLINE DBTranslator< ALLOC >::DBTranslator(const DBTranslator< ALLOC >& from) :
109  DBTranslator< ALLOC >(from, from.getAllocator()) {}
110 
111 
113  template < template < typename > class ALLOC >
115  DBTranslator< ALLOC >&& from,
116  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
117  DBTranslator< ALLOC >::allocator_type(alloc),
121  _back_dico(std::move(from._back_dico)), _val_type(from._val_type) {
122  GUM_CONS_MOV(DBTranslator);
123  }
124 
125 
127  template < template < typename > class ALLOC >
128  INLINE DBTranslator< ALLOC >::DBTranslator(DBTranslator< ALLOC >&& from) :
129  DBTranslator< ALLOC >(from, from.getAllocator()) {}
130 
131 
133  template < template < typename > class ALLOC >
135  GUM_DESTRUCTOR(DBTranslator);
136  }
137 
138 
140  template < template < typename > class ALLOC >
141  INLINE DBTranslator< ALLOC >& DBTranslator< ALLOC >::
142  operator=(const DBTranslator< ALLOC >& from) {
143  if (this != &from) {
144  _is_dictionary_dynamic = from._is_dictionary_dynamic;
145  _max_dico_entries = from._max_dico_entries;
146  _missing_symbols = from._missing_symbols;
147  _back_dico = from._back_dico;
148  _val_type = from._val_type;
149  }
150  return *this;
151  }
152 
153 
155  template < template < typename > class ALLOC >
156  INLINE DBTranslator< ALLOC >& DBTranslator< ALLOC >::
157  operator=(DBTranslator< ALLOC >&& from) {
158  _is_dictionary_dynamic = from._is_dictionary_dynamic;
159  _max_dico_entries = from._max_dico_entries;
160  _missing_symbols = std::move(from._missing_symbols);
161  _back_dico = std::move(from._back_dico);
162  _val_type = from._val_type;
163 
164  return *this;
165  }
166 
167 
169  template < template < typename > class ALLOC >
170  INLINE DBTranslatedValue DBTranslator< ALLOC >::
171  operator<<(const std::string& str) {
172  return translate(str);
173  }
174 
175 
177  template < template < typename > class ALLOC >
178  INLINE std::string DBTranslator< ALLOC >::
179  operator>>(const DBTranslatedValue translated_val) {
180  return translateBack(translated_val);
181  }
182 
183 
185  template < template < typename > class ALLOC >
187  return _is_dictionary_dynamic;
188  }
189 
190 
192  template < template < typename > class ALLOC >
193  INLINE void DBTranslator< ALLOC >::setEditableDictionaryMode(bool new_mode) {
194  _is_dictionary_dynamic = new_mode;
195  }
196 
197 
199  template < template < typename > class ALLOC >
200  INLINE const Set< std::string, ALLOC< std::string > >&
202  return _missing_symbols;
203  }
204 
205 
207  template < template < typename > class ALLOC >
208  INLINE bool
209  DBTranslator< ALLOC >::isMissingSymbol(const std::string& str) const {
210  return _missing_symbols.exists(str);
211  }
212 
213 
215  template < template < typename > class ALLOC >
216  INLINE void
217  DBTranslator< ALLOC >::setVariableName(const std::string& str) const {
218  const_cast< Variable* >(this->variable())->setName(str);
219  }
220 
221 
223  template < template < typename > class ALLOC >
225  const std::string& str) const {
226  const_cast< Variable* >(this->variable())->setDescription(str);
227  }
228 
229 
231  template < template < typename > class ALLOC >
233  const DBTranslatedValue& value) const {
234  switch (_val_type) {
236  return value.discr_val == std::numeric_limits< std::size_t >::max();
237 
239  return value.cont_val == std::numeric_limits< float >::max();
240 
241  default:
242  GUM_ERROR(NotImplementedYet,
243  "No missing value interpretation for this "
244  "translated value type");
245  }
246  }
247 
248 
249  } /* namespace learning */
250 
251 } /* namespace gum */
252 
253 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
Bijection< std::size_t, std::string, ALLOC< std::pair< float, std::string > > > _back_dico
the bijection relating back translated values and their original strings.
Definition: DBTranslator.h:396
DBTranslatedValue operator<<(const std::string &str)
alias for method translate
ALLOC< DBTranslatedValue > allocator_type
type for the allocators passed in arguments of methods
Definition: DBTranslator.h:120
virtual const RangeVariable * variable() const final
returns the variable stored into the translator
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
std::size_t _max_dico_entries
the maximum number of entries that the dictionary is allowed to contain
Definition: DBTranslator.h:382
void setVariableName(const std::string &str) const
sets the name of the variable stored into the translator
STL namespace.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
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
virtual ~DBTranslator()
destructor
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
const Set< std::string, ALLOC< std::string > > & missingSymbols() const
returns the set of missing symbols taken into account by the translator
allocator_type getAllocator() const
returns the allocator used by the translator
bool isMissingValue(const DBTranslatedValue &val) const
indicates whether a translated value corresponds to a missing value
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
void resize(Size new_capacity)
Changes the size of the underlying hash table containing the set.
Definition: set_tpl.h:552
Set< std::string, ALLOC< std::string > > _missing_symbols
the set of missing symbols
Definition: DBTranslator.h:385
DBTranslator< ALLOC > & operator=(const DBTranslator< ALLOC > &from)
copy operator
virtual DBTranslatedValue translate(const std::string &str) final
returns the translation of a string
virtual void setEditableDictionaryMode(bool new_mode)
sets/unset the editable dictionary mode
DBTranslatedValueType _val_type
the type of the values translated by the translator
Definition: DBTranslator.h:399
bool _is_dictionary_dynamic
indicates whether the dictionary can be updated or not
Definition: DBTranslator.h:379
std::string operator>>(const DBTranslatedValue translated_val)
alias for method translateBack
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
typename DBTranslator< ALLOC >::allocator_type allocator_type
type for the allocators passed in arguments of methods
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void setVariableDescription(const std::string &str) const
sets the name of the variable stored into the translator
DBTranslatedValueType getValType() const
returns the type of values handled by the translator
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55