aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
multiDimDecorator_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 for MultiDimDecorator.
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27
*/
28
29
// include the operators that will be used by the decorators
30
#
include
<
agrum
/
tools
/
multidim
/
utils
/
operators
/
completeProjections4MultiDim
.
h
>
31
#
include
<
agrum
/
tools
/
multidim
/
utils
/
operators
/
operators4MultiDim
.
h
>
32
#
include
<
agrum
/
tools
/
multidim
/
utils
/
operators
/
projections4MultiDim
.
h
>
33
#
include
<
agrum
/
tools
/
multidim
/
utils
/
partialInstantiation4MultiDim
.
h
>
34
35
#
include
<
agrum
/
tools
/
multidim
/
implementations
/
multiDimDecorator
.
h
>
36
37
namespace
gum
{
38
// instrumental and non-API function
39
template
<
typename
GUM_SCALAR >
40
void
_initPotentialOperators__
() {
41
static
bool
first =
true
;
42
43
if
(first) {
44
first =
false
;
45
46
// register the operators that will be used by the decorator
47
Operators4MultiDimInitialize< GUM_SCALAR > op;
48
op.init();
49
50
// register the projectors that will be used by the decorator
51
Projections4MultiDimInitialize< GUM_SCALAR > proj;
52
proj.init();
53
54
// register the projectors that will be used by the decorator
55
CompleteProjections4MultiDimInitialize< GUM_SCALAR > comp_proj;
56
comp_proj.init();
57
58
// register the partial instantiators that will be used by the decorator
59
PartialInstantiation4MultiDimInitialize< GUM_SCALAR > inst;
60
inst.init();
61
}
62
}
63
64
// constructors
65
template
<
typename
GUM_SCALAR >
66
INLINE MultiDimDecorator<
GUM_SCALAR
>::
MultiDimDecorator
(
67
MultiDimImplementation
<
GUM_SCALAR
>*
aContent
,
68
GUM_SCALAR
empty_value
) :
69
content_
(
aContent
),
70
empty_value_
(
empty_value
) {
71
_initPotentialOperators__
<
GUM_SCALAR
>();
72
GUM_CONSTRUCTOR
(
MultiDimDecorator
);
73
}
74
75
template
<
typename
GUM_SCALAR
>
76
INLINE
MultiDimDecorator
<
GUM_SCALAR
>::
MultiDimDecorator
(
77
const
MultiDimDecorator
<
GUM_SCALAR
>&
from
) :
78
MultiDimContainer
<
GUM_SCALAR
>(
from
) {
79
GUM_CONS_CPY
(
MultiDimDecorator
);
80
_initPotentialOperators__
<
GUM_SCALAR
>();
81
empty_value_
=
from
.
empty_value_
;
82
content
()->
copy
(
from
.
content
());
83
}
84
85
86
template
<
typename
GUM_SCALAR
>
87
INLINE
MultiDimDecorator
<
GUM_SCALAR
>&
88
MultiDimDecorator
<
GUM_SCALAR
>::
operator
=(
MultiDimDecorator
<
GUM_SCALAR
>&&
from
) {
89
MultiDimContainer
<
GUM_SCALAR
>::
operator
=(
90
std
::
forward
<
MultiDimContainer
<
GUM_SCALAR
> >(
from
));
91
GUM_OP_MOV
(
MultiDimDecorator
);
92
93
if
(
this
!= &
from
) {
94
if
(
content_
!=
nullptr
)
delete
(
content_
);
// should be the case
95
empty_value_
=
from
.
empty_value_
;
96
content_
=
from
.
content_
;
97
from
.
content_
=
nullptr
;
98
}
99
100
return
*
this
;
101
}
102
103
104
template
<
typename
GUM_SCALAR
>
105
INLINE
MultiDimDecorator
<
GUM_SCALAR
>::
MultiDimDecorator
(
106
MultiDimDecorator
<
GUM_SCALAR
>&&
from
)
noexcept
:
107
MultiDimContainer
<
GUM_SCALAR
>(
std
::
forward
<
MultiDimContainer
<
GUM_SCALAR
> >(
from
)) {
108
GUM_CONS_MOV
(
MultiDimDecorator
);
109
110
empty_value_
=
from
.
empty_value_
;
111
content_
=
from
.
content_
;
112
from
.
content_
=
nullptr
;
113
}
114
115
116
template
<
typename
GUM_SCALAR
>
117
INLINE
MultiDimDecorator
<
GUM_SCALAR
>&
MultiDimDecorator
<
GUM_SCALAR
>::
operator
=(
118
const
MultiDimDecorator
<
GUM_SCALAR
>&
from
)
noexcept
{
119
GUM_OP_CPY
(
MultiDimDecorator
);
120
_initPotentialOperators__
<
GUM_SCALAR
>();
121
MultiDimContainer
<
GUM_SCALAR
>::
operator
=(
from
);
122
empty_value_
=
from
.
empty_value_
;
123
if
(
content_
==
nullptr
)
124
content_
=
static_cast
<
MultiDimImplementation
<
GUM_SCALAR
>* >(
from
.
content
()->
newFactory
());
125
MultiDimDecorator
<
GUM_SCALAR
>::
content
()->
copy
(*
from
.
content
());
126
return
*
this
;
127
}
128
129
130
// destructor
131
132
template
<
typename
GUM_SCALAR
>
133
INLINE
MultiDimDecorator
<
GUM_SCALAR
>::~
MultiDimDecorator
() {
134
if
(
content_
!=
nullptr
) {
delete
(
content_
); }
135
136
GUM_DESTRUCTOR
(
MultiDimDecorator
);
137
}
138
139
// return a data, given a Instantiation - final method
140
141
template
<
typename
GUM_SCALAR
>
142
INLINE
GUM_SCALAR
&
MultiDimDecorator
<
GUM_SCALAR
>::
get_
(
const
Instantiation
&
i
)
const
{
143
GUM_ERROR
(
OperationNotAllowed
,
"_get in the implementation !"
)
144
}
145
146
template
<
typename
GUM_SCALAR
>
147
INLINE
GUM_SCALAR
MultiDimDecorator
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
148
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
149
return
empty_value_
;
150
}
else
{
151
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
get
(
i
);
152
}
153
}
154
155
template
<
typename
GUM_SCALAR
>
156
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
set
(
const
Instantiation
&
i
,
157
const
GUM_SCALAR
&
value
)
const
{
158
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
nbrDim
() == 0) {
159
empty_value_
=
value
;
160
}
else
{
161
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
set
(
i
,
value
);
162
}
163
}
164
165
// get the size of domains - final method
166
167
template
<
typename
GUM_SCALAR
>
168
INLINE
Size
MultiDimDecorator
<
GUM_SCALAR
>::
domainSize
()
const
{
169
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
domainSize
();
170
}
171
172
// add a new var to the sequence of vars - final method
173
174
template
<
typename
GUM_SCALAR
>
175
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
add
(
const
DiscreteVariable
&
v
) {
176
if
(
v
.
domainSize
() < 1) {
177
GUM_ERROR
(
InvalidArgument
,
"Empty variable "
<<
v
<<
" cannot be added in a Potential"
)
178
}
179
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
add
(
v
);
180
}
181
182
// listen to change in each recorded Instantiation. final method
183
184
template
<
typename
GUM_SCALAR
>
185
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
changeNotification
(
const
Instantiation
&
i
,
186
const
DiscreteVariable
*
const
var
,
187
Idx
oldval
,
188
Idx
newval
) {
189
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
changeNotification
(
i
,
190
var
,
191
oldval
,
192
newval
);
193
}
194
195
// listen to an assignment of a value in a Instantiation
196
197
template
<
typename
GUM_SCALAR
>
198
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setChangeNotification
(
const
Instantiation
&
i
) {
199
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setChangeNotification
(
i
);
200
}
201
202
// listen to setFirst in each recorded Instantiation. final method.
203
204
template
<
typename
GUM_SCALAR
>
205
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setFirstNotification
(
const
Instantiation
&
i
) {
206
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setFirstNotification
(
i
);
207
}
208
209
// listen to setLast in each recorded Instantiation. final method.
210
211
template
<
typename
GUM_SCALAR
>
212
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setLastNotification
(
const
Instantiation
&
i
) {
213
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setLastNotification
(
i
);
214
}
215
216
// listen to increment in each recorded Instantiation. final method.
217
218
template
<
typename
GUM_SCALAR
>
219
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setIncNotification
(
const
Instantiation
&
i
) {
220
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setIncNotification
(
i
);
221
}
222
223
// listen to increment in each recorded Instantiation. final method.
224
225
template
<
typename
GUM_SCALAR
>
226
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setDecNotification
(
const
Instantiation
&
i
) {
227
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setDecNotification
(
i
);
228
}
229
230
// add a Instantiation as a slave of this
231
232
template
<
typename
GUM_SCALAR
>
233
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
registerSlave
(
Instantiation
&
i
) {
234
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
registerSlave
(
i
);
235
}
236
237
template
<
typename
GUM_SCALAR
>
238
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
erase
(
const
DiscreteVariable
&
var
) {
239
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
erase
(
var
);
240
}
241
242
template
<
typename
GUM_SCALAR
>
243
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
erase
(
const
std
::
string
&
name
) {
244
erase
(
variable
(
name
));
245
}
246
247
template
<
typename
GUM_SCALAR
>
248
INLINE
const
DiscreteVariable
&
MultiDimDecorator
<
GUM_SCALAR
>::
variable
(
Idx
i
)
const
{
249
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
variable
(
i
);
250
}
251
252
template
<
typename
GUM_SCALAR
>
253
INLINE
const
DiscreteVariable
&
254
MultiDimDecorator
<
GUM_SCALAR
>::
variable
(
const
std
::
string
&
name
)
const
{
255
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
variable
(
name
);
256
}
257
258
template
<
typename
GUM_SCALAR
>
259
INLINE
Idx
MultiDimDecorator
<
GUM_SCALAR
>::
pos
(
const
DiscreteVariable
&
var
)
const
{
260
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
pos
(
var
);
261
}
262
263
template
<
typename
GUM_SCALAR
>
264
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
contains
(
const
DiscreteVariable
&
var
)
const
{
265
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
contains
(
var
);
266
}
267
268
template
<
typename
GUM_SCALAR
>
269
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
empty
()
const
{
270
if
(
content_
==
nullptr
)
return
true
;
271
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
();
272
}
273
274
template
<
typename
GUM_SCALAR
>
275
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
unregisterSlave
(
Instantiation
&
i
) {
276
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
unregisterSlave
(
i
);
277
}
278
279
template
<
typename
GUM_SCALAR
>
280
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
fill
(
const
GUM_SCALAR
&
d
)
const
{
281
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
282
empty_value_
=
d
;
283
}
else
{
284
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
fill
(
d
);
285
}
286
}
287
288
// notification modification on vars to all Instantiation listeners.
289
290
template
<
typename
GUM_SCALAR
>
291
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
notifyChange
()
const
{
292
/*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
293
GUM_ERROR
(
OperationNotAllowed
,
"Not implemented yet"
)
294
}
295
296
// give a const ref to the sequence of DiscreteVariable*. final method.
297
298
template
<
typename
GUM_SCALAR
>
299
INLINE
const
Sequence
<
const
DiscreteVariable
* >&
300
MultiDimDecorator
<
GUM_SCALAR
>::
variablesSequence
()
const
{
301
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
variablesSequence
();
302
}
303
304
// get the nbr of vars in the sequence. final method
305
306
template
<
typename
GUM_SCALAR
>
307
INLINE
Idx
MultiDimDecorator
<
GUM_SCALAR
>::
nbrDim
()
const
{
308
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
nbrDim
();
309
}
310
311
template
<
typename
GUM_SCALAR
>
312
void
MultiDimDecorator
<
GUM_SCALAR
>::
populate
(
const
std
::
vector
<
GUM_SCALAR
>&
v
)
const
{
313
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
314
if
(
v
.
size
() == 1) {
315
empty_value_
=
v
[0];
316
}
else
{
317
GUM_ERROR
(
SizeError
,
"Size do not match in populate"
)
318
}
319
}
else
{
320
content_
->
populate
(
v
);
321
}
322
}
323
324
template
<
typename
GUM_SCALAR
>
325
void
MultiDimDecorator
<
GUM_SCALAR
>::
apply
(
std
::
function
<
GUM_SCALAR
(
GUM_SCALAR
) >
f
)
const
{
326
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
327
empty_value_
=
f
(
empty_value_
);
328
}
else
{
329
content_
->
apply
(
f
);
330
}
331
}
332
333
template
<
typename
GUM_SCALAR
>
334
GUM_SCALAR
335
MultiDimDecorator
<
GUM_SCALAR
>::
reduce
(
std
::
function
<
GUM_SCALAR
(
GUM_SCALAR
,
GUM_SCALAR
) >
f
,
336
GUM_SCALAR
base
)
const
{
337
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
338
return
base
;
339
}
else
{
340
return
content_
->
reduce
(
f
,
base
);
341
}
342
}
343
344
// protected access to content_
345
template
<
typename
GUM_SCALAR
>
346
INLINE
MultiDimImplementation
<
GUM_SCALAR
>*
MultiDimDecorator
<
GUM_SCALAR
>::
content
() {
347
return
content_
;
348
}
349
350
// protected access to content_
351
template
<
typename
GUM_SCALAR
>
352
INLINE
const
MultiDimImplementation
<
GUM_SCALAR
>*
353
MultiDimDecorator
<
GUM_SCALAR
>::
content
()
const
{
354
return
content_
;
355
}
356
357
template
<
typename
GUM_SCALAR
>
358
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
beginMultipleChanges
() {
359
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
beginMultipleChanges
();
360
}
361
362
template
<
typename
GUM_SCALAR
>
363
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
endMultipleChanges
() {
364
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
endMultipleChanges
();
365
}
366
367
template
<
typename
GUM_SCALAR
>
368
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
endMultipleChanges
(
const
GUM_SCALAR
&
x
) {
369
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
endMultipleChanges
(
x
);
370
}
371
372
template
<
typename
GUM_SCALAR
>
373
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
swapContent_
(
374
MultiDimImplementation
<
GUM_SCALAR
>*
aContent
)
const
{
375
if
(
aContent
!=
nullptr
) {
376
// TODO : frees all slave instantiations
377
// TODO : control the dimensions ?
378
MultiDimImplementation
<
GUM_SCALAR
>*
tmp
=
content_
;
379
content_
=
aContent
;
380
// registers all instantiations
381
delete
(
tmp
);
382
}
383
}
384
385
386
// string representation of internal data about i in this.
387
template
<
typename
GUM_SCALAR
>
388
INLINE
std
::
string
MultiDimDecorator
<
GUM_SCALAR
>::
toString
(
const
Instantiation
*
i
)
const
{
389
return
content_
->
toString
(
i
);
390
}
391
392
393
template
<
typename
GUM_SCALAR
>
394
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
replace_
(
const
DiscreteVariable
*
x
,
395
const
DiscreteVariable
*
y
) {
396
this
->
content
()->
replace
(*
x
, *
y
);
397
}
398
399
template
<
typename
GUM_SCALAR
>
400
INLINE
std
::
string
MultiDimDecorator
<
GUM_SCALAR
>::
toString
()
const
{
401
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
402
std
::
stringstream
ss
;
403
ss
<<
"<> :: "
<<
empty_value_
;
404
return
ss
.
str
();
405
}
else
{
406
return
content_
->
toString
();
407
}
408
}
409
410
//@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
411
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
gum::_initPotentialOperators__
void _initPotentialOperators__()
Definition:
multiDimDecorator_tpl.h:40