aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
arcGraphPart_inl.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
/** @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
{
return
_arcs_
.
contains
(
arc
); }
41
42
INLINE
bool
ArcGraphPart
::
existsArc
(
NodeId
tail
,
NodeId
head
)
const
{
43
return
_parents_
.
exists
(
head
) &&
_parents_
[
head
]->
exists
(
tail
);
44
}
45
46
INLINE
void
ArcGraphPart
::
_checkParents_
(
NodeId
id
)
const
{
47
if
(!
_parents_
.
exists
(
id
)) {
_parents_
.
insert
(
id
,
new
NodeSet
); }
48
}
49
50
INLINE
void
ArcGraphPart
::
_checkChildren_
(
NodeId
id
)
const
{
51
if
(!
_children_
.
exists
(
id
)) {
_children_
.
insert
(
id
,
new
NodeSet
); }
52
}
53
54
INLINE
const
NodeSet
&
ArcGraphPart
::
parents
(
NodeId
id
)
const
{
55
_checkParents_
(
id
);
56
return
*(
_parents_
[
id
]);
57
}
58
59
INLINE
NodeSet
ArcGraphPart
::
family
(
NodeId
id
)
const
{
60
_checkParents_
(
id
);
61
NodeSet
res
{
id
};
62
return
res
+
parents
(
id
);
63
}
64
65
/// returns the set of children of a set of nodes
66
INLINE
NodeSet
ArcGraphPart
::
children
(
const
NodeSet
&
ids
)
const
{
67
NodeSet
res
;
68
for
(
const
auto
node
:
ids
)
69
res
+=
children
(
node
);
70
return
res
;
71
}
72
73
/// returns the set of parents of a set of nodes
74
INLINE
NodeSet
ArcGraphPart
::
parents
(
const
NodeSet
&
ids
)
const
{
75
NodeSet
res
;
76
for
(
const
auto
node
:
ids
)
77
res
+=
parents
(
node
);
78
return
res
;
79
}
80
81
/// returns the set of family nodes of a set of nodes
82
INLINE
NodeSet
ArcGraphPart
::
family
(
const
NodeSet
&
ids
)
const
{
83
NodeSet
res
;
84
for
(
const
auto
node
:
ids
)
85
res
+=
family
(
node
);
86
return
res
;
87
}
88
89
INLINE
const
NodeSet
&
ArcGraphPart
::
children
(
NodeId
id
)
const
{
90
_checkChildren_
(
id
);
91
return
*(
_children_
[
id
]);
92
}
93
94
INLINE
void
ArcGraphPart
::
addArc
(
NodeId
tail
,
NodeId
head
) {
95
Arc
arc
(
tail
,
head
);
96
97
_arcs_
.
insert
(
arc
);
98
_checkParents_
(
head
);
99
_checkChildren_
(
tail
);
100
_parents_
[
head
]->
insert
(
tail
);
101
_children_
[
tail
]->
insert
(
head
);
102
103
GUM_EMIT2
(
onArcAdded
,
tail
,
head
);
104
}
105
106
INLINE
void
ArcGraphPart
::
eraseArc
(
const
Arc
&
arc
) {
107
// ASSUMING tail and head exists in _parents_ anf _children_
108
// (if not, it is an error)
109
if
(
existsArc
(
arc
)) {
110
NodeId
tail
=
arc
.
tail
(),
head
=
arc
.
head
();
111
_parents_
[
head
]->
erase
(
tail
);
112
_children_
[
tail
]->
erase
(
head
);
113
_arcs_
.
erase
(
arc
);
114
GUM_EMIT2
(
onArcDeleted
,
tail
,
head
);
115
}
116
}
117
118
INLINE
void
ArcGraphPart
::
eraseSetOfArcs_
(
const
ArcSet
&
set
) {
119
for
(
const
auto
&
arc
:
set
)
120
eraseArc
(
arc
);
121
}
122
123
INLINE
void
ArcGraphPart
::
eraseParents
(
NodeId
id
) {
124
if
(
_parents_
.
exists
(
id
)) {
125
NodeSet
&
parents
= *(
_parents_
[
id
]);
126
127
for
(
auto
iter
=
parents
.
beginSafe
();
// safe iterator needed here
128
iter
!=
parents
.
endSafe
();
129
++
iter
) {
130
// warning: use this erase so that you actually use the virtualized
131
// arc removal function
132
eraseArc
(
Arc
(*
iter
,
id
));
133
}
134
}
135
}
136
137
INLINE
void
ArcGraphPart
::
eraseChildren
(
NodeId
id
) {
138
if
(
_children_
.
exists
(
id
)) {
139
NodeSet
&
children
= *(
_children_
[
id
]);
140
141
for
(
auto
iter
=
children
.
beginSafe
();
// safe iterator needed here
142
iter
!=
children
.
endSafe
();
143
++
iter
) {
144
// warning: use this erase so that you actually use the vritualized
145
// arc removal function
146
eraseArc
(
Arc
(
id
, *
iter
));
147
}
148
}
149
}
150
151
INLINE
void
ArcGraphPart
::
unvirtualizedEraseSetOfArcs_
(
const
ArcSet
&
set
) {
152
for
(
const
auto
&
arc
:
set
)
153
ArcGraphPart
::
eraseArc
(
arc
);
154
}
155
156
INLINE
void
ArcGraphPart
::
unvirtualizedEraseParents
(
NodeId
id
) {
157
if
(
_parents_
.
exists
(
id
)) {
158
NodeSet
&
parents
= *(
_parents_
[
id
]);
159
160
for
(
auto
iter
=
parents
.
beginSafe
();
// safe iterator needed here
161
iter
!=
parents
.
endSafe
();
162
++
iter
) {
163
ArcGraphPart
::
eraseArc
(
Arc
(*
iter
,
id
));
164
}
165
}
166
}
167
168
INLINE
void
ArcGraphPart
::
unvirtualizedEraseChildren
(
NodeId
id
) {
169
if
(
_children_
.
exists
(
id
)) {
170
NodeSet
&
children
= *(
_children_
[
id
]);
171
172
for
(
auto
iter
=
children
.
beginSafe
();
// safe iterator needed here
173
iter
!=
children
.
endSafe
();
174
++
iter
) {
175
ArcGraphPart
::
eraseArc
(
Arc
(
id
, *
iter
));
176
}
177
}
178
}
179
180
INLINE
bool
ArcGraphPart
::
operator
==(
const
ArcGraphPart
&
p
)
const
{
return
_arcs_
==
p
.
_arcs_
; }
181
182
INLINE
bool
ArcGraphPart
::
operator
!=(
const
ArcGraphPart
&
p
)
const
{
return
_arcs_
!=
p
.
_arcs_
; }
183
184
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643