aGrUM 2.3.2
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-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
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 <type_traits>
60
61namespace gum {
62
63 namespace learning {
64
93 class DBCell {
94 public:
96 enum class EltType : unsigned char { REAL, INTEGER, STRING, MISSING };
97
98 // ##########################################################################
100 // ##########################################################################
101
103
106
108 DBCell(const float nb);
109
111 DBCell(const int nb);
112
114 DBCell(const std::string& str);
115
117 DBCell(const DBCell& from);
118
120 DBCell(DBCell&& from);
121
124
126
127
128 // ##########################################################################
130 // ##########################################################################
131
133
135 DBCell& operator=(const DBCell& from);
136
139
141 DBCell& operator=(const float x);
142
144 DBCell& operator=(const int x);
145
147 DBCell& operator=(const std::string& x);
148
150 bool operator==(const DBCell& from) const;
151
153 bool operator!=(const DBCell& from) const;
154
156
157
158 // ##########################################################################
160 // ##########################################################################
161
163
165 EltType type() const noexcept;
166
168
169 bool convertType(const EltType newtype);
170
172
177 float real() const;
178
180 void setReal(const float x);
181
183
184 void setReal(const std::string& elt);
185
187
191 int integer() const;
192
194 void setInteger(const int x);
195
197
198 void setInteger(const std::string& elt);
199
201
204 const std::string& string() const;
205
212 int stringIndex() const;
213
215 void setString(const std::string& elt);
216
219
221 bool isMissing() const;
222
224
225
226 // ##########################################################################
228 // ##########################################################################
229
231
233
235 static const std::string& string(const int index);
236
238
242 static EltType bestType(const std::string& str,
243 const std::vector< std::string >& missingVals);
244
246
250 static DBCell bestDBCell(const std::string& str,
251 const std::vector< std::string >& missingVals);
252
254
257 std::string toString(const std::vector< std::string >& missingVals) const;
258
260 static bool isInteger(const std::string& str);
261
263 static bool isReal(const std::string& str);
264
266 static bool isMissing(const std::string& str, const std::vector< std::string >& missingVals);
267
269
270
271#ifndef DOXYGEN_SHOULD_SKIP_THIS
272
273 private:
274 // the real type of the last element read from the database
276
277 // the element read from the database
278 union {
279 int _val_index_; // stores string indices. Basically, it should have
280 int _val_integer_; // been an Idx, but int are shorter than Idx.
281 float _val_real_;
282 };
283
284 // determine the longest type of the union. This is used for fast
285 // copying/moving DBCells
286 using UnionType = typename std::conditional< sizeof(int) < sizeof(float), float, int >::type;
287
288 // raises an appropriate exception when encountering a type error
289 std::string _typeErrorMsg_(const std::string& real_type) const;
290
291
292 // a bijection assigning to each string index its corresponding string
293 static Bijection< std::string, int >& _strings_();
294
295 // the last index used so far
296 static int _string_max_index_;
297
298#endif /* DOXYGEN_SHOULD_SKIP_THIS */
299 };
300
301 } /* namespace learning */
302
303} /* namespace gum */
304
306#ifndef GUM_NO_INLINE
308#endif /* GUM_NO_INLINE */
309
310#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:1594
EltType type() const noexcept
returns the current type of the DBCell
void setString(const std::string &elt)
sets the content of the DBCell
static EltType bestType(const std::string &str, const std::vector< std::string > &missingVals)
returns the best type to store a given element encoded as a string
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:96
DBCell & operator=(DBCell &&from)
move operator
static DBCell bestDBCell(const std::string &str, const std::vector< std::string > &missingVals)
returns the DBCell with the best type for an element encoded as a string
void setInteger(const int x)
sets the content of the DBCell
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 bool isInteger(const std::string &str)
determines whether a string corresponds precisely to an integer
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 bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
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
bool operator!=(const DBCell &from) const
test of inequality
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:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.