aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
DBCell.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
47#ifndef GUM_LEARNING_DB_CELL_H
48#define GUM_LEARNING_DB_CELL_H
49
50#include <cstring>
51#include <stdexcept>
52#include <string>
53#include <utility>
54
55#include <agrum/agrum.h>
56
58
59#include <string_view>
60#include <type_traits>
61
62namespace gum {
63
64 namespace learning {
65
94 class DBCell {
95 public:
97 enum class EltType : unsigned char { REAL, INTEGER, STRING, MISSING };
98
99 // ##########################################################################
101 // ##########################################################################
102
104
107
109 DBCell(const float nb);
110
112 DBCell(const int nb);
113
115 DBCell(const std::string& str);
116
118 DBCell(const DBCell& from);
119
121 DBCell(DBCell&& from);
122
125
127
128
129 // ##########################################################################
131 // ##########################################################################
132
134
136 DBCell& operator=(const DBCell& from);
137
140
142 DBCell& operator=(const float x);
143
145 DBCell& operator=(const int x);
146
148 DBCell& operator=(const std::string& x);
149
151 bool operator==(const DBCell& from) const;
152
153
155
156
157 // ##########################################################################
159 // ##########################################################################
160
162
164 EltType type() const noexcept;
165
167
168 bool convertType(const EltType newtype);
169
171
176 float real() const;
177
179 void setReal(const float x);
180
182
183 void setReal(std::string_view elt);
184
186
190 int integer() const;
191
193 void setInteger(const int x);
194
196
197 void setInteger(std::string_view elt);
198
200
203 const std::string& string() const;
204
211 int stringIndex() const;
212
214 void setString(const std::string& elt);
215
218
220 bool isMissing() const;
221
223
224
225 // ##########################################################################
227 // ##########################################################################
228
230
232
234 static const std::string& string(const int index);
235
237
241 static EltType bestType(std::string_view str, const std::vector< std::string >& missingVals);
242
244
248 static DBCell bestDBCell(std::string_view str, const std::vector< std::string >& missingVals);
249
251
254 std::string toString(const std::vector< std::string >& missingVals) const;
255
257 static bool isInteger(std::string_view str);
258
260 static bool isReal(std::string_view str);
261
263 static bool isMissing(std::string_view str, const std::vector< std::string >& missingVals);
264
266
267
268#ifndef DOXYGEN_SHOULD_SKIP_THIS
269
270 private:
271 // the real type of the last element read from the database
273
274 // the element read from the database
275 union {
276 int _val_index_; // stores string indices. Basically, it should have
277 int _val_integer_; // been an Idx, but int are shorter than Idx.
278 float _val_real_;
279 };
280
281 // determine the longest type of the union. This is used for fast
282 // copying/moving DBCells
283 using UnionType = typename std::conditional< sizeof(int) < sizeof(float), float, int >::type;
284
285 // raises an appropriate exception when encountering a type error
286 std::string _typeErrorMsg_(std::string_view real_type) const;
287
288
289 // a bijection assigning to each string index its corresponding string
290 static Bijection< std::string, int >& _strings_();
291
292 // the last index used so far
293 static int _string_max_index_;
294
295#endif /* DOXYGEN_SHOULD_SKIP_THIS */
296 };
297
298 } /* namespace learning */
299
300} /* namespace gum */
301
303#ifndef GUM_NO_INLINE
305#endif /* GUM_NO_INLINE */
306
307#endif /* GUM_LEARNING_DB_CELL_H */
The inlined implementation of DBCells.
Set of pairs of elements with fast search for both elements.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1640
EltType type() const noexcept
returns the current type of the DBCell
void setString(const std::string &elt)
sets the content of the DBCell
DBCell(const std::string &str)
constructor for a string
DBCell()
default constructor (ontains a missing value)
EltType
the set of types possibly taken by the last element read
Definition DBCell.h:97
DBCell & operator=(DBCell &&from)
move operator
void setInteger(const int x)
sets the content of the DBCell
static bool isReal(std::string_view str)
determine whether a string corresponds precisely to a real number
bool operator==(const DBCell &from) const
test of equality
int integer() const
returns the DBcell as an integer
bool isMissing() const
indicates whether the cell contains a missing value
static DBCell bestDBCell(std::string_view str, const std::vector< std::string > &missingVals)
returns the DBCell with the best type for an element encoded as a string
DBCell & operator=(const int x)
assignment operator
DBCell(const float nb)
constructor for a real number
std::string toString(const std::vector< std::string > &missingVals) const
returns the content of the DBCell as a string, whatever its type
static EltType bestType(std::string_view str, const std::vector< std::string > &missingVals)
returns the best type to store a given element encoded as a string
float real() const
returns the DBcell as a real number
DBCell(const DBCell &from)
copy constructor
DBCell & operator=(const DBCell &from)
copy operator
void setMissingState()
sets the DBCell as a missing element
static bool isInteger(std::string_view str)
determines whether a string corresponds precisely to an integer
int stringIndex() const
returns the DBcell as the index of a string in a static bijection
void setReal(const float x)
sets the content of the DBCell
bool convertType(const EltType newtype)
try to convert the content of the DBCell into another type
DBCell(const int nb)
constructor for an integer number
DBCell & operator=(const std::string &x)
assignment operator
DBCell(DBCell &&from)
move constructor
DBCell & operator=(const float x)
assignment operator
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.