aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
DBCell_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The inlined implementation of DBCells
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 # include <agrum/tools/database/DBCell.h>
30 
31 namespace gum {
32 
33  namespace learning {
34 
35  /// default constructor
36  INLINE DBCell::DBCell() {
38  ;
39  }
40 
41 
42  /// constructor for a real number
43  INLINE DBCell::DBCell(const float nb) : _type_(DBCell::EltType::REAL), _val_real_(nb) {
45  }
46 
47 
48  /// constructor for an integer number
51  }
52 
53 
54  /// constructor for a string
56  // store the string into the static list of strings
57  if (!_strings_().existsFirst(str)) {
61  } else {
63  }
64 
66  }
67 
68 
69  /// copy constructor
72 
73  // for debugging
75  }
76 
77 
78  /// move constructor
81 
82  // for debugging
84  }
85 
86 
87  /// destructor
88  INLINE DBCell::~DBCell() {
90  ;
91  }
92 
93 
94  /// copy operator
95  INLINE DBCell& DBCell::operator=(const DBCell& from) {
96  if (this != &from) {
97  _type_ = from._type_;
99  }
100 
101  return *this;
102  }
103 
104 
105  /// move operator
107  if (this != &from) {
108  _type_ = from._type_;
110  }
111 
112  return *this;
113  }
114 
115 
116  /// assignment operator
117  INLINE DBCell& DBCell::operator=(const float x) {
118  _type_ = EltType::REAL;
119  _val_real_ = x;
120  return *this;
121  }
122 
123 
124  /// assignment operator
125  INLINE DBCell& DBCell::operator=(const int x) {
127  _val_integer_ = x;
128  return *this;
129  }
130 
131 
132  /// assignment operator
133  INLINE DBCell& DBCell::operator=(const std::string& str) {
134  if (!_strings_().existsFirst(str)) {
138  } else {
140  }
141  _type_ = EltType::STRING;
142 
143  return *this;
144  }
145 
146 
147  /// test of equality
148  INLINE bool DBCell::operator==(const DBCell& from) const {
149  return (_type_ == from._type_)
150  && ((_type_ == EltType::MISSING)
151  || ((_type_ == EltType::REAL) && (_val_real_ == from._val_real_))
153  }
154 
155 
156  /// test of inequality
157  INLINE bool DBCell::operator!=(const DBCell& from) const { return !operator==(from); }
158 
159 
160  /// returns the current type of the DBCell
161  INLINE DBCell::EltType DBCell::type() const noexcept { return _type_; }
162 
163 
164  /// returns the DBcell as a float
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 
173  /// sets the content of the DBCell
174  INLINE void DBCell::setReal(const float x) {
175  _type_ = EltType::REAL;
176  _val_real_ = x;
177  }
178 
179 
180  /// sets the content of the DBCell from a string
181  INLINE void DBCell::setReal(const std::string& elt) {
182  if (!isReal(elt)) GUM_ERROR(TypeError, "the string does not contain a real number")
183  _val_real_ = std::stof(elt);
184  _type_ = EltType::REAL;
185  }
186 
187 
188  /// returns the DBcell as an integer
189  INLINE int DBCell::integer() const {
190  if (_type_ == EltType::INTEGER)
191  return _val_integer_;
192  else
193  GUM_ERROR(TypeError, _typeErrorMsg_("an integer"))
194  }
195 
196 
197  /// sets the content of the DBCell
198  INLINE void DBCell::setInteger(const int x) {
200  _val_integer_ = x;
201  }
202 
203 
204  /// sets the content of the DBCell from a string
205  INLINE void DBCell::setInteger(const std::string& elt) {
206  if (!isInteger(elt)) GUM_ERROR(TypeError, "the string does not contain an integer")
209  }
210 
211 
212  /// returns the DBcell as a string
213  INLINE const std::string& DBCell::string() const {
214  if (_type_ == EltType::STRING)
215  return _strings_().first(_val_index_);
216  else
217  GUM_ERROR(TypeError, _typeErrorMsg_("a string"))
218  }
219 
220 
221  /// returns the DBcell as a string index (if it contains a string)
222  INLINE int DBCell::stringIndex() const {
223  if (_type_ == EltType::STRING)
224  return _val_index_;
225  else
226  GUM_ERROR(TypeError, _typeErrorMsg_("a string"))
227  }
228 
229 
230  /// returns the DBcell as a string (without checking its type)
231  INLINE const std::string& DBCell::string(const int index) { return _strings_().first(index); }
232 
233 
234  /// set the content of the DBCell from a string
235  INLINE void DBCell::setString(const std::string& str) {
236  if (!_strings_().existsFirst(str)) {
240  } else {
242  }
243  _type_ = EltType::STRING;
244  }
245 
246 
247  /// sets the DBCell as a missing element
249 
250 
251  /// indicates whether the cell contains a missing value
252  INLINE bool DBCell::isMissing() const { return _type_ == EltType::MISSING; }
253 
254 
255  } /* namespace learning */
256 
257 } /* namespace gum */
258 
259 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)