aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
DBCell_inl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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
() {
GUM_CONSTRUCTOR
(
DBCell
); }
37
38
39
/// constructor for a real number
40
INLINE
DBCell
::
DBCell
(
const
float
nb
) :
41
type__
(
DBCell
::
EltType
::
REAL
),
val_real__
(
nb
) {
42
GUM_CONSTRUCTOR
(
DBCell
);
43
}
44
45
46
/// constructor for an integer number
47
INLINE
DBCell
::
DBCell
(
const
int
nb
) :
48
type__
(
DBCell
::
EltType
::
INTEGER
),
val_integer__
(
nb
) {
49
GUM_CONSTRUCTOR
(
DBCell
);
50
}
51
52
53
/// constructor for a string
54
INLINE
DBCell
::
DBCell
(
const
std
::
string
&
str
) :
55
type__
(
DBCell
::
EltType
::
STRING
) {
56
// store the string into the static list of strings
57
if
(!
strings__
().
existsFirst
(
str
)) {
58
strings__
().
insert
(
str
,
string_max_index__
);
59
val_index__
=
string_max_index__
;
60
++
string_max_index__
;
61
}
else
{
62
val_index__
=
strings__
().
second
(
str
);
63
}
64
65
GUM_CONSTRUCTOR
(
DBCell
);
66
}
67
68
69
/// copy constructor
70
INLINE
DBCell
::
DBCell
(
const
DBCell
&
from
) :
type__
(
from
.
type__
) {
71
std
::
memcpy
(&
val_index__
, &(
from
.
val_index__
),
sizeof
(
UnionType
));
72
73
// for debugging
74
GUM_CONS_CPY
(
DBCell
);
75
}
76
77
78
/// move constructor
79
INLINE
DBCell
::
DBCell
(
DBCell
&&
from
) :
type__
(
from
.
type__
) {
80
std
::
memcpy
(&
val_index__
, &(
from
.
val_index__
),
sizeof
(
UnionType
));
81
82
// for debugging
83
GUM_CONS_MOV
(
DBCell
);
84
}
85
86
87
/// destructor
88
INLINE
DBCell
::~
DBCell
() {
GUM_DESTRUCTOR
(
DBCell
); }
89
90
91
/// copy operator
92
INLINE DBCell&
DBCell
::
operator
=(
const
DBCell
&
from
) {
93
if
(
this
!= &
from
) {
94
type__
=
from
.
type__
;
95
std
::
memcpy
(&
val_index__
, &(
from
.
val_index__
),
sizeof
(
UnionType
));
96
}
97
98
return
*
this
;
99
}
100
101
102
/// move operator
103
INLINE
DBCell
&
DBCell
::
operator
=(
DBCell
&&
from
) {
104
if
(
this
!= &
from
) {
105
type__
=
from
.
type__
;
106
std
::
memcpy
(&
val_index__
, &(
from
.
val_index__
),
sizeof
(
UnionType
));
107
}
108
109
return
*
this
;
110
}
111
112
113
/// assignment operator
114
INLINE
DBCell
&
DBCell
::
operator
=(
const
float
x
) {
115
type__
=
EltType
::
REAL
;
116
val_real__
=
x
;
117
return
*
this
;
118
}
119
120
121
/// assignment operator
122
INLINE
DBCell
&
DBCell
::
operator
=(
const
int
x
) {
123
type__
=
EltType
::
INTEGER
;
124
val_integer__
=
x
;
125
return
*
this
;
126
}
127
128
129
/// assignment operator
130
INLINE
DBCell
&
DBCell
::
operator
=(
const
std
::
string
&
str
) {
131
if
(!
strings__
().
existsFirst
(
str
)) {
132
strings__
().
insert
(
str
,
string_max_index__
);
133
val_index__
=
string_max_index__
;
134
++
string_max_index__
;
135
}
else
{
136
val_index__
=
strings__
().
second
(
str
);
137
}
138
type__
=
EltType
::
STRING
;
139
140
return
*
this
;
141
}
142
143
144
/// test of equality
145
INLINE
bool
DBCell
::
operator
==(
const
DBCell
&
from
)
const
{
146
return
(
type__
==
from
.
type__
)
147
&& ((
type__
==
EltType
::
MISSING
)
148
|| ((
type__
==
EltType
::
REAL
) && (
val_real__
==
from
.
val_real__
))
149
|| (
val_integer__
==
from
.
val_integer__
));
150
}
151
152
153
/// test of inequality
154
INLINE
bool
DBCell
::
operator
!=(
const
DBCell
&
from
)
const
{
155
return
!
operator
==(
from
);
156
}
157
158
159
/// returns the current type of the DBCell
160
INLINE
DBCell
::
EltType
DBCell
::
type
()
const
noexcept
{
return
type__
; }
161
162
163
/// returns the DBcell as a float
164
INLINE
float
DBCell
::
real
()
const
{
165
if
(
type__
==
EltType
::
REAL
)
166
return
val_real__
;
167
else
168
GUM_ERROR
(
TypeError
,
typeErrorMsg__
(
"a real number"
));
169
}
170
171
172
/// sets the content of the DBCell
173
INLINE
void
DBCell
::
setReal
(
const
float
x
) {
174
type__
=
EltType
::
REAL
;
175
val_real__
=
x
;
176
}
177
178
179
/// sets the content of the DBCell from a string
180
INLINE
void
DBCell
::
setReal
(
const
std
::
string
&
elt
) {
181
if
(!
isReal
(
elt
))
182
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
) {
199
type__
=
EltType
::
INTEGER
;
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
))
207
GUM_ERROR
(
TypeError
,
"the string does not contain an integer"
);
208
val_integer__
=
std
::
stoi
(
elt
);
209
type__
=
EltType
::
INTEGER
;
210
}
211
212
213
/// returns the DBcell as a string
214
INLINE
const
std
::
string
&
DBCell
::
string
()
const
{
215
if
(
type__
==
EltType
::
STRING
)
216
return
strings__
().
first
(
val_index__
);
217
else
218
GUM_ERROR
(
TypeError
,
typeErrorMsg__
(
"a string"
));
219
}
220
221
222
/// returns the DBcell as a string index (if it contains a string)
223
INLINE
int
DBCell
::
stringIndex
()
const
{
224
if
(
type__
==
EltType
::
STRING
)
225
return
val_index__
;
226
else
227
GUM_ERROR
(
TypeError
,
typeErrorMsg__
(
"a string"
));
228
}
229
230
231
/// returns the DBcell as a string (without checking its type)
232
INLINE
const
std
::
string
&
DBCell
::
string
(
const
int
index
) {
233
return
strings__
().
first
(
index
);
234
}
235
236
237
/// set the content of the DBCell from a string
238
INLINE
void
DBCell
::
setString
(
const
std
::
string
&
str
) {
239
if
(!
strings__
().
existsFirst
(
str
)) {
240
strings__
().
insert
(
str
,
string_max_index__
);
241
val_index__
=
string_max_index__
;
242
++
string_max_index__
;
243
}
else
{
244
val_index__
=
strings__
().
second
(
str
);
245
}
246
type__
=
EltType
::
STRING
;
247
}
248
249
250
/// sets the DBCell as a missing element
251
INLINE
void
DBCell
::
setMissingState
() {
type__
=
EltType
::
MISSING
; }
252
253
254
/// indicates whether the cell contains a missing value
255
INLINE
bool
DBCell
::
isMissing
()
const
{
return
type__
==
EltType
::
MISSING
; }
256
257
258
}
/* namespace learning */
259
260
}
/* namespace gum */
261
262
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::learning::genericBNLearner::Database::Database
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Definition:
genericBNLearner_tpl.h:31