aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
arcGraphPart.cpp
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
49
50#ifdef GUM_NO_INLINE
52#endif // GU%_NO_INLINE
54
55namespace gum {
56
58 ArcGraphPart::ArcGraphPart(Size arcs_size, bool arcs_resize_policy) :
59 _arcs_(arcs_size, arcs_resize_policy) {
60 GUM_CONSTRUCTOR(ArcGraphPart);
61 }
62
64 _arcs_(std::move(s._arcs_)), _parents_(std::move(s._parents_)),
65 _children_(std::move(s._children_)) {
66 GUM_CONS_MOV(ArcGraphPart);
67 }
68
70 GUM_CONS_CPY(ArcGraphPart);
71
72 // copy the sets of parents
73 const NodeProperty< NodeSet* >& pars = s._parents_;
74 _parents_.resize(pars.capacity());
75
76 for (const auto& [key, nodeset]: pars) {
77 NodeSet* newpar = new NodeSet(*nodeset);
78 _parents_.insert(key, newpar);
79 }
80
81 // copy the sets of children
83 _children_.resize(children.capacity());
84
85 for (const auto& [key, nodeset]: children) {
86 NodeSet* newchildren = new NodeSet(*nodeset);
87 _children_.insert(key, newchildren);
88 }
89
90 // send signals to indicate that there are new arcs
91 if (onArcAdded.hasListener()) {
92 for (const auto& arc: _arcs_) {
93 GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
94 }
95 }
96 }
97
99 GUM_DESTRUCTOR(ArcGraphPart);
100 // be sure to deallocate all the parents and children sets
101 clearArcs();
102 }
103
105 for (const auto& elt: _parents_)
106 delete elt.second;
107
108 _parents_.clear();
109
110 for (const auto& elt: _children_)
111 delete elt.second;
112
113 _children_.clear();
114
115 // we need this copy only if at least one onArcDeleted listener exists
116 if (onArcDeleted.hasListener()) {
117 ArcSet tmp = _arcs_;
118 _arcs_.clear();
119
120 for (const auto& arc: tmp)
121 GUM_EMIT2(onArcDeleted, arc.tail(), arc.head());
122 } else {
123 _arcs_.clear();
124 }
125 }
126
128 // avoid self assignment
129 if (this != &s) {
130 // copy the arcs
131 clearArcs();
132 _arcs_ = s._arcs_;
133
134 // copy the sets of parents
135 _parents_.resize(s._parents_.capacity());
136
137 for (const auto& [key, nodeset]: s._parents_) {
138 NodeSet* newpar = new NodeSet(*nodeset);
139 _parents_.insert(key, newpar);
140 }
141
142 // copy the sets of children
143 _children_.resize(s._children_.capacity());
144
145 for (const auto& [key, nodeset]: s._children_) {
146 NodeSet* newchildren = new NodeSet(*nodeset);
147 _children_.insert(key, newchildren);
148 }
149
150 if (onArcAdded.hasListener()) {
151 for (const auto& arc: _arcs_) {
152 GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
153 }
154 }
155
156 GUM_OP_CPY(ArcGraphPart);
157 }
158
159 return *this;
160 }
161
163 if (this != &s) {
164 clearArcs();
165 _arcs_ = std::move(s._arcs_);
166 _parents_ = std::move(s._parents_);
167 _children_ = std::move(s._children_);
168 if (onArcAdded.hasListener()) {
169 for (const auto& arc: _arcs_) {
170 GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
171 }
172 }
173 GUM_OP_MOV(ArcGraphPart);
174 }
175 return *this;
176 }
177
178 std::string ArcGraphPart::toString() const {
179 std::stringstream s;
180 bool first = true;
181 s << "{";
182
183 for (const auto& arc: _arcs_) {
184 if (first) {
185 first = false;
186 } else {
187 s << ",";
188 }
189
190 s << arc;
191 }
192
193 s << "}";
194
195 return s.str();
196 }
197
198 std::ostream& operator<<(std::ostream& stream, const ArcGraphPart& set) {
199 stream << set.toString();
200 return stream;
201 }
202
203} /* namespace gum */
Inline implementation of classes for directed edge sets.
Classes for directed edge sets.
virtual ~ArcGraphPart()
destructor
Set< Arc > _arcs_
the set of all the arcs contained within the ArcGraphPart
ArcGraphPart & operator=(const ArcGraphPart &s)
copy operator
void clearArcs()
removes all the arcs from the ArcGraphPart
NodeProperty< NodeSet * > _children_
for each arc, the set of its children
Signaler< NodeId, NodeId > onArcDeleted
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 ...
ArcGraphPart(Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
std::string toString() const
to friendly display the content of the ArcGraphPart
Size capacity() const noexcept
Returns the number of slots in the 'nodes' vector of the hashtable.
some utils for topology : NodeId, Edge, Arc and consorts ...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Set< Arc > ArcSet
Some typdefs and define for shortcuts ...
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
STL namespace.
#define GUM_EMIT2(signal, arg1, arg2)
Definition signaler.h:290