aGrUM  0.14.2
DBCell.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  ***************************************************************************/
25 #ifndef GUM_LEARNING_DB_CELL_H
26 #define GUM_LEARNING_DB_CELL_H
27 
28 #include <cstring>
29 #include <stdexcept>
30 #include <string>
31 #include <type_traits>
32 #include <utility>
33 
34 #include <agrum/agrum.h>
35 #include <agrum/core/bijection.h>
36 
37 namespace gum {
38 
39  namespace learning {
40 
69  class DBCell {
70  public:
72  enum class EltType : unsigned char { REAL, INTEGER, STRING, MISSING };
73 
74  // ##########################################################################
76  // ##########################################################################
77 
79 
81  DBCell();
82 
84  DBCell(const float nb);
85 
87  DBCell(const int nb);
88 
90  DBCell(const std::string& str);
91 
93  DBCell(const DBCell& from);
94 
96  DBCell(DBCell&& from);
97 
99  ~DBCell();
100 
102 
103 
104  // ##########################################################################
106  // ##########################################################################
107 
109 
111  DBCell& operator=(const DBCell& from);
112 
114  DBCell& operator=(DBCell&& from);
115 
117  DBCell& operator=(const float x);
118 
120  DBCell& operator=(const int x);
121 
123  DBCell& operator=(const std::string& x);
124 
126  bool operator==(const DBCell& from) const;
127 
129  bool operator!=(const DBCell& from) const;
130 
132 
133 
134  // ##########################################################################
136  // ##########################################################################
137 
139 
141  EltType type() const noexcept;
142 
144 
145  bool convertType(const EltType newtype);
146 
148 
153  float real() const;
154 
156  void setReal(const float x);
157 
159 
160  void setReal(const std::string& elt);
161 
163 
167  int integer() const;
168 
170  void setInteger(const int x);
171 
173 
174  void setInteger(const std::string& elt);
175 
177 
180  const std::string& string() const;
181 
188  int stringIndex() const;
189 
191  void setString(const std::string& elt);
192 
194  void setMissingState();
195 
197  bool isMissing() const;
198 
200 
201 
202  // ##########################################################################
204  // ##########################################################################
205 
207 
209 
211  static const std::string& string(const int index);
212 
214 
218  template < template < typename > class ALLOC = std::allocator >
219  static EltType bestType(
220  const std::string& str,
221  const std::vector< std::string, ALLOC< std::string > >& missingVals);
222 
224 
228  template < template < typename > class ALLOC = std::allocator >
229  static DBCell bestDBCell(
230  const std::string& str,
231  const std::vector< std::string, ALLOC< std::string > >& missingVals);
232 
234 
237  template < template < typename > class ALLOC = std::allocator >
238  std::string toString(const std::vector< std::string, ALLOC< std::string > >&
239  missingVals) const;
240 
242  static bool isInteger(const std::string& str);
243 
245  static bool isReal(const std::string& str);
246 
248  template < template < typename > class ALLOC = std::allocator >
249  static bool isMissing(
250  const std::string& str,
251  const std::vector< std::string, ALLOC< std::string > >& missingVals);
252 
254 
255 
256 #ifndef DOXYGEN_SHOULD_SKIP_THIS
257 
258  private:
259  // the real type of the last element read from the database
260  EltType __type{EltType::MISSING};
261 
262  // the element read from the database
263  union {
264  int __val_index; // stores string indices. Basically, it should have
265  int __val_integer; // been an Idx, but int are shorter than Idx.
266  float __val_real;
267  };
268 
269 
270  // determine the longest type of the union. This is used for fast
271  // copying/moving DBCells
272  using UnionType = typename std::
273  conditional< sizeof(int) < sizeof(float), float, int >::type;
274 
275  // raises an appropriate exception when encountering a type error
276  std::string __typeErrorMsg(const std::string& real_type) const;
277 
278 
279  // a bijection assigning to each string index its corresponding string
280  static Bijection< std::string, int >& __strings();
281 
282  // the last index used so far
283  static int __string_max_index;
284 
285 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
286  };
287 
288  } /* namespace learning */
289 
290 } /* namespace gum */
291 
293 #ifndef GUM_NO_INLINE
295 #endif /* GUM_NO_INLINE */
296 
298 
299 #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:69
The inlined implementation of DBCells.
gum is the global namespace for all aGrUM entities
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
The inlined implementation of DBCells.
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:1803
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:72
Set of pairs of elements with fast search for both elements.
void setMissingState()
sets the DBCell as a missing element
DBCell()
default constructor (ontains a missing value)