aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
multiDimSparse_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by 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
(
const
MultiDimSparse
<
GUM_SCALAR
>&
from
) :
44
MultiDimWithOffset
<
GUM_SCALAR
>(
from
),
params_
(
from
.
params_
),
default_
(
from
.
default_
) {
45
// for debugging purposes
46
GUM_CONS_CPY
(
MultiDimSparse
);
47
}
48
49
// destructor
50
51
template
<
typename
GUM_SCALAR
>
52
MultiDimSparse
<
GUM_SCALAR
>::~
MultiDimSparse
() {
53
// for debugging purposes
54
GUM_DESTRUCTOR
(
MultiDimSparse
);
55
// no need to unregister all slaves as it will be done by MultiDimWithOffset
56
}
57
58
// data access operator
59
60
template
<
typename
GUM_SCALAR
>
61
INLINE
GUM_SCALAR
MultiDimSparse
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
62
Size
key
;
63
64
if
(
i
.
isMaster
(
this
)) {
65
key
=
this
->
offsets_
[&
i
];
66
}
else
{
67
key
=
this
->
getOffs_
(
i
);
68
}
69
70
return
params_
.
exists
(
key
) ?
params_
[
key
] :
default_
;
71
}
72
73
template
<
typename
GUM_SCALAR
>
74
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
set
(
const
Instantiation
&
i
,
75
const
GUM_SCALAR
&
value
)
const
{
76
Size
key
;
77
78
if
(
i
.
isMaster
(
this
)) {
79
key
=
this
->
offsets_
[&
i
];
80
}
else
{
81
key
=
this
->
getOffs_
(
i
);
82
}
83
84
if
(
value
==
default_
) {
85
params_
.
reset
(
key
);
86
}
else
{
87
params_
.
set
(
key
,
value
);
88
}
89
}
90
91
// add a new dimension, needed for updating the offsets_ & gaps_
92
93
template
<
typename
GUM_SCALAR
>
94
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
add
(
const
DiscreteVariable
&
v
) {
95
MultiDimWithOffset
<
GUM_SCALAR
>::
add
(
v
);
96
fill
(
default_
);
97
}
98
99
// removes a dimension, needed for updating the offsets_ & gaps_
100
101
template
<
typename
GUM_SCALAR
>
102
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
erase
(
const
DiscreteVariable
&
v
) {
103
MultiDimWithOffset
<
GUM_SCALAR
>::
erase
(
v
);
104
fill
(
default_
);
105
}
106
107
// synchronise content after MultipleChanges
108
template
<
typename
GUM_SCALAR
>
109
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
commitMultipleChanges_
() {
110
fill
(
default_
);
111
}
112
113
// fill the array with the arg
114
template
<
typename
GUM_SCALAR
>
115
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
fill
(
const
GUM_SCALAR
&
d
)
const
{
116
params_
.
clear
();
117
default_
=
d
;
118
}
119
120
template
<
typename
GUM_SCALAR
>
121
INLINE
Size
MultiDimSparse
<
GUM_SCALAR
>::
realSize
()
const
{
122
return
params_
.
size
();
123
}
124
125
template
<
typename
GUM_SCALAR
>
126
INLINE
MultiDimContainer
<
GUM_SCALAR
>*
MultiDimSparse
<
GUM_SCALAR
>::
newFactory
()
const
{
127
return
new
MultiDimSparse
<
GUM_SCALAR
>(
default_
);
128
}
129
130
// returns the name of the implementation
131
template
<
typename
GUM_SCALAR
>
132
INLINE
const
std
::
string
&
MultiDimSparse
<
GUM_SCALAR
>::
name
()
const
{
133
static
const
std
::
string
str
=
"MultiDimSparse"
;
134
return
str
;
135
}
136
137
template
<
typename
GUM_SCALAR
>
138
INLINE
void
MultiDimSparse
<
GUM_SCALAR
>::
replace_
(
const
DiscreteVariable
*
x
,
139
const
DiscreteVariable
*
y
) {
140
MultiDimImplementation
<
GUM_SCALAR
>::
replace_
(
x
,
y
);
141
}
142
143
template
<
typename
GUM_SCALAR
>
144
INLINE
GUM_SCALAR
&
MultiDimSparse
<
GUM_SCALAR
>::
get_
(
const
Instantiation
&
i
)
const
{
145
GUM_ERROR
(
OperationNotAllowed
,
"Do not use this with the MultiDimSparse class."
)
146
}
147
148
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643