aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
arcGraphPart_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
49
50// to ease parsing by IDE
52
53namespace gum {
54
55 INLINE bool ArcGraphPart::emptyArcs() const { return _arcs_.empty(); }
56
57 INLINE Size ArcGraphPart::sizeArcs() const { return _arcs_.size(); }
58
59 INLINE const ArcSet& ArcGraphPart::arcs() const { return _arcs_; }
60
61 INLINE bool ArcGraphPart::existsArc(const Arc& arc) const { return _arcs_.contains(arc); }
62
63 INLINE bool ArcGraphPart::existsArc(NodeId tail, NodeId head) const {
64 return _parents_.exists(head) && _parents_[head]->exists(tail);
65 }
66
68 if (!_parents_.exists(id)) { _parents_.insert(id, new NodeSet); }
69 }
70
72 if (!_children_.exists(id)) { _children_.insert(id, new NodeSet); }
73 }
74
75 INLINE const NodeSet& ArcGraphPart::parents(NodeId id) const {
76 if (_parents_.exists(id)) return *(_parents_[id]);
77 else return emptyNodeSet;
78 }
79
81 NodeSet res{id};
82 return res + parents(id);
83 }
84
86 INLINE NodeSet ArcGraphPart::children(const NodeSet& ids) const {
87 NodeSet res;
88 for (const auto node: ids)
89 res += children(node);
90 return res;
91 }
92
94 INLINE NodeSet ArcGraphPart::parents(const NodeSet& ids) const {
95 NodeSet res;
96 for (const auto node: ids)
97 res += parents(node);
98 return res;
99 }
100
102 INLINE NodeSet ArcGraphPart::family(const NodeSet& ids) const {
103 NodeSet res;
104 for (const auto node: ids)
105 res += family(node);
106 return res;
107 }
108
109 INLINE const NodeSet& ArcGraphPart::children(NodeId id) const {
110 if (_children_.exists(id)) return *_children_[id];
111 else return emptyNodeSet;
112 }
113
114 INLINE void ArcGraphPart::addArc(NodeId tail, NodeId head) {
115 Arc arc(tail, head);
116
117 _arcs_.insert(arc);
118 _checkParents_(head);
119 _checkChildren_(tail);
120 _parents_[head]->insert(tail);
121 _children_[tail]->insert(head);
122
123 GUM_EMIT2(onArcAdded, tail, head);
124 }
125
126 INLINE void ArcGraphPart::eraseArc(const Arc& arc) {
127 // ASSUMING tail and head exists in _parents_ anf _children_
128 // (if not, it is an error)
129 if (existsArc(arc)) {
130 NodeId tail = arc.tail();
131 NodeId head = arc.head();
132 _parents_[head]->erase(tail);
133 _children_[tail]->erase(head);
134 _arcs_.erase(arc);
135 GUM_EMIT2(onArcDeleted, tail, head);
136 }
137 }
138
139 INLINE void ArcGraphPart::eraseSetOfArcs_(const ArcSet& set) {
140 for (const auto& arc: set)
141 eraseArc(arc);
142 }
143
145 if (_parents_.exists(id)) {
146 const NodeSet& parents = *(_parents_[id]);
147
148 for (auto iter = parents.beginSafe(); // safe iterator needed here
149 iter != parents.endSafe();
150 ++iter) {
151 // warning: use this erase so that you actually use the virtualized
152 // arc removal function
153 eraseArc(Arc(*iter, id));
154 }
155 }
156 }
157
159 if (_children_.exists(id)) {
160 const NodeSet& children = *(_children_[id]);
161
162 for (auto iter = children.beginSafe(); // safe iterator needed here
163 iter != children.endSafe();
164 ++iter) {
165 // warning: use this erase so that you actually use the virtualized
166 // arc removal function
167 eraseArc(Arc(id, *iter));
168 }
169 }
170 }
171
173 for (const auto& arc: set)
175 }
176
178 if (_parents_.exists(id)) {
179 const NodeSet& parents = *(_parents_[id]);
180
181 for (auto iter = parents.beginSafe(); // safe iterator needed here
182 iter != parents.endSafe();
183 ++iter) {
184 ArcGraphPart::eraseArc(Arc(*iter, id));
185 }
186 }
187 }
188
190 if (_children_.exists(id)) {
191 const NodeSet& children = *(_children_[id]);
192
193 for (auto iter = children.beginSafe(); // safe iterator needed here
194 iter != children.endSafe();
195 ++iter) {
196 ArcGraphPart::eraseArc(Arc(id, *iter));
197 }
198 }
199 }
200
201 INLINE bool ArcGraphPart::operator==(const ArcGraphPart& p) const { return _arcs_ == p._arcs_; }
202
203} /* namespace gum */
bool emptyArcs() const
indicates wether the ArcGraphPart contains any arc
virtual void addArc(NodeId tail, NodeId head)
insert a new arc into the ArcGraphPart
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
NodeSet family(NodeId id) const
returns the set of nodes which consists in the node and its parents
bool operator==(const ArcGraphPart &p) const
tests whether two ArcGraphParts contain the same arcs
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
void _checkParents_(NodeId id)
when the ArcGraphPart contains no arc ingoing into a given node, this function adds an empty set entr...
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
Signaler2< NodeId, NodeId > onArcAdded
Set< Arc > _arcs_
the set of all the arcs contained within the ArcGraphPart
void _checkChildren_(NodeId id)
when the ArcGraphPart contains no arc outgoing from a given node, this function adds an empty set ent...
void eraseSetOfArcs_(const ArcSet &set)
a (virtualized) function to remove a given set of arcs
void unvirtualizedEraseChildren(NodeId id)
same function as eraseChildren but without any virtual call to an erase
NodeProperty< NodeSet * > _children_
for each arc, the set of its children
void unvirtualizedEraseParents(NodeId id)
same function as eraseParents but without any virtual call to an erase
void eraseParents(NodeId id)
erase all the parents of a given node
void eraseChildren(NodeId id)
removes all the children of a given node
void unvirtualizedEraseSetOfArcs_(const ArcSet &set)
similar to eraseSetOfArcs_ except that it is unvirtualized
NodeProperty< NodeSet * > _parents_
for each arc, the sets of its parents
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
ArcGraphPart(Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
Signaler2< NodeId, NodeId > onArcDeleted
The base class for all directed edges.
GUM_NODISCARD NodeId head() const
returns the head of the arc
GUM_NODISCARD NodeId tail() const
returns the tail of the arc
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< Arc > ArcSet
Some typdefs and define for shortcuts ...
const NodeSet emptyNodeSet
Some typdefs and define for shortcuts ...
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
#define GUM_EMIT2(signal, arg1, arg2)
Definition signaler2.h:61