aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
arcGraphPart_inl.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
/** @file
23
* @brief Inline implementation of classes for directed edge sets
24
*
25
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26
*
27
*/
28
29
// to ease parsing by IDE
30
#
include
<
agrum
/
tools
/
graphs
/
parts
/
arcGraphPart
.
h
>
31
32
namespace
gum
{
33
34
INLINE
bool
ArcGraphPart
::
emptyArcs
()
const
{
return
arcs__
.
empty
(); }
35
36
INLINE Size
ArcGraphPart
::
sizeArcs
()
const
{
return
arcs__
.
size
(); }
37
38
INLINE
const
ArcSet
&
ArcGraphPart
::
arcs
()
const
{
return
arcs__
; }
39
40
INLINE
bool
ArcGraphPart
::
existsArc
(
const
Arc
&
arc
)
const
{
41
return
arcs__
.
contains
(
arc
);
42
}
43
44
INLINE
bool
ArcGraphPart
::
existsArc
(
NodeId
tail
,
NodeId
head
)
const
{
45
return
parents__
.
exists
(
head
) &&
parents__
[
head
]->
exists
(
tail
);
46
}
47
48
INLINE
void
ArcGraphPart
::
checkParents__
(
NodeId
id
)
const
{
49
if
(!
parents__
.
exists
(
id
)) {
parents__
.
insert
(
id
,
new
NodeSet
); }
50
}
51
52
INLINE
void
ArcGraphPart
::
checkChildren__
(
NodeId
id
)
const
{
53
if
(!
children__
.
exists
(
id
)) {
children__
.
insert
(
id
,
new
NodeSet
); }
54
}
55
56
INLINE
const
NodeSet
&
ArcGraphPart
::
parents
(
NodeId
id
)
const
{
57
checkParents__
(
id
);
58
return
*(
parents__
[
id
]);
59
}
60
61
INLINE
NodeSet
ArcGraphPart
::
family
(
NodeId
id
)
const
{
62
checkParents__
(
id
);
63
NodeSet
res
{
id
};
64
return
res
+
parents
(
id
);
65
}
66
67
/// returns the set of children of a set of nodes
68
INLINE
NodeSet
ArcGraphPart
::
children
(
const
NodeSet
&
ids
)
const
{
69
NodeSet
res
;
70
for
(
const
auto
node
:
ids
)
71
res
+=
children
(
node
);
72
return
res
;
73
}
74
75
/// returns the set of parents of a set of nodes
76
INLINE
NodeSet
ArcGraphPart
::
parents
(
const
NodeSet
&
ids
)
const
{
77
NodeSet
res
;
78
for
(
const
auto
node
:
ids
)
79
res
+=
parents
(
node
);
80
return
res
;
81
}
82
83
/// returns the set of family nodes of a set of nodes
84
INLINE
NodeSet
ArcGraphPart
::
family
(
const
NodeSet
&
ids
)
const
{
85
NodeSet
res
;
86
for
(
const
auto
node
:
ids
)
87
res
+=
family
(
node
);
88
return
res
;
89
}
90
91
INLINE
const
NodeSet
&
ArcGraphPart
::
children
(
NodeId
id
)
const
{
92
checkChildren__
(
id
);
93
return
*(
children__
[
id
]);
94
}
95
96
INLINE
void
ArcGraphPart
::
addArc
(
NodeId
tail
,
NodeId
head
) {
97
Arc
arc
(
tail
,
head
);
98
99
arcs__
.
insert
(
arc
);
100
checkParents__
(
head
);
101
checkChildren__
(
tail
);
102
parents__
[
head
]->
insert
(
tail
);
103
children__
[
tail
]->
insert
(
head
);
104
105
GUM_EMIT2
(
onArcAdded
,
tail
,
head
);
106
}
107
108
INLINE
void
ArcGraphPart
::
eraseArc
(
const
Arc
&
arc
) {
109
// ASSUMING tail and head exists in parents__ anf children__
110
// (if not, it is an error)
111
if
(
existsArc
(
arc
)) {
112
NodeId
tail
=
arc
.
tail
(),
head
=
arc
.
head
();
113
parents__
[
head
]->
erase
(
tail
);
114
children__
[
tail
]->
erase
(
head
);
115
arcs__
.
erase
(
arc
);
116
GUM_EMIT2
(
onArcDeleted
,
tail
,
head
);
117
}
118
}
119
120
INLINE
void
ArcGraphPart
::
eraseSetOfArcs_
(
const
ArcSet
&
set
) {
121
for
(
const
auto
&
arc
:
set
)
122
eraseArc
(
arc
);
123
}
124
125
INLINE
void
ArcGraphPart
::
eraseParents
(
NodeId
id
) {
126
if
(
parents__
.
exists
(
id
)) {
127
NodeSet
&
parents
= *(
parents__
[
id
]);
128
129
for
(
auto
iter
=
parents
.
beginSafe
();
// safe iterator needed here
130
iter
!=
parents
.
endSafe
();
131
++
iter
) {
132
// warning: use this erase so that you actually use the virtualized
133
// arc removal function
134
eraseArc
(
Arc
(*
iter
,
id
));
135
}
136
}
137
}
138
139
INLINE
void
ArcGraphPart
::
eraseChildren
(
NodeId
id
) {
140
if
(
children__
.
exists
(
id
)) {
141
NodeSet
&
children
= *(
children__
[
id
]);
142
143
for
(
auto
iter
=
children
.
beginSafe
();
// safe iterator needed here
144
iter
!=
children
.
endSafe
();
145
++
iter
) {
146
// warning: use this erase so that you actually use the vritualized
147
// arc removal function
148
eraseArc
(
Arc
(
id
, *
iter
));
149
}
150
}
151
}
152
153
INLINE
void
ArcGraphPart
::
unvirtualizedEraseSetOfArcs_
(
const
ArcSet
&
set
) {
154
for
(
const
auto
&
arc
:
set
)
155
ArcGraphPart
::
eraseArc
(
arc
);
156
}
157
158
INLINE
void
ArcGraphPart
::
unvirtualizedEraseParents
(
NodeId
id
) {
159
if
(
parents__
.
exists
(
id
)) {
160
NodeSet
&
parents
= *(
parents__
[
id
]);
161
162
for
(
auto
iter
=
parents
.
beginSafe
();
// safe iterator needed here
163
iter
!=
parents
.
endSafe
();
164
++
iter
) {
165
ArcGraphPart
::
eraseArc
(
Arc
(*
iter
,
id
));
166
}
167
}
168
}
169
170
INLINE
void
ArcGraphPart
::
unvirtualizedEraseChildren
(
NodeId
id
) {
171
if
(
children__
.
exists
(
id
)) {
172
NodeSet
&
children
= *(
children__
[
id
]);
173
174
for
(
auto
iter
=
children
.
beginSafe
();
// safe iterator needed here
175
iter
!=
children
.
endSafe
();
176
++
iter
) {
177
ArcGraphPart
::
eraseArc
(
Arc
(
id
, *
iter
));
178
}
179
}
180
}
181
182
INLINE
bool
ArcGraphPart
::
operator
==(
const
ArcGraphPart
&
p
)
const
{
183
return
arcs__
==
p
.
arcs__
;
184
}
185
186
INLINE
bool
ArcGraphPart
::
operator
!=(
const
ArcGraphPart
&
p
)
const
{
187
return
arcs__
!=
p
.
arcs__
;
188
}
189
190
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669