aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
multiDimSparse_tpl.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
/**
23
* @file
24
* @brief Implementation of MultiDimSparse.
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27
*/
28
29
namespace
gum
{
30
31
// Default constructor: creates an empty null dimensional matrix
32
33
template
<
typename
GUM_SCALAR >
34
MultiDimSparse< GUM_SCALAR >::MultiDimSparse(
const
GUM_SCALAR& default_value) :
35
MultiDimWithOffset< GUM_SCALAR >(), default_(default_value) {
36
// for debugging purposes
37
GUM_CONSTRUCTOR(MultiDimSparse);
38
}
39
40
// copy constructor
41
42
template
<
typename
GUM_SCALAR
>
43
MultiDimSparse
<
GUM_SCALAR
>::
MultiDimSparse
(
44
const
MultiDimSparse
<
GUM_SCALAR
>&
from
) :
45
MultiDimWithOffset
<
GUM_SCALAR
>(
from
),
46
params_
(
from
.
params_
),
default_
(
from
.
default_
) {
47
// for debugging purposes
48
GUM_CONS_CPY
(
MultiDimSparse
);
49
}
50
51
// destructor
52
53
template
<
typename
GUM_SCALAR
>
54
MultiDimSparse
<
GUM_SCALAR
>::~
MultiDimSparse
() {
55
// for debugging purposes
56
GUM_DESTRUCTOR
(
MultiDimSparse
);
57
// no need to unregister all slaves as it will be done by MultiDimWithOffset
58
}
59
60
// data access operator
61
62
template
<
typename
GUM_SCALAR
>
63
INLINE
GUM_SCALAR
64
MultiDimSparse
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
65
Size
key
;
66
67
if
(
i
.
isMaster
(
this
)) {
68
key
=
this
->
offsets_
[&
i
];
69
}
else
{
70
key
=
this
->
getOffs_
(
i
);
71
}
72
73
return
params_
.
exists
(
key
) ?
params_
[
key
] :
default_
;
74
}
75
76
template
<
typename
GUM_SCALAR
>
77
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
set
(
const
Instantiation
&
i
,
78
const
GUM_SCALAR
&
value
)
const
{
79
Size
key
;
80
81
if
(
i
.
isMaster
(
this
)) {
82
key
=
this
->
offsets_
[&
i
];
83
}
else
{
84
key
=
this
->
getOffs_
(
i
);
85
}
86
87
if
(
value
==
default_
) {
88
params_
.
reset
(
key
);
89
}
else
{
90
params_
.
set
(
key
,
value
);
91
}
92
}
93
94
// add a new dimension, needed for updating the offsets_ & gaps_
95
96
template
<
typename
GUM_SCALAR
>
97
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
add
(
const
DiscreteVariable
&
v
) {
98
MultiDimWithOffset
<
GUM_SCALAR
>::
add
(
v
);
99
fill
(
default_
);
100
}
101
102
// removes a dimension, needed for updating the offsets_ & gaps_
103
104
template
<
typename
GUM_SCALAR
>
105
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
erase
(
const
DiscreteVariable
&
v
) {
106
MultiDimWithOffset
<
GUM_SCALAR
>::
erase
(
v
);
107
fill
(
default_
);
108
}
109
110
// synchronise content after MultipleChanges
111
template
<
typename
GUM_SCALAR
>
112
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
commitMultipleChanges_
() {
113
fill
(
default_
);
114
}
115
116
// fill the array with the arg
117
template
<
typename
GUM_SCALAR
>
118
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
fill
(
const
GUM_SCALAR
&
d
)
const
{
119
params_
.
clear
();
120
default_
=
d
;
121
}
122
123
template
<
typename
GUM_SCALAR
>
124
INLINE
Size
MultiDimSparse
<
GUM_SCALAR
>::
realSize
()
const
{
125
return
params_
.
size
();
126
}
127
128
template
<
typename
GUM_SCALAR
>
129
INLINE
MultiDimContainer
<
GUM_SCALAR
>*
130
MultiDimSparse
<
GUM_SCALAR
>::
newFactory
()
const
{
131
return
new
MultiDimSparse
<
GUM_SCALAR
>(
default_
);
132
}
133
134
// returns the name of the implementation
135
template
<
typename
GUM_SCALAR
>
136
INLINE
const
std
::
string
&
MultiDimSparse
<
GUM_SCALAR
>::
name
()
const
{
137
static
const
std
::
string
str
=
"MultiDimSparse"
;
138
return
str
;
139
}
140
141
template
<
typename
GUM_SCALAR
>
142
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
replace_
(
const
DiscreteVariable
*
x
,
143
const
DiscreteVariable
*
y
) {
144
MultiDimImplementation
<
GUM_SCALAR
>::
replace_
(
x
,
y
);
145
}
146
147
template
<
typename
GUM_SCALAR
>
148
INLINE
GUM_SCALAR
&
149
MultiDimSparse
<
GUM_SCALAR
>::
get_
(
const
Instantiation
&
i
)
const
{
150
GUM_ERROR
(
OperationNotAllowed
,
151
"Do not use this with the MultiDimSparse class."
);
152
}
153
154
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669