aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
49
50#include <agrum/base/database/DBCell.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54
55namespace gum {
56
57 namespace learning {
58
60 INLINE DBCell::DBCell() : _val_index_(0) {
61 GUM_CONSTRUCTOR(DBCell);
62 ;
63 }
64
66 INLINE DBCell::DBCell(const float nb) : _type_(DBCell::EltType::REAL), _val_real_(nb) {
67 GUM_CONSTRUCTOR(DBCell);
68 }
69
71 INLINE DBCell::DBCell(const int nb) : _type_(DBCell::EltType::INTEGER), _val_integer_(nb) {
72 GUM_CONSTRUCTOR(DBCell);
73 }
74
76 INLINE DBCell::DBCell(const std::string& str) : _type_(DBCell::EltType::STRING) {
77 // store the string into the static list of strings
78 if (!_strings_().existsFirst(str)) {
79 _strings_().insert(str, _string_max_index_);
80 _val_index_ = _string_max_index_;
81 ++_string_max_index_;
82 } else {
83 _val_index_ = _strings_().second(str);
84 }
85
86 GUM_CONSTRUCTOR(DBCell);
87 }
88
90 INLINE DBCell::DBCell(const DBCell& from) : _type_(from._type_) {
91 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
92
93 // for debugging
94 GUM_CONS_CPY(DBCell);
95 }
96
98 INLINE DBCell::DBCell(DBCell&& from) : _type_(from._type_) {
99 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
100
101 // for debugging
102 GUM_CONS_MOV(DBCell);
103 }
104
106 INLINE DBCell::~DBCell() {
107 GUM_DESTRUCTOR(DBCell);
108 ;
109 }
110
112 INLINE DBCell& DBCell::operator=(const DBCell& from) {
113 if (this != &from) {
114 _type_ = from._type_;
115 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
116 }
117
118 return *this;
119 }
120
122 INLINE DBCell& DBCell::operator=(DBCell&& from) {
123 if (this != &from) {
124 _type_ = from._type_;
125 std::memcpy(&_val_index_, &(from._val_index_), sizeof(UnionType));
126 }
127
128 return *this;
129 }
130
132 INLINE DBCell& DBCell::operator=(const float x) {
133 _type_ = EltType::REAL;
134 _val_real_ = x;
135 return *this;
136 }
137
139 INLINE DBCell& DBCell::operator=(const int x) {
140 _type_ = EltType::INTEGER;
141 _val_integer_ = x;
142 return *this;
143 }
144
146 INLINE DBCell& DBCell::operator=(const std::string& str) {
147 if (!_strings_().existsFirst(str)) {
148 _strings_().insert(str, _string_max_index_);
149 _val_index_ = _string_max_index_;
150 ++_string_max_index_;
151 } else {
152 _val_index_ = _strings_().second(str);
153 }
154 _type_ = EltType::STRING;
155
156 return *this;
157 }
158
160 INLINE bool DBCell::operator==(const DBCell& from) const {
161 return (_type_ == from._type_)
162 && ((_type_ == EltType::MISSING)
163 || ((_type_ == EltType::REAL) && (_val_real_ == from._val_real_))
164 || (_val_integer_ == from._val_integer_));
165 }
166
168 INLINE DBCell::EltType DBCell::type() const noexcept { return _type_; }
169
171 INLINE float DBCell::real() const {
172 if (_type_ == EltType::REAL) return _val_real_;
173 else GUM_ERROR(TypeError, _typeErrorMsg_("a real number"))
174 }
175
177 INLINE void DBCell::setReal(const float x) {
178 _type_ = EltType::REAL;
179 _val_real_ = x;
180 }
181
183 INLINE void DBCell::setReal(std::string_view elt) {
184 if (!isReal(elt)) GUM_ERROR(TypeError, "the string does not contain a real number")
185 _val_real_ = std::stof(std::string(elt));
186 _type_ = EltType::REAL;
187 }
188
190 INLINE int DBCell::integer() const {
191 if (_type_ == EltType::INTEGER) return _val_integer_;
192 else GUM_ERROR(TypeError, _typeErrorMsg_("an integer"))
193 }
194
196 INLINE void DBCell::setInteger(const int x) {
197 _type_ = EltType::INTEGER;
198 _val_integer_ = x;
199 }
200
202 INLINE void DBCell::setInteger(std::string_view elt) {
203 if (!isInteger(elt)) GUM_ERROR(TypeError, "the string does not contain an integer")
204 _val_integer_ = std::stoi(std::string(elt));
205 _type_ = EltType::INTEGER;
206 }
207
209 INLINE const std::string& DBCell::string() const {
210 if (_type_ == EltType::STRING) return _strings_().first(_val_index_);
211 else GUM_ERROR(TypeError, _typeErrorMsg_("a string"))
212 }
213
215 INLINE int DBCell::stringIndex() const {
216 if (_type_ == EltType::STRING) return _val_index_;
217 else GUM_ERROR(TypeError, _typeErrorMsg_("a string"))
218 }
219
221 INLINE const std::string& DBCell::string(const int index) { return _strings_().first(index); }
222
224 INLINE void DBCell::setString(const std::string& str) {
225 if (!_strings_().existsFirst(str)) {
226 _strings_().insert(str, _string_max_index_);
227 _val_index_ = _string_max_index_;
228 ++_string_max_index_;
229 } else {
230 _val_index_ = _strings_().second(str);
231 }
232 _type_ = EltType::STRING;
233 }
234
236 INLINE void DBCell::setMissingState() { _type_ = EltType::MISSING; }
237
239 INLINE bool DBCell::isMissing() const { return _type_ == EltType::MISSING; }
240
241 // checks whether a string correspond to a missing value
242 INLINE bool DBCell::isMissing(std::string_view str,
243 const std::vector< std::string >& missingVals) {
244 for (const auto& missing: missingVals) {
245 if (str == missing) return true;
246 }
247 return false;
248 }
249
250 // returns the best type to store a given element encoded as a string
251 INLINE DBCell::EltType DBCell::bestType(std::string_view str,
252 const std::vector< std::string >& missingVals) {
253 if (isMissing(str, missingVals)) return EltType::MISSING;
254 if (isInteger(str)) return EltType::INTEGER;
255 if (isReal(str)) return EltType::REAL;
256 return EltType::STRING;
257 }
258
259 // returns the DBCell with the best type for an element encoded as a string
260 INLINE DBCell DBCell::bestDBCell(std::string_view str,
261 const std::vector< std::string >& missingVals) {
262 if (isMissing(str, missingVals)) return {};
263 if (isInteger(str)) return DBCell(std::stoi(std::string(str)));
264 if (isReal(str)) return DBCell(std::stof(std::string(str)));
265
266 return DBCell(std::string(str));
267 }
268
269
270 } /* namespace learning */
271
272} /* namespace gum */
273
274#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:76
bool isInteger(std::string_view val)
return true is a string contains an integer value
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.