aGrUM  0.16.0
DBCell.h
Go to the documentation of this file.
1 
28 #ifndef GUM_LEARNING_DB_CELL_H
29 #define GUM_LEARNING_DB_CELL_H
30 
31 #include <cstring>
32 #include <stdexcept>
33 #include <string>
34 #include <type_traits>
35 #include <utility>
36 
37 #include <agrum/agrum.h>
38 #include <agrum/core/bijection.h>
39 
40 namespace gum {
41 
42  namespace learning {
43 
72  class DBCell {
73  public:
75  enum class EltType : unsigned char { REAL, INTEGER, STRING, MISSING };
76 
77  // ##########################################################################
79  // ##########################################################################
80 
82 
84  DBCell();
85 
87  DBCell(const float nb);
88 
90  DBCell(const int nb);
91 
93  DBCell(const std::string& str);
94 
96  DBCell(const DBCell& from);
97 
99  DBCell(DBCell&& from);
100 
102  ~DBCell();
103 
105 
106 
107  // ##########################################################################
109  // ##########################################################################
110 
112 
114  DBCell& operator=(const DBCell& from);
115 
117  DBCell& operator=(DBCell&& from);
118 
120  DBCell& operator=(const float x);
121 
123  DBCell& operator=(const int x);
124 
126  DBCell& operator=(const std::string& x);
127 
129  bool operator==(const DBCell& from) const;
130 
132  bool operator!=(const DBCell& from) const;
133 
135 
136 
137  // ##########################################################################
139  // ##########################################################################
140 
142 
144  EltType type() const noexcept;
145 
147 
148  bool convertType(const EltType newtype);
149 
151 
156  float real() const;
157 
159  void setReal(const float x);
160 
162 
163  void setReal(const std::string& elt);
164 
166 
170  int integer() const;
171 
173  void setInteger(const int x);
174 
176 
177  void setInteger(const std::string& elt);
178 
180 
183  const std::string& string() const;
184 
191  int stringIndex() const;
192 
194  void setString(const std::string& elt);
195 
197  void setMissingState();
198 
200  bool isMissing() const;
201 
203 
204 
205  // ##########################################################################
207  // ##########################################################################
208 
210 
212 
214  static const std::string& string(const int index);
215 
217 
221  template < template < typename > class ALLOC = std::allocator >
222  static EltType bestType(
223  const std::string& str,
224  const std::vector< std::string, ALLOC< std::string > >& missingVals);
225 
227 
231  template < template < typename > class ALLOC = std::allocator >
232  static DBCell bestDBCell(
233  const std::string& str,
234  const std::vector< std::string, ALLOC< std::string > >& missingVals);
235 
237 
240  template < template < typename > class ALLOC = std::allocator >
241  std::string toString(const std::vector< std::string, ALLOC< std::string > >&
242  missingVals) const;
243 
245  static bool isInteger(const std::string& str);
246 
248  static bool isReal(const std::string& str);
249 
251  template < template < typename > class ALLOC = std::allocator >
252  static bool isMissing(
253  const std::string& str,
254  const std::vector< std::string, ALLOC< std::string > >& missingVals);
255 
257 
258 
259 #ifndef DOXYGEN_SHOULD_SKIP_THIS
260 
261  private:
262  // the real type of the last element read from the database
263  EltType __type{EltType::MISSING};
264 
265  // the element read from the database
266  union {
267  int __val_index; // stores string indices. Basically, it should have
268  int __val_integer; // been an Idx, but int are shorter than Idx.
269  float __val_real;
270  };
271 
272 
273  // determine the longest type of the union. This is used for fast
274  // copying/moving DBCells
275  using UnionType = typename std::
276  conditional< sizeof(int) < sizeof(float), float, int >::type;
277 
278  // raises an appropriate exception when encountering a type error
279  std::string __typeErrorMsg(const std::string& real_type) const;
280 
281 
282  // a bijection assigning to each string index its corresponding string
283  static Bijection< std::string, int >& __strings();
284 
285  // the last index used so far
286  static int __string_max_index;
287 
288 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
289  };
290 
291  } /* namespace learning */
292 
293 } /* namespace gum */
294 
296 #ifndef GUM_NO_INLINE
298 #endif /* GUM_NO_INLINE */
299 
301 
302 #endif /* GUM_LEARNING_DB_CELL_H */
~DBCell()
destructor
bool convertType(const EltType newtype)
try to convert the content of the DBCell into another type
void setString(const std::string &elt)
sets the content of the DBCell
bool isMissing() const
indicates whether the cell contains a missing value
static bool isInteger(const std::string &str)
determines whether a string corresponds precisely to an integer
The class representing the original values of the cells of databases.
Definition: DBCell.h:72
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
static DBCell bestDBCell(const std::string &str, const std::vector< std::string, ALLOC< std::string > > &missingVals)
returns the DBCell with the best type for an element encoded as a string
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const std::string & string() const
returns the DBcell as a string
int integer() const
returns the DBcell as an integer
float real() const
returns the DBcell as a real number
static bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
bool operator!=(const DBCell &from) const
test of inequality
DBCell & operator=(const DBCell &from)
copy operator
void setReal(const float x)
sets the content of the DBCell
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
static EltType bestType(const std::string &str, const std::vector< std::string, ALLOC< std::string > > &missingVals)
returns the best type to store a given element encoded as a string
int stringIndex() const
returns the DBcell as the index of a string in a static bijection
EltType type() const noexcept
returns the current type of the DBCell
bool operator==(const DBCell &from) const
test of equality
std::string toString(const std::vector< std::string, ALLOC< std::string > > &missingVals) const
returns the content of the DBCell as a string, whatever its type
void setInteger(const int x)
sets the content of the DBCell
EltType
the set of types possibly taken by the last element read
Definition: DBCell.h:75
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setMissingState()
sets the DBCell as a missing element
DBCell()
default constructor (ontains a missing value)