aGrUM  0.14.2
DBTranslator_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 namespace gum {
30 
31  namespace learning {
32 
33 
35  template < template < typename > class ALLOC >
38  return *this;
39  }
40 
41 
43  template < template < typename > class ALLOC >
45  return _val_type;
46  }
47 
48 
50  template < template < typename > class ALLOC >
51  template < template < typename > class XALLOC >
53  DBTranslatedValueType val_type,
54  const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
55  const bool dynamic_dictionary,
56  std::size_t max_dico_entries,
57  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
58  DBTranslator< ALLOC >::allocator_type(alloc),
59  _is_dictionary_dynamic(dynamic_dictionary),
60  _max_dico_entries(max_dico_entries), _val_type(val_type) {
61  const std::size_t size = missing_symbols.size();
62 
63  if (size) {
64  // save the set of symbols representing the missing values
65  _missing_symbols.resize((Size)missing_symbols.size());
66  for (const auto& symbol : missing_symbols) {
67  _missing_symbols.insert(symbol);
68  }
69  }
70 
71  GUM_CONSTRUCTOR(DBTranslator);
72  }
73 
74 
76  template < template < typename > class ALLOC >
78  DBTranslatedValueType val_type,
79  const bool dynamic_dictionary,
80  std::size_t max_dico_entries,
81  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
82  DBTranslator< ALLOC >::allocator_type(alloc),
83  _is_dictionary_dynamic(dynamic_dictionary),
84  _max_dico_entries(max_dico_entries), _val_type(val_type) {
85  GUM_CONSTRUCTOR(DBTranslator);
86  }
87 
88 
90  template < template < typename > class ALLOC >
92  const DBTranslator< ALLOC >& from,
93  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
94  DBTranslator< ALLOC >::allocator_type(alloc),
98  _val_type(from._val_type) {
99  GUM_CONS_CPY(DBTranslator);
100  }
101 
102 
104  template < template < typename > class ALLOC >
105  INLINE DBTranslator< ALLOC >::DBTranslator(const DBTranslator< ALLOC >& from) :
106  DBTranslator< ALLOC >(from, from.getAllocator()) {}
107 
108 
110  template < template < typename > class ALLOC >
112  DBTranslator< ALLOC >&& from,
113  const typename DBTranslator< ALLOC >::allocator_type& alloc) :
114  DBTranslator< ALLOC >::allocator_type(alloc),
118  _back_dico(std::move(from._back_dico)), _val_type(from._val_type) {
119  GUM_CONS_MOV(DBTranslator);
120  }
121 
122 
124  template < template < typename > class ALLOC >
125  INLINE DBTranslator< ALLOC >::DBTranslator(DBTranslator< ALLOC >&& from) :
126  DBTranslator< ALLOC >(from, from.getAllocator()) {}
127 
128 
130  template < template < typename > class ALLOC >
132  GUM_DESTRUCTOR(DBTranslator);
133  }
134 
135 
137  template < template < typename > class ALLOC >
138  INLINE DBTranslator< ALLOC >& DBTranslator< ALLOC >::
139  operator=(const DBTranslator< ALLOC >& from) {
140  if (this != &from) {
141  _is_dictionary_dynamic = from._is_dictionary_dynamic;
142  _max_dico_entries = from._max_dico_entries;
143  _missing_symbols = from._missing_symbols;
144  _back_dico = from._back_dico;
145  _val_type = from._val_type;
146  }
147  return *this;
148  }
149 
150 
152  template < template < typename > class ALLOC >
153  INLINE DBTranslator< ALLOC >& DBTranslator< ALLOC >::
154  operator=(DBTranslator< ALLOC >&& from) {
155  _is_dictionary_dynamic = from._is_dictionary_dynamic;
156  _max_dico_entries = from._max_dico_entries;
157  _missing_symbols = std::move(from._missing_symbols);
158  _back_dico = std::move(from._back_dico);
159  _val_type = from._val_type;
160 
161  return *this;
162  }
163 
164 
166  template < template < typename > class ALLOC >
167  INLINE DBTranslatedValue DBTranslator< ALLOC >::
168  operator<<(const std::string& str) {
169  return translate(str);
170  }
171 
172 
174  template < template < typename > class ALLOC >
175  INLINE std::string DBTranslator< ALLOC >::
176  operator>>(const DBTranslatedValue translated_val) {
177  return translateBack(translated_val);
178  }
179 
180 
182  template < template < typename > class ALLOC >
184  return _is_dictionary_dynamic;
185  }
186 
187 
189  template < template < typename > class ALLOC >
190  INLINE void DBTranslator< ALLOC >::setEditableDictionaryMode(bool new_mode) {
191  _is_dictionary_dynamic = new_mode;
192  }
193 
194 
196  template < template < typename > class ALLOC >
197  INLINE const Set< std::string, ALLOC< std::string > >&
199  return _missing_symbols;
200  }
201 
202 
204  template < template < typename > class ALLOC >
205  INLINE bool
206  DBTranslator< ALLOC >::isMissingSymbol(const std::string& str) const {
207  return _missing_symbols.exists(str);
208  }
209 
210 
212  template < template < typename > class ALLOC >
213  INLINE void
214  DBTranslator< ALLOC >::setVariableName(const std::string& str) const {
215  const_cast< Variable* >(this->variable())->setName(str);
216  }
217 
218 
220  template < template < typename > class ALLOC >
222  const std::string& str) const {
223  const_cast< Variable* >(this->variable())->setDescription(str);
224  }
225 
226 
228  template < template < typename > class ALLOC >
230  const DBTranslatedValue& value) const {
231  switch (_val_type) {
233  return value.discr_val == std::numeric_limits< std::size_t >::max();
234 
236  return value.cont_val == std::numeric_limits< float >::max();
237 
238  default:
239  GUM_ERROR(NotImplementedYet,
240  "No missing value interpretation for this "
241  "translated value type");
242  }
243  }
244 
245 
246  } /* namespace learning */
247 
248 } /* namespace gum */
249 
250 #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:393
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:117
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:379
void setVariableName(const std::string &str) const
sets the name of the variable stored into the translator
STL namespace.
gum is the global namespace for all aGrUM entities
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:604
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:549
Set< std::string, ALLOC< std::string > > _missing_symbols
the set of missing symbols
Definition: DBTranslator.h:382
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:396
bool _is_dictionary_dynamic
indicates whether the dictionary can be updated or not
Definition: DBTranslator.h:376
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
The base class for all the tabular databases&#39; cell translators.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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:610
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52