aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
multiDimDecorator_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 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
=(
89
MultiDimDecorator
<
GUM_SCALAR
>&&
from
) {
90
MultiDimContainer
<
GUM_SCALAR
>::
operator
=(
91
std
::
forward
<
MultiDimContainer
<
GUM_SCALAR
> >(
from
));
92
GUM_OP_MOV
(
MultiDimDecorator
);
93
94
if
(
this
!= &
from
) {
95
if
(
content_
!=
nullptr
)
delete
(
content_
);
// should be the case
96
empty_value_
=
from
.
empty_value_
;
97
content_
=
from
.
content_
;
98
from
.
content_
=
nullptr
;
99
}
100
101
return
*
this
;
102
}
103
104
105
template
<
typename
GUM_SCALAR
>
106
INLINE
MultiDimDecorator
<
GUM_SCALAR
>::
MultiDimDecorator
(
107
MultiDimDecorator
<
GUM_SCALAR
>&&
from
)
noexcept
:
108
MultiDimContainer
<
GUM_SCALAR
>(
109
std
::
forward
<
MultiDimContainer
<
GUM_SCALAR
> >(
from
)) {
110
GUM_CONS_MOV
(
MultiDimDecorator
);
111
112
empty_value_
=
from
.
empty_value_
;
113
content_
=
from
.
content_
;
114
from
.
content_
=
nullptr
;
115
}
116
117
118
template
<
typename
GUM_SCALAR
>
119
INLINE
MultiDimDecorator
<
GUM_SCALAR
>&
120
MultiDimDecorator
<
GUM_SCALAR
>::
operator
=(
121
const
MultiDimDecorator
<
GUM_SCALAR
>&
from
)
noexcept
{
122
GUM_OP_CPY
(
MultiDimDecorator
);
123
initPotentialOperators___
<
GUM_SCALAR
>();
124
MultiDimContainer
<
GUM_SCALAR
>::
operator
=(
from
);
125
empty_value_
=
from
.
empty_value_
;
126
if
(
content_
==
nullptr
)
127
content_
=
static_cast
<
MultiDimImplementation
<
GUM_SCALAR
>* >(
128
from
.
content
()->
newFactory
());
129
MultiDimDecorator
<
GUM_SCALAR
>::
content
()->
copy
(*
from
.
content
());
130
return
*
this
;
131
}
132
133
134
// destructor
135
136
template
<
typename
GUM_SCALAR
>
137
INLINE
MultiDimDecorator
<
GUM_SCALAR
>::~
MultiDimDecorator
() {
138
if
(
content_
!=
nullptr
) {
delete
(
content_
); }
139
140
GUM_DESTRUCTOR
(
MultiDimDecorator
);
141
}
142
143
// return a data, given a Instantiation - final method
144
145
template
<
typename
GUM_SCALAR
>
146
INLINE
GUM_SCALAR
&
147
MultiDimDecorator
<
GUM_SCALAR
>::
get_
(
const
Instantiation
&
i
)
const
{
148
GUM_ERROR
(
OperationNotAllowed
,
"_get in the implementation !"
);
149
}
150
151
template
<
typename
GUM_SCALAR
>
152
INLINE
GUM_SCALAR
153
MultiDimDecorator
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
154
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
155
return
empty_value_
;
156
}
else
{
157
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
get
(
i
);
158
}
159
}
160
161
template
<
typename
GUM_SCALAR
>
162
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
set
(
const
Instantiation
&
i
,
163
const
GUM_SCALAR
&
value
)
const
{
164
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
nbrDim
() == 0) {
165
empty_value_
=
value
;
166
}
else
{
167
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
set
(
i
,
value
);
168
}
169
}
170
171
// get the size of domains - final method
172
173
template
<
typename
GUM_SCALAR
>
174
INLINE
Size
MultiDimDecorator
<
GUM_SCALAR
>::
domainSize
()
const
{
175
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
domainSize
();
176
}
177
178
// add a new var to the sequence of vars - final method
179
180
template
<
typename
GUM_SCALAR
>
181
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
add
(
const
DiscreteVariable
&
v
) {
182
if
(
v
.
domainSize
() < 1) {
183
GUM_ERROR
(
InvalidArgument
,
184
"Empty variable "
<<
v
<<
" cannot be added in a Potential"
);
185
}
186
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
add
(
v
);
187
}
188
189
// listen to change in each recorded Instantiation. final method
190
191
template
<
typename
GUM_SCALAR
>
192
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
changeNotification
(
193
const
Instantiation
&
i
,
194
const
DiscreteVariable
*
const
var
,
195
Idx
oldval
,
196
Idx
newval
) {
197
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
198
->
changeNotification
(
i
,
var
,
oldval
,
newval
);
199
}
200
201
// listen to an assignment of a value in a Instantiation
202
203
template
<
typename
GUM_SCALAR
>
204
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setChangeNotification
(
205
const
Instantiation
&
i
) {
206
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
207
->
setChangeNotification
(
i
);
208
}
209
210
// listen to setFirst in each recorded Instantiation. final method.
211
212
template
<
typename
GUM_SCALAR
>
213
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
setFirstNotification
(
214
const
Instantiation
&
i
) {
215
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
216
->
setFirstNotification
(
i
);
217
}
218
219
// listen to setLast in each recorded Instantiation. final method.
220
221
template
<
typename
GUM_SCALAR
>
222
INLINE
void
223
MultiDimDecorator
<
GUM_SCALAR
>::
setLastNotification
(
const
Instantiation
&
i
) {
224
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setLastNotification
(
225
i
);
226
}
227
228
// listen to increment in each recorded Instantiation. final method.
229
230
template
<
typename
GUM_SCALAR
>
231
INLINE
void
232
MultiDimDecorator
<
GUM_SCALAR
>::
setIncNotification
(
const
Instantiation
&
i
) {
233
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setIncNotification
(
234
i
);
235
}
236
237
// listen to increment in each recorded Instantiation. final method.
238
239
template
<
typename
GUM_SCALAR
>
240
INLINE
void
241
MultiDimDecorator
<
GUM_SCALAR
>::
setDecNotification
(
const
Instantiation
&
i
) {
242
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
setDecNotification
(
243
i
);
244
}
245
246
// add a Instantiation as a slave of this
247
248
template
<
typename
GUM_SCALAR
>
249
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
registerSlave
(
Instantiation
&
i
) {
250
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
251
->
registerSlave
(
i
);
252
}
253
254
template
<
typename
GUM_SCALAR
>
255
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
erase
(
const
DiscreteVariable
&
var
) {
256
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
erase
(
var
);
257
}
258
259
template
<
typename
GUM_SCALAR
>
260
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
erase
(
const
std
::
string
&
name
) {
261
erase
(
variable
(
name
));
262
}
263
264
template
<
typename
GUM_SCALAR
>
265
INLINE
const
DiscreteVariable
&
266
MultiDimDecorator
<
GUM_SCALAR
>::
variable
(
Idx
i
)
const
{
267
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
variable
(
i
);
268
}
269
270
template
<
typename
GUM_SCALAR
>
271
INLINE
const
DiscreteVariable
&
272
MultiDimDecorator
<
GUM_SCALAR
>::
variable
(
const
std
::
string
&
name
)
const
{
273
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
variable
(
274
name
);
275
}
276
277
template
<
typename
GUM_SCALAR
>
278
INLINE
Idx
279
MultiDimDecorator
<
GUM_SCALAR
>::
pos
(
const
DiscreteVariable
&
var
)
const
{
280
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
pos
(
var
);
281
}
282
283
template
<
typename
GUM_SCALAR
>
284
INLINE
bool
285
MultiDimDecorator
<
GUM_SCALAR
>::
contains
(
const
DiscreteVariable
&
var
)
const
{
286
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
contains
(
287
var
);
288
}
289
290
template
<
typename
GUM_SCALAR
>
291
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
empty
()
const
{
292
if
(
content_
==
nullptr
)
return
true
;
293
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
();
294
}
295
296
template
<
typename
GUM_SCALAR
>
297
INLINE
bool
MultiDimDecorator
<
GUM_SCALAR
>::
unregisterSlave
(
Instantiation
&
i
) {
298
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
299
->
unregisterSlave
(
i
);
300
}
301
302
template
<
typename
GUM_SCALAR
>
303
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
fill
(
const
GUM_SCALAR
&
d
)
const
{
304
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
305
empty_value_
=
d
;
306
}
else
{
307
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
fill
(
d
);
308
}
309
}
310
311
// notification modification on vars to all Instantiation listeners.
312
313
template
<
typename
GUM_SCALAR
>
314
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
notifyChange
()
const
{
315
/*( (MultiDimContainer<GUM_SCALAR> *) content_)->notifyChange();*/
316
GUM_ERROR
(
OperationNotAllowed
,
"Not implemented yet"
);
317
}
318
319
// give a const ref to the sequence of DiscreteVariable*. final method.
320
321
template
<
typename
GUM_SCALAR
>
322
INLINE
const
Sequence
<
const
DiscreteVariable
* >&
323
MultiDimDecorator
<
GUM_SCALAR
>::
variablesSequence
()
const
{
324
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
325
->
variablesSequence
();
326
}
327
328
// get the nbr of vars in the sequence. final method
329
330
template
<
typename
GUM_SCALAR
>
331
INLINE
Idx
MultiDimDecorator
<
GUM_SCALAR
>::
nbrDim
()
const
{
332
return
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
nbrDim
();
333
}
334
335
template
<
typename
GUM_SCALAR
>
336
void
MultiDimDecorator
<
GUM_SCALAR
>::
populate
(
337
const
std
::
vector
<
GUM_SCALAR
>&
v
)
const
{
338
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
339
if
(
v
.
size
() == 1) {
340
empty_value_
=
v
[0];
341
}
else
{
342
GUM_ERROR
(
SizeError
,
"Size do not match in populate"
)
343
}
344
}
else
{
345
content_
->
populate
(
v
);
346
}
347
}
348
349
template
<
typename
GUM_SCALAR
>
350
void
MultiDimDecorator
<
GUM_SCALAR
>::
apply
(
351
std
::
function
<
GUM_SCALAR
(
GUM_SCALAR
) >
f
)
const
{
352
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
353
empty_value_
=
f
(
empty_value_
);
354
}
else
{
355
content_
->
apply
(
f
);
356
}
357
}
358
359
template
<
typename
GUM_SCALAR
>
360
GUM_SCALAR
MultiDimDecorator
<
GUM_SCALAR
>::
reduce
(
361
std
::
function
<
GUM_SCALAR
(
GUM_SCALAR
,
GUM_SCALAR
) >
f
,
362
GUM_SCALAR
base
)
const
{
363
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
364
return
base
;
365
}
else
{
366
return
content_
->
reduce
(
f
,
base
);
367
}
368
}
369
370
// protected access to content_
371
template
<
typename
GUM_SCALAR
>
372
INLINE
MultiDimImplementation
<
GUM_SCALAR
>*
373
MultiDimDecorator
<
GUM_SCALAR
>::
content
() {
374
return
content_
;
375
}
376
377
// protected access to content_
378
template
<
typename
GUM_SCALAR
>
379
INLINE
const
MultiDimImplementation
<
GUM_SCALAR
>*
380
MultiDimDecorator
<
GUM_SCALAR
>::
content
()
const
{
381
return
content_
;
382
}
383
384
template
<
typename
GUM_SCALAR
>
385
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
beginMultipleChanges
() {
386
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
387
->
beginMultipleChanges
();
388
}
389
390
template
<
typename
GUM_SCALAR
>
391
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
endMultipleChanges
() {
392
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)
393
->
endMultipleChanges
();
394
}
395
396
template
<
typename
GUM_SCALAR
>
397
INLINE
void
398
MultiDimDecorator
<
GUM_SCALAR
>::
endMultipleChanges
(
const
GUM_SCALAR
&
x
) {
399
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
endMultipleChanges
(
400
x
);
401
}
402
403
template
<
typename
GUM_SCALAR
>
404
INLINE
void
MultiDimDecorator
<
GUM_SCALAR
>::
swapContent_
(
405
MultiDimImplementation
<
GUM_SCALAR
>*
aContent
)
const
{
406
if
(
aContent
!=
nullptr
) {
407
// TODO : frees all slave instantiations
408
// TODO : control the dimensions ?
409
MultiDimImplementation
<
GUM_SCALAR
>*
tmp
=
content_
;
410
content_
=
aContent
;
411
// registers all instantiations
412
delete
(
tmp
);
413
}
414
}
415
416
417
// string representation of internal data about i in this.
418
template
<
typename
GUM_SCALAR
>
419
INLINE
std
::
string
420
MultiDimDecorator
<
GUM_SCALAR
>::
toString
(
const
Instantiation
*
i
)
const
{
421
return
content_
->
toString
(
i
);
422
}
423
424
425
template
<
typename
GUM_SCALAR
>
426
INLINE
void
427
MultiDimDecorator
<
GUM_SCALAR
>::
replace_
(
const
DiscreteVariable
*
x
,
428
const
DiscreteVariable
*
y
) {
429
this
->
content
()->
replace
(*
x
, *
y
);
430
}
431
432
template
<
typename
GUM_SCALAR
>
433
INLINE
std
::
string
MultiDimDecorator
<
GUM_SCALAR
>::
toString
()
const
{
434
if
(
static_cast
<
MultiDimContainer
<
GUM_SCALAR
>* >(
content_
)->
empty
()) {
435
std
::
stringstream
ss
;
436
ss
<<
"<> :: "
<<
empty_value_
;
437
return
ss
.
str
();
438
}
else
{
439
return
content_
->
toString
();
440
}
441
}
442
443
//@todo force GUM_SCALAR to be double-castable (to be able to use fabs,etc.)
444
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::initPotentialOperators___
void initPotentialOperators___()
Definition:
multiDimDecorator_tpl.h:40