aGrUM  0.16.0
DBCell_inl.h
Go to the documentation of this file.
1 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
31 
32 namespace gum {
33 
34  namespace learning {
35 
37  INLINE DBCell::DBCell() { GUM_CONSTRUCTOR(DBCell); }
38 
39 
41  INLINE DBCell::DBCell(const float nb) :
42  __type(DBCell::EltType::REAL), __val_real(nb) {
43  GUM_CONSTRUCTOR(DBCell);
44  }
45 
46 
48  INLINE DBCell::DBCell(const int nb) :
49  __type(DBCell::EltType::INTEGER), __val_integer(nb) {
50  GUM_CONSTRUCTOR(DBCell);
51  }
52 
53 
55  INLINE DBCell::DBCell(const std::string& str) :
56  __type(DBCell::EltType::STRING) {
57  // store the string into the static list of strings
58  if (!__strings().existsFirst(str)) {
59  __strings().insert(str, __string_max_index);
60  __val_index = __string_max_index;
61  ++__string_max_index;
62  } else {
63  __val_index = __strings().second(str);
64  }
65 
66  GUM_CONSTRUCTOR(DBCell);
67  }
68 
69 
71  INLINE DBCell::DBCell(const DBCell& from) : __type(from.__type) {
72  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
73 
74  // for debugging
75  GUM_CONS_CPY(DBCell);
76  }
77 
78 
80  INLINE DBCell::DBCell(DBCell&& from) : __type(from.__type) {
81  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
82 
83  // for debugging
84  GUM_CONS_MOV(DBCell);
85  }
86 
87 
89  INLINE DBCell::~DBCell() { GUM_DESTRUCTOR(DBCell); }
90 
91 
93  INLINE DBCell& DBCell::operator=(const DBCell& from) {
94  if (this != &from) {
95  __type = from.__type;
96  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
97  }
98 
99  return *this;
100  }
101 
102 
104  INLINE DBCell& DBCell::operator=(DBCell&& from) {
105  if (this != &from) {
106  __type = from.__type;
107  std::memcpy(&__val_index, &(from.__val_index), sizeof(UnionType));
108  }
109 
110  return *this;
111  }
112 
113 
115  INLINE DBCell& DBCell::operator=(const float x) {
116  __type = EltType::REAL;
117  __val_real = x;
118  return *this;
119  }
120 
121 
123  INLINE DBCell& DBCell::operator=(const int x) {
124  __type = EltType::INTEGER;
125  __val_integer = x;
126  return *this;
127  }
128 
129 
131  INLINE DBCell& DBCell::operator=(const std::string& str) {
132  if (!__strings().existsFirst(str)) {
133  __strings().insert(str, __string_max_index);
134  __val_index = __string_max_index;
135  ++__string_max_index;
136  } else {
137  __val_index = __strings().second(str);
138  }
139  __type = EltType::STRING;
140 
141  return *this;
142  }
143 
144 
146  INLINE bool DBCell::operator==(const DBCell& from) const {
147  return (__type == from.__type)
148  && ((__type == EltType::MISSING)
149  || ((__type == EltType::REAL) && (__val_real == from.__val_real))
150  || (__val_integer == from.__val_integer));
151  }
152 
153 
155  INLINE bool DBCell::operator!=(const DBCell& from) const {
156  return !operator==(from);
157  }
158 
159 
161  INLINE DBCell::EltType DBCell::type() const noexcept { return __type; }
162 
163 
165  INLINE float DBCell::real() const {
166  if (__type == EltType::REAL)
167  return __val_real;
168  else
169  GUM_ERROR(TypeError, __typeErrorMsg("a real number"));
170  }
171 
172 
174  INLINE void DBCell::setReal(const float x) {
175  __type = EltType::REAL;
176  __val_real = x;
177  }
178 
179 
181  INLINE void DBCell::setReal(const std::string& elt) {
182  if (!isReal(elt))
183  GUM_ERROR(TypeError, "the string does not contain a real number");
184  __val_real = std::stof(elt);
185  __type = EltType::REAL;
186  }
187 
188 
190  INLINE int DBCell::integer() const {
191  if (__type == EltType::INTEGER)
192  return __val_integer;
193  else
194  GUM_ERROR(TypeError, __typeErrorMsg("an integer"));
195  }
196 
197 
199  INLINE void DBCell::setInteger(const int x) {
200  __type = EltType::INTEGER;
201  __val_integer = x;
202  }
203 
204 
206  INLINE void DBCell::setInteger(const std::string& elt) {
207  if (!isInteger(elt))
208  GUM_ERROR(TypeError, "the string does not contain an integer");
209  __val_integer = std::stoi(elt);
210  __type = EltType::INTEGER;
211  }
212 
213 
215  INLINE const std::string& DBCell::string() const {
216  if (__type == EltType::STRING)
217  return __strings().first(__val_index);
218  else
219  GUM_ERROR(TypeError, __typeErrorMsg("a string"));
220  }
221 
222 
224  INLINE int DBCell::stringIndex() const {
225  if (__type == EltType::STRING)
226  return __val_index;
227  else
228  GUM_ERROR(TypeError, __typeErrorMsg("a string"));
229  }
230 
231 
233  INLINE const std::string& DBCell::string(const int index) {
234  return __strings().first(index);
235  }
236 
237 
239  INLINE void DBCell::setString(const std::string& str) {
240  if (!__strings().existsFirst(str)) {
241  __strings().insert(str, __string_max_index);
242  __val_index = __string_max_index;
243  ++__string_max_index;
244  } else {
245  __val_index = __strings().second(str);
246  }
247  __type = EltType::STRING;
248  }
249 
250 
252  INLINE void DBCell::setMissingState() { __type = EltType::MISSING; }
253 
254 
256  INLINE bool DBCell::isMissing() const { return __type == EltType::MISSING; }
257 
258 
259  } /* namespace learning */
260 
261 } /* namespace gum */
262 
263 #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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:75
void setMissingState()
sets the DBCell as a missing element
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
DBCell()
default constructor (ontains a missing value)