aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBCell_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
52
53namespace gum {
54
55 namespace learning {
56
58 INLINE DBCell::DBCell() : _val_index_(0) {
59 GUM_CONSTRUCTOR(DBCell);
60 ;
61 }
62
64 INLINE DBCell::DBCell(const float nb) : _type_(DBCell::EltType::REAL), _val_real_(nb) {
65 GUM_CONSTRUCTOR(DBCell);
66 }
67
69 INLINE DBCell::DBCell(const int nb) : _type_(DBCell::EltType::INTEGER), _val_integer_(nb) {
70 GUM_CONSTRUCTOR(DBCell);
71 }
72
74 INLINE DBCell::DBCell(const std::string& str) : _type_(DBCell::EltType::STRING) {
75 // store the string into the static list of strings
76 if (!_strings_().existsFirst(str)) {
77 _strings_().insert(str, _string_max_index_);
78 _val_index_ = _string_max_index_;
79 ++_string_max_index_;
80 } else {
81 _val_index_ = _strings_().second(str);
82 }
83
84 GUM_CONSTRUCTOR(DBCell);
85 }
86
88 INLINE DBCell::DBCell(const DBCell& from) : _type_(from._type_) {
89 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
90
91 // for debugging
92 GUM_CONS_CPY(DBCell);
93 }
94
96 INLINE DBCell::DBCell(DBCell&& from) : _type_(from._type_) {
97 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
98
99 // for debugging
100 GUM_CONS_MOV(DBCell);
101 }
102
104 INLINE DBCell::~DBCell() {
105 GUM_DESTRUCTOR(DBCell);
106 ;
107 }
108
110 INLINE DBCell& DBCell::operator=(const DBCell& from) {
111 if (this != &from) {
112 _type_ = from._type_;
113 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
114 }
115
116 return *this;
117 }
118
120 INLINE DBCell& DBCell::operator=(DBCell&& from) {
121 if (this != &from) {
122 _type_ = from._type_;
123 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
124 }
125
126 return *this;
127 }
128
130 INLINE DBCell& DBCell::operator=(const float x) {
131 _type_ = EltType::REAL;
132 _val_real_ = x;
133 return *this;
134 }
135
137 INLINE DBCell& DBCell::operator=(const int x) {
138 _type_ = EltType::INTEGER;
139 _val_integer_ = x;
140 return *this;
141 }
142
144 INLINE DBCell& DBCell::operator=(const std::string& str) {
145 if (!_strings_().existsFirst(str)) {
146 _strings_().insert(str, _string_max_index_);
147 _val_index_ = _string_max_index_;
148 ++_string_max_index_;
149 } else {
150 _val_index_ = _strings_().second(str);
151 }
152 _type_ = EltType::STRING;
153
154 return *this;
155 }
156
158 INLINE bool DBCell::operator==(const DBCell& from) const {
159 return (_type_ == from._type_)
160 && ((_type_ == EltType::MISSING)
161 || ((_type_ == EltType::REAL) && (_val_real_ == from._val_real_))
162 || (_val_integer_ == from._val_integer_));
163 }
164
166 INLINE bool DBCell::operator!=(const DBCell& from) const { return !operator==(from); }
167
169 INLINE DBCell::EltType DBCell::type() const noexcept { return _type_; }
170
172 INLINE float DBCell::real() const {
173 if (_type_ == EltType::REAL) return _val_real_;
174 else GUM_ERROR(TypeError, _typeErrorMsg_("a real number"))
175 }
176
178 INLINE void DBCell::setReal(const float x) {
179 _type_ = EltType::REAL;
180 _val_real_ = x;
181 }
182
184 INLINE void DBCell::setReal(const std::string& elt) {
185 if (!isReal(elt)) GUM_ERROR(TypeError, "the string does not contain a real number")
186 _val_real_ = std::stof(elt);
187 _type_ = EltType::REAL;
188 }
189
191 INLINE int DBCell::integer() const {
192 if (_type_ == EltType::INTEGER) return _val_integer_;
193 else GUM_ERROR(TypeError, _typeErrorMsg_("an integer"))
194 }
195
197 INLINE void DBCell::setInteger(const int x) {
198 _type_ = EltType::INTEGER;
199 _val_integer_ = x;
200 }
201
203 INLINE void DBCell::setInteger(const std::string& elt) {
204 if (!isInteger(elt)) GUM_ERROR(TypeError, "the string does not contain an integer")
205 _val_integer_ = std::stoi(elt);
206 _type_ = EltType::INTEGER;
207 }
208
210 INLINE const std::string& DBCell::string() const {
211 if (_type_ == EltType::STRING) return _strings_().first(_val_index_);
212 else GUM_ERROR(TypeError, _typeErrorMsg_("a string"))
213 }
214
216 INLINE int DBCell::stringIndex() const {
217 if (_type_ == EltType::STRING) return _val_index_;
218 else GUM_ERROR(TypeError, _typeErrorMsg_("a string"))
219 }
220
222 INLINE const std::string& DBCell::string(const int index) { return _strings_().first(index); }
223
225 INLINE void DBCell::setString(const std::string& str) {
226 if (!_strings_().existsFirst(str)) {
227 _strings_().insert(str, _string_max_index_);
228 _val_index_ = _string_max_index_;
229 ++_string_max_index_;
230 } else {
231 _val_index_ = _strings_().second(str);
232 }
233 _type_ = EltType::STRING;
234 }
235
237 INLINE void DBCell::setMissingState() { _type_ = EltType::MISSING; }
238
240 INLINE bool DBCell::isMissing() const { return _type_ == EltType::MISSING; }
241
242 // checks whether a string correspond to a missing value
243 INLINE bool DBCell::isMissing(const std::string& str,
244 const std::vector< std::string >& missingVals) {
245 for (auto missing: missingVals) {
246 if (str == missing) return true;
247 }
248 return false;
249 }
250
251 // returns the best type to store a given element encoded as a string
252 INLINE DBCell::EltType DBCell::bestType(const std::string& str,
253 const std::vector< std::string >& missingVals) {
254 if (isMissing(str, missingVals)) return EltType::MISSING;
255 if (isInteger(str)) return EltType::INTEGER;
256 if (isReal(str)) return EltType::REAL;
257 return EltType::STRING;
258 }
259
260 // returns the DBCell with the best type for an element encoded as a string
261 INLINE DBCell DBCell::bestDBCell(const std::string& str,
262 const std::vector< std::string >& missingVals) {
263 if (isMissing(str, missingVals)) return DBCell();
264 if (isInteger(str)) return DBCell(std::stoi(str));
265 if (isReal(str)) return DBCell(std::stof(str));
266
267 return DBCell(str);
268 }
269
270
271 } /* namespace learning */
272
273} /* namespace gum */
274
275#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The class representing the original values of the cells of databases.
Exception : wrong type for this operation.
DBCell()
default constructor (ontains a missing value)
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
bool isInteger(const std::string &val)
return true is a string contains an integer value
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:243