aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
nodeGraphPart.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
50
51#ifdef GUM_NO_INLINE
53#endif // GU%_NO_INLINE
54
55namespace gum {
56
58 NodeGraphPart::NodeGraphPart(Size holes_size, bool holes_resize_policy) :
59 _holes_size_(holes_size), _holes_resize_policy_(holes_resize_policy),
60 _endIteratorSafe_(*this), _boundVal_(0) {
61 _holes_ = nullptr;
62 GUM_CONSTRUCTOR(NodeGraphPart);
64 }
65
79
84 s._holes_ = nullptr;
85 s._boundVal_ = 0;
86
88
89 GUM_CONS_MOV(NodeGraphPart);
90 }
91
93 if (_holes_) delete _holes_;
94
95 GUM_DESTRUCTOR(NodeGraphPart);
96 }
97
99 clear(); // "virtual" flush of the nodes set
102
103 if (s._holes_) _holes_ = new NodeSet(*s._holes_);
104
105 _names_ = s._cloneNames_();
106
108
110 }
111
112 // id is assumed to belong to NodeGraphPart
114 // we assume that the node exists
115 if (node + 1 == _boundVal_) {
116 // we remove the max : no new hole and maybe a bunch of holes to remove
117 --_boundVal_;
118
119 if (_holes_) {
120 while (_holes_->contains(_boundVal_ - 1)) {
121 // a bunch of holes to remove. We do not use inHoles for optimisation
122 // :
123 // not to repeat the test if ( _holes_) each time
125 }
126
127 if (_holes_->empty()) {
128 delete _holes_;
129 _holes_ = nullptr;
130 }
131 }
132
134 } else {
136
137 _holes_->insert(node);
138 }
139 }
140
141 std::string NodeGraphPart::toString() const {
142 std::string s = "{";
143 bool first = true;
144
145 const bool hasNames = (_names_ != nullptr);
146 for (NodeId id = 0; id < _boundVal_; ++id) {
147 if (_inHoles_(id)) continue;
148
149 if (!first) s += ",";
150 first = false;
151
152 if (hasNames && _names_->existsFirst(id)) s += std::format("{}<{}>", id, _names_->second(id));
153 else s += std::format("{}", id);
154 }
155
156 s += "}";
157
158 return s;
159 }
160
161 std::string NodeGraphPart::nameFromId(NodeId id) const {
162 if (_names_ && _names_->existsFirst(id)) return _names_->second(id);
163 return std::to_string(id);
164 }
165
166 std::optional< NodeId > NodeGraphPart::idFromName(const std::string& name) const {
167 if (_names_ && _names_->existsSecond(name)) return _names_->first(name);
168 return std::nullopt;
169 }
170
171 void NodeGraphPart::setName(NodeId id, const std::string& name) {
172 if (!existsNode(id)) GUM_ERROR(InvalidNode, "node " << id << " does not exist")
173 if (_names_) {
174 auto owner = _names_->tryFirst(name);
175 if (owner.has_value()) {
176 if (*owner != id)
177 GUM_ERROR(DuplicateElement, "name '" << name << "' already used by node " << *owner)
178 return;
179 }
180 if (_names_->existsFirst(id)) _names_->eraseFirst(id);
181 } else {
182 _names_ = std::make_unique< Bijection< NodeId, std::string > >();
183 }
184 _names_->insert(id, name);
185 }
186
187 bool NodeGraphPart::hasName(NodeId id) const { return _names_ && _names_->existsFirst(id); }
188
189 std::string NodeGraphPart::dotNodeText(NodeId id) const {
190 if (!hasName(id)) return "";
191 return "(" + std::to_string(id) + ") " + _names_->second(id);
192 }
193
195 const dot::DotStyle& style,
196 const dot::DotOptions& options,
197 NodeId id) const {
198 writer.node(id, dot::nodeAttributes(style, options.node(id), dotNodeText(id)));
199 }
200
202 const dot::DotStyle& style,
203 const dot::DotOptions& options) const {
204 for (const auto node: nodes())
205 writeDotNode(writer, style, options, node);
206 }
207
208 std::ostream& operator<<(std::ostream& stream, const NodeGraphPart& set) {
209 stream << set.toString();
210 return stream;
211 }
212
214 if (id >= _boundVal_) {
215 if (id > _boundVal_) { // we have to add holes
217
218 for (NodeId i = _boundVal_; i < id; ++i)
219 _holes_->insert(i);
220 }
221
222 _boundVal_ = id + 1;
223
225 } else {
226 if (_inHoles_(id)) { // we fill a hole
227 _eraseHole_(id);
228 } else {
229 GUM_ERROR(DuplicateElement, "Id " << id << " is already used")
230 }
231 }
232
234 }
235
238 _boundVal_ = 0;
239
241 for (NodeId n = 0; n < bound; ++n) {
242 if (!_inHoles_(n)) GUM_EMIT1(onNodeDeleted, n);
243 }
244 }
245
247
248 delete (_holes_);
249 _holes_ = nullptr;
250 _names_.reset();
251 }
252
254 if (id == pos_) { // we just deleted the pos_ in NodeGraphPart
255 valid_ = false;
256 }
257
258 if (pos_ >= nodes_->bound()) { // moreover, it was the last position
259 pos_ = nodes_->bound();
260 valid_ = false;
261 }
262 }
263
264} /* namespace gum */
Exception : a similar element already exists.
Exception : node does not exist.
void whenNodeDeleted(const void *src, NodeId id)
called when a node is deleted in the iterated NodeGraphPart
const NodeGraphPart * nodes_
the nodegraphpart on which points the iterator
NodeId pos_
the nodeid on which the iterator points currently
Class for node sets in graph.
NodeGraphPartIteratorSafe _endIteratorSafe_
the end iterator (used to speed-up parsings of the NodeGraphPart)
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
Signaler< NodeId > onNodeDeleted
void _clearNodes_()
code for clearing nodes (called twice)
virtual void clear()
alias for clearNodes
std::unique_ptr< Bijection< NodeId, std::string > > _names_
optional node names — null when no name has been set
NodeId bound() const
returns a number n such that all node ids are strictly lower than n
bool hasName(NodeId id) const
returns true iff node id has an explicit name
void _eraseHole_(NodeId id)
to delete hole.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
std::string nameFromId(NodeId id) const
returns the name of node id, or "<id>" if no name is set
void _updateEndIteratorSafe_()
updating endIterator (always at max+1)
void setName(NodeId id, const std::string &name)
sets the name of node id
Size _holes_size_
value for holes configuration
void writeDotNodes(dot::DotWriter &writer, const dot::DotStyle &style, const dot::DotOptions &options) const
writes the nodes in the dot language, see writeDotNode()
virtual std::string toString() const
a function to display the set of nodes
std::string dotNodeText(NodeId id) const
the label of a node in a graph drawn with graphviz : "(id) name", or "" if the node has no name
bool _holes_resize_policy_
value for holes configuration
virtual ~NodeGraphPart()
destructor
std::unique_ptr< Bijection< NodeId, std::string > > _cloneNames_() const
clone the names bijection (returns nullptr when no name has been set)
NodeSet * _holes_
the set of nodes not contained in the NodeGraphPart in the interval 1.
Signaler< NodeId > onNodeAdded
NodeId _boundVal_
the id below which NodeIds may belong to the NodeGraphPart
std::optional< NodeId > idFromName(const std::string &name) const
returns the id of the node with the given name, or std::nullopt
void writeDotNode(dot::DotWriter &writer, const dot::DotStyle &style, const dot::DotOptions &options, NodeId id) const
writes a node in the dot language, with the label "(id) name" if it is named and the colors given by ...
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
void _addHole_(NodeId id)
to add a hole.
bool _inHoles_(NodeId id) const
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
const DotNodeOptions * node(Size node) const
the options of a node, or nullptr if none was given for it
Writes a graph in the dot language, statement after statement, in the order of the calls.
Definition dotWriter.h:100
void node(std::string_view id, const DotAttributes &attributes={})
a node, whose identifier is id (quoted).
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
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.
Base node set class for graphs.
Inline implementation of the base node set class for graphs.
#define GUM_EMIT1(signal, arg1)
Definition signaler.h:289
The look shared by all the graphs: size, direction, layout, colors of the arcs and of the nodes.
Definition dotStyle.h:76