aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
50
51// to ease parsing by IDE
53
54namespace gum {
55
56 INLINE bool ArcGraphPart::emptyArcs() const { return _arcs_.empty(); }
57
58 INLINE Size ArcGraphPart::sizeArcs() const { return _arcs_.size(); }
59
60 INLINE const ArcSet& ArcGraphPart::arcs() const { return _arcs_; }
61
62 INLINE bool ArcGraphPart::existsArc(const Arc& arc) const { return _arcs_.contains(arc); }
63
64 INLINE bool ArcGraphPart::existsArc(NodeId tail, NodeId head) const {
65 return _parents_.exists(head) && _parents_[head]->exists(tail);
66 }
67
69 if (!_parents_.exists(id)) { _parents_.insert(id, new NodeSet); }
70 }
71
73 if (!_children_.exists(id)) { _children_.insert(id, new NodeSet); }
74 }
75
76 INLINE const NodeSet& ArcGraphPart::parents(NodeId id) const {
77 if (_parents_.exists(id)) return *(_parents_[id]);
78 else return emptyNodeSet;
79 }
80
82 INLINE NodeSet ArcGraphPart::children(const NodeSet& ids) const {
83 NodeSet res;
84 for (const auto node: ids)
85 res += children(node);
86 return res;
87 }
88
90 INLINE NodeSet ArcGraphPart::parents(const NodeSet& ids) const {
91 NodeSet res;
92 for (const auto node: ids)
93 res += parents(node);
94 return res;
95 }
96
97 INLINE const NodeSet& ArcGraphPart::children(NodeId id) const {
98 if (_children_.exists(id)) return *_children_[id];
99 else return emptyNodeSet;
100 }
101
102 INLINE void ArcGraphPart::addArc(NodeId tail, NodeId head) {
103 Arc arc(tail, head);
104
105 _arcs_.insert(arc);
106 _checkParents_(head);
107 _checkChildren_(tail);
108 _parents_[head]->insert(tail);
109 _children_[tail]->insert(head);
110
111 GUM_EMIT2(onArcAdded, tail, head);
112 }
113
114 INLINE void ArcGraphPart::eraseArc(const Arc& arc) {
115 // ASSUMING tail and head exists in _parents_ anf _children_
116 // (if not, it is an error)
117 if (existsArc(arc)) {
118 NodeId tail = arc.tail();
119 NodeId head = arc.head();
120 _parents_[head]->erase(tail);
121 _children_[tail]->erase(head);
122 _arcs_.erase(arc);
123 GUM_EMIT2(onArcDeleted, tail, head);
124 }
125 }
126
127 INLINE void ArcGraphPart::eraseSetOfArcs_(const ArcSet& set) {
128 for (const auto& arc: set)
129 eraseArc(arc);
130 }
131
133 if (_parents_.exists(id)) {
134 const NodeSet& parents = *(_parents_[id]);
135
136 for (auto iter = parents.beginSafe(); // safe iterator needed here
137 iter != parents.endSafe();
138 ++iter) {
139 // warning: use this erase so that you actually use the virtualized
140 // arc removal function
141 eraseArc(Arc(*iter, id));
142 }
143 }
144 }
145
147 if (_children_.exists(id)) {
148 const NodeSet& children = *(_children_[id]);
149
150 for (auto iter = children.beginSafe(); // safe iterator needed here
151 iter != children.endSafe();
152 ++iter) {
153 // warning: use this erase so that you actually use the virtualized
154 // arc removal function
155 eraseArc(Arc(id, *iter));
156 }
157 }
158 }
159
161 for (const auto& arc: set)
163 }
164
166 if (_parents_.exists(id)) {
167 const NodeSet& parents = *(_parents_[id]);
168
169 for (auto iter = parents.beginSafe(); // safe iterator needed here
170 iter != parents.endSafe();
171 ++iter) {
172 ArcGraphPart::eraseArc(Arc(*iter, id));
173 }
174 }
175 }
176
178 if (_children_.exists(id)) {
179 const NodeSet& children = *(_children_[id]);
180
181 for (auto iter = children.beginSafe(); // safe iterator needed here
182 iter != children.endSafe();
183 ++iter) {
184 ArcGraphPart::eraseArc(Arc(id, *iter));
185 }
186 }
187 }
188
189 INLINE bool ArcGraphPart::operator==(const ArcGraphPart& p) const { return _arcs_ == p._arcs_; }
190
191} /* 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
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
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
Signaler< NodeId, NodeId > onArcDeleted
void unvirtualizedEraseSetOfArcs_(const ArcSet &set)
similar to eraseSetOfArcs_ except that it is unvirtualized
Signaler< NodeId, NodeId > onArcAdded
NodeProperty< NodeSet * > _parents_
for each arc, the sets of its parents
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...
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
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 signaler.h:290