aGrUM  0.14.2
DBCell_inl.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 DOXYGEN_SHOULD_SKIP_THIS
26 
28 
29 namespace gum {
30 
31  namespace learning {
32 
34  INLINE DBCell::DBCell() { GUM_CONSTRUCTOR(DBCell); }
35 
36 
38  INLINE DBCell::DBCell(const float nb) :
39  __type(DBCell::EltType::REAL), __val_real(nb) {
40  GUM_CONSTRUCTOR(DBCell);
41  }
42 
43 
45  INLINE DBCell::DBCell(const int nb) :
46  __type(DBCell::EltType::INTEGER), __val_integer(nb) {
47  GUM_CONSTRUCTOR(DBCell);
48  }
49 
50 
52  INLINE DBCell::DBCell(const std::string& str) :
53  __type(DBCell::EltType::STRING) {
54  // store the string into the static list of strings
55  if (!__strings().existsFirst(str)) {
56  __strings().insert(str, __string_max_index);
57  __val_index = __string_max_index;
58  ++__string_max_index;
59  } else {
60  __val_index = __strings().second(str);
61  }
62 
63  GUM_CONSTRUCTOR(DBCell);
64  }
65 
66 
68  INLINE DBCell::DBCell(const DBCell& from) : __type(from.__type) {
69  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
70 
71  // for debugging
72  GUM_CONS_CPY(DBCell);
73  }
74 
75 
77  INLINE DBCell::DBCell(DBCell&& from) : __type(from.__type) {
78  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
79 
80  // for debugging
81  GUM_CONS_MOV(DBCell);
82  }
83 
84 
86  INLINE DBCell::~DBCell() { GUM_DESTRUCTOR(DBCell); }
87 
88 
90  INLINE DBCell& DBCell::operator=(const DBCell& from) {
91  if (this != &from) {
92  __type = from.__type;
93  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
94  }
95 
96  return *this;
97  }
98 
99 
101  INLINE DBCell& DBCell::operator=(DBCell&& from) {
102  if (this != &from) {
103  __type = from.__type;
104  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
105  }
106 
107  return *this;
108  }
109 
110 
112  INLINE DBCell& DBCell::operator=(const float x) {
113  __type = EltType::REAL;
114  __val_real = x;
115  return *this;
116  }
117 
118 
120  INLINE DBCell& DBCell::operator=(const int x) {
121  __type = EltType::INTEGER;
122  __val_integer = x;
123  return *this;
124  }
125 
126 
128  INLINE DBCell& DBCell::operator=(const std::string& str) {
129  if (!__strings().existsFirst(str)) {
130  __strings().insert(str, __string_max_index);
131  __val_index = __string_max_index;
132  ++__string_max_index;
133  } else {
134  __val_index = __strings().second(str);
135  }
136  __type = EltType::STRING;
137 
138  return *this;
139  }
140 
141 
143  INLINE bool DBCell::operator==(const DBCell& from) const {
144  return (__type == from.__type)
145  && ((__type == EltType::MISSING)
146  || ((__type == EltType::REAL) && (__val_real == from.__val_real))
147  || (__val_integer == from.__val_integer));
148  }
149 
150 
152  INLINE bool DBCell::operator!=(const DBCell& from) const {
153  return !operator==(from);
154  }
155 
156 
158  INLINE DBCell::EltType DBCell::type() const noexcept { return __type; }
159 
160 
162  INLINE float DBCell::real() const {
163  if (__type == EltType::REAL)
164  return __val_real;
165  else
166  GUM_ERROR(TypeError, __typeErrorMsg("a real number"));
167  }
168 
169 
171  INLINE void DBCell::setReal(const float x) {
172  __type = EltType::REAL;
173  __val_real = x;
174  }
175 
176 
178  INLINE void DBCell::setReal(const std::string& elt) {
179  if (!isReal(elt))
180  GUM_ERROR(TypeError, "the string does not contain a real number");
181  __val_real = std::stof(elt);
182  __type = EltType::REAL;
183  }
184 
185 
187  INLINE int DBCell::integer() const {
188  if (__type == EltType::INTEGER)
189  return __val_integer;
190  else
191  GUM_ERROR(TypeError, __typeErrorMsg("an integer"));
192  }
193 
194 
196  INLINE void DBCell::setInteger(const int x) {
197  __type = EltType::INTEGER;
198  __val_integer = x;
199  }
200 
201 
203  INLINE void DBCell::setInteger(const std::string& elt) {
204  if (!isInteger(elt))
205  GUM_ERROR(TypeError, "the string does not contain an integer");
206  __val_integer = std::stoi(elt);
207  __type = EltType::INTEGER;
208  }
209 
210 
212  INLINE const std::string& DBCell::string() const {
213  if (__type == EltType::STRING)
214  return __strings().first(__val_index);
215  else
216  GUM_ERROR(TypeError, __typeErrorMsg("a string"));
217  }
218 
219 
221  INLINE int DBCell::stringIndex() const {
222  if (__type == EltType::STRING)
223  return __val_index;
224  else
225  GUM_ERROR(TypeError, __typeErrorMsg("a string"));
226  }
227 
228 
230  INLINE const std::string& DBCell::string(const int index) {
231  return __strings().first(index);
232  }
233 
234 
236  INLINE void DBCell::setString(const std::string& str) {
237  if (!__strings().existsFirst(str)) {
238  __strings().insert(str, __string_max_index);
239  __val_index = __string_max_index;
240  ++__string_max_index;
241  } else {
242  __val_index = __strings().second(str);
243  }
244  __type = EltType::STRING;
245  }
246 
247 
249  INLINE void DBCell::setMissingState() { __type = EltType::MISSING; }
250 
251 
253  INLINE bool DBCell::isMissing() const { return __type == EltType::MISSING; }
254 
255 
256  } /* namespace learning */
257 
258 } /* namespace gum */
259 
260 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
~DBCell()
destructor
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
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
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
The class representing the original values of the cells of databases.
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
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
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
void setMissingState()
sets the DBCell as a missing element
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
DBCell()
default constructor (ontains a missing value)