aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
fusionContext_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 Templates for fusion context class
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
*/
29
// =======================================================
30
#
include
<
agrum
/
tools
/
core
/
math
/
math_utils
.
h
>
31
#
include
<
agrum
/
FMDP
/
learning
/
datastructure
/
leaves
/
fusionContext
.
h
>
32
// =======================================================
33
34
namespace
gum
{
35
36
// ############################################################################
37
// Constructors / Destructors
38
// ############################################################################
39
40
// ============================================================================
41
// Default constructor.
42
// ============================================================================
43
template
<
bool
isInitial >
44
FusionContext< isInitial >::FusionContext(AbstractLeaf* leaf) : leaf__(leaf) {
45
GUM_CONSTRUCTOR(FusionContext);
46
}
47
48
// ============================================================================
49
// Default constructor.
50
// ============================================================================
51
template
<
bool
isInitial
>
52
FusionContext
<
isInitial
>::~
FusionContext
() {
53
GUM_DESTRUCTOR
(
FusionContext
);
54
55
for
(
auto
leafIter
=
leaf2Pair__
.
beginSafe
();
56
leafIter
!=
leaf2Pair__
.
endSafe
();
57
++
leafIter
)
58
delete
leafIter
.
val
();
59
60
delete
leaf__
;
61
}
62
63
// ############################################################################
64
//
65
// ############################################################################
66
67
// ============================================================================
68
//
69
// ============================================================================
70
template
<
bool
isInitial
>
71
bool
FusionContext
<
isInitial
>::
associateLeaf__
(
AbstractLeaf
*
l
,
72
Int2Type
<
false
>) {
73
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
74
;
75
LeafPair
*
p
=
new
LeafPair
(
l
,
leaf__
);
76
leaf2Pair__
.
insert
(
l
,
p
);
77
leaf2Pair__
[
l
]->
updateLikelyhood
();
78
pairsHeap__
.
insert
(
p
,
p
->
likelyhood
());
79
80
return
ptop
!=
pairsHeap__
.
top
();
81
}
82
83
// ============================================================================
84
//
85
// ============================================================================
86
template
<
bool
isInitial
>
87
bool
FusionContext
<
isInitial
>::
updateAssociatedLeaf__
(
AbstractLeaf
*
l
,
88
Int2Type
<
false
>) {
89
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
90
;
91
leaf2Pair__
[
l
]->
updateLikelyhood
();
92
pairsHeap__
.
setPriority
(
leaf2Pair__
[
l
],
leaf2Pair__
[
l
]->
likelyhood
());
93
94
return
ptop
!=
pairsHeap__
.
top
();
95
}
96
97
// ============================================================================
98
//
99
// ============================================================================
100
template
<
bool
isInitial
>
101
bool
FusionContext
<
isInitial
>::
updateAllAssociatedLeaves__
(
Int2Type
<
false
>) {
102
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
103
;
104
for
(
HashTableConstIteratorSafe
<
AbstractLeaf
*,
LeafPair
* >
pairIter
105
=
leaf2Pair__
.
cbeginSafe
();
106
pairIter
!=
leaf2Pair__
.
cendSafe
();
107
++
pairIter
) {
108
pairIter
.
val
()->
updateLikelyhood
();
109
pairsHeap__
.
setPriority
(
pairIter
.
val
(),
pairIter
.
val
()->
likelyhood
());
110
}
111
LeafPair
*
ctop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
112
113
return
ptop
!=
ctop
;
114
}
115
116
// ============================================================================
117
//
118
// ============================================================================
119
template
<
bool
isInitial
>
120
bool
FusionContext
<
isInitial
>::
deassociateLeaf__
(
AbstractLeaf
*
l
,
121
Int2Type
<
false
>) {
122
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
123
pairsHeap__
.
erase
(
leaf2Pair__
[
l
]);
124
leaf2Pair__
.
erase
(
l
);
125
126
LeafPair
*
ctop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
127
128
return
ptop
!=
ctop
;
129
}
130
131
132
// ############################################################################
133
//
134
// ############################################################################
135
136
// ============================================================================
137
//
138
// ============================================================================
139
template
<
bool
isInitial
>
140
bool
FusionContext
<
isInitial
>::
addPair
(
LeafPair
*
p
) {
141
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
142
pairsHeap__
.
insert
(
p
,
p
->
likelyhood
());
143
144
return
ptop
!=
pairsHeap__
.
top
();
145
}
146
147
// ============================================================================
148
//
149
// ============================================================================
150
template
<
bool
isInitial
>
151
bool
FusionContext
<
isInitial
>::
updatePair
(
LeafPair
*
p
) {
152
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
153
pairsHeap__
.
setPriority
(
p
,
p
->
likelyhood
());
154
155
return
ptop
!=
pairsHeap__
.
top
();
156
}
157
158
// ============================================================================
159
//
160
// ============================================================================
161
template
<
bool
isInitial
>
162
bool
FusionContext
<
isInitial
>::
removePair
(
LeafPair
*
p
) {
163
LeafPair
*
ptop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
164
pairsHeap__
.
erase
(
p
);
165
166
LeafPair
*
ctop
=
pairsHeap__
.
empty
() ?
nullptr
:
pairsHeap__
.
top
();
167
168
return
ptop
!=
ctop
;
169
}
170
171
172
// ############################################################################
173
//
174
// ############################################################################
175
176
// ============================================================================
177
//
178
// ============================================================================
179
template
<
bool
isInitial
>
180
Set
<
LeafPair
* >
181
FusionContext
<
isInitial
>::
associatedPairs__
(
Int2Type
<
false
>) {
182
Set
<
LeafPair
* >
retBag
;
183
for
(
auto
pairIter
=
leaf2Pair__
.
beginSafe
();
184
pairIter
!=
leaf2Pair__
.
endSafe
();
185
++
pairIter
)
186
retBag
<<
pairIter
.
val
();
187
188
return
retBag
;
189
}
190
191
192
template
<
bool
isInitial
>
193
std
::
string
FusionContext
<
isInitial
>::
toString
() {
194
std
::
stringstream
ss
;
195
if
(
leaf__
)
196
ss
<<
"Associated Leaf : "
<<
leaf__
->
toString
() <<
std
::
endl
197
<<
"Leaves Heap : "
<<
std
::
endl
;
198
199
// for( HashTableConstIteratorSafe<LeafPair*, std::vector<Size>>
200
// leafIter = pairsHeap__.allValues().cbeginSafe();
201
// leafIter != pairsHeap__.allValues().cendSafe(); ++leafIter ){
202
// ss << leafIter.key()->toString() << std::endl;
203
// }
204
if
(!
pairsHeap__
.
empty
())
205
ss
<<
"Top pair : "
<<
pairsHeap__
.
top
()->
toString
() <<
std
::
endl
;
206
207
return
ss
.
str
();
208
}
209
210
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669