aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
nodeGraphPart.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
47#ifndef GUM_NODE_GRAPH_PART_H
48#define GUM_NODE_GRAPH_PART_H
49
50#include <algorithm>
51#include <memory>
52#include <optional>
53#include <utility>
54
55#include <agrum/agrum.h>
56
60
61namespace gum {
62
63 namespace dot {
64 class DotOptions;
65 class DotWriter;
66 struct DotStyle;
67 } // namespace dot
68
69 class NodeGraphPart;
70
75 class GUM_SHARED_PUBLIC NodeGraphPartIterator {
76 friend class NodeGraphPart;
77
78 public:
81 using iterator_category = std::forward_iterator_tag;
86 using const_pointer = const value_type*;
87 using difference_type = std::ptrdiff_t;
89
90 // ############################################################################
92 // ############################################################################
94
98 explicit NodeGraphPartIterator(const NodeGraphPart& nodes) noexcept;
99
102
105
107 virtual ~NodeGraphPartIterator() noexcept;
108
110
111 // ############################################################################
113 // ############################################################################
115
117 NodeGraphPartIterator& operator=(const NodeGraphPartIterator& it) noexcept;
118
120 NodeGraphPartIterator& operator=(NodeGraphPartIterator&& it) noexcept;
121
123 bool operator==(const NodeGraphPartIterator& it) const noexcept;
124
125
127 NodeGraphPartIterator& operator++() noexcept;
128
130 value_type operator*() const;
131
133
134 protected:
136 void setPos_(NodeId id) noexcept;
137
139 void validate_() noexcept;
140
143
146
147 // is this iterator still valid ?
148 bool valid_{false};
149 };
150
155 class GUM_SHARED_PUBLIC NodeGraphPartIteratorSafe final:
157 public Listener {
158 friend class NodeGraphPart;
159
160 public:
163 using iterator_category = std::forward_iterator_tag;
168 using const_pointer = const value_type*;
169 using difference_type = std::ptrdiff_t;
171
172 // ############################################################################
174 // ############################################################################
176
178 explicit NodeGraphPartIteratorSafe(const NodeGraphPart& nodes);
179
182
185
188
190
191 // ############################################################################
193 // ############################################################################
195
198
201
203
204 // ############################################################################
206 // ############################################################################
208
210
214 void whenNodeDeleted(const void* src, NodeId id);
215
217 };
218
270
271 class GUM_SHARED_PUBLIC NodeGraphPart {
272 public:
280
281 // something strange with SWIG (with "using", these definitions cored dump
282 // the
283 // swig process)
288
291
292 // ############################################################################
294 // ############################################################################
296
298
305 explicit NodeGraphPart(Size holes_size = HashTableConst::default_size,
306 bool holes_resize_policy = true);
307
309
311
314
316 virtual ~NodeGraphPart();
317
319
320 // ############################################################################
322 // ############################################################################
324
326
328
331
333
334 bool operator==(const NodeGraphPart& p) const;
335
336
338
339 // ############################################################################
341 // ############################################################################
343
345
348 void populateNodes(const NodeGraphPart& s);
349
351
353 template < typename T >
355
360 NodeId nextNodeId() const;
361
363
364 virtual NodeId addNode();
365
371 std::vector< NodeId > addNodes(Size n);
372
374
378 virtual void addNodeWithId(const NodeId id);
379
381
385 virtual void eraseNode(const NodeId id);
386
388 bool existsNode(const NodeId id) const;
389
391 bool exists(const NodeId id) const;
392
394 bool emptyNodes() const;
395
397 bool empty() const;
398
400 virtual void clearNodes();
401
403 virtual void clear();
404
406 Size sizeNodes() const;
407
409 Size size() const;
410
412 NodeId bound() const;
413
415
422 NodeSet asNodeSet() const;
423
425 const NodeGraphPart& nodes() const;
426
430
433 const node_iterator_safe& endSafe() const noexcept;
434
437 node_iterator begin() const noexcept;
438
441 const node_iterator& end() const noexcept;
442
443 virtual
444 std::string toString() const;
445
447 std::string nameFromId(NodeId id) const;
448
450 std::optional< NodeId > idFromName(const std::string& name) const;
451
454 void setName(NodeId id, const std::string& name);
455
457 bool hasName(NodeId id) const;
458
460
469 template < typename VAL >
470 NodeProperty< VAL > nodesPropertyFromFunction(VAL (*f)(const NodeId&), Size size = 0) const;
471
473
483 template < typename VAL >
484 NodeProperty< VAL > nodesPropertyFromVal(const VAL& a, Size size = 0) const;
485
489 template < typename VAL >
490 List< VAL > listMapNodes(VAL (*f)(const NodeId&)) const;
491
493
494 protected:
497 std::string dotNodeText(NodeId id) const;
498
501 void writeDotNode(dot::DotWriter& writer,
502 const dot::DotStyle& style,
503 const dot::DotOptions& options,
504 NodeId id) const;
505
507 void writeDotNodes(dot::DotWriter& writer,
508 const dot::DotStyle& style,
509 const dot::DotOptions& options) const;
510
511 private:
514
517
519 void _clearNodes_();
520
523 void _eraseHole_(NodeId id);
524
527 void _addHole_(NodeId id);
528
529 // ############################################################################
531 // ############################################################################
533
535 bool _inHoles_(NodeId id) const;
536
538 Size _sizeHoles_() const;
539
541
546
548 std::unique_ptr< Bijection< NodeId, std::string > > _names_;
549
550 std::unique_ptr< Bijection< NodeId, std::string > > _cloneNames_() const;
551
554
557
560
563 };
564
566 GUM_SHARED_PUBLIC std::ostream& operator<<(std::ostream&, const NodeGraphPart&);
567
568} /* namespace gum */
569
570#ifndef GUM_NO_INLINE
572#endif // GU%_NO_INLINE
573
575
576#endif // GUM_NODE_GRAPH_PART_H
Set of pairs of elements with fast search for both elements.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1640
Generic doubly linked lists.
Definition list.h:378
Listener()
Class constructor.
Definition listener.cpp:54
NodeId value_type
types for STL compliance
value_type & reference
types for STL compliance
std::ptrdiff_t difference_type
types for STL compliance
void whenNodeDeleted(const void *src, NodeId id)
called when a node is deleted in the iterated NodeGraphPart
NodeGraphPartIteratorSafe(const NodeGraphPart &nodes)
default constructor
const value_type * const_pointer
types for STL compliance
const value_type & const_reference
types for STL compliance
value_type * pointer
types for STL compliance
std::forward_iterator_tag iterator_category
types for STL compliance
std::ptrdiff_t difference_type
types for STL compliance
void setPos_(NodeId id) noexcept
this function is used by NodeGraphPart to update
const value_type & const_reference
types for STL compliance
const value_type * const_pointer
types for STL compliance
void validate_() noexcept
ensure that the nodeId is either end() either a valid NodeId
NodeGraphPartIterator(const NodeGraphPart &nodes) noexcept
Default constructor.
value_type & reference
types for STL compliance
const NodeGraphPart * nodes_
the nodegraphpart on which points the iterator
NodeId pos_
the nodeid on which the iterator points currently
std::forward_iterator_tag iterator_category
types for STL compliance
NodeId value_type
types for STL compliance
value_type * pointer
types for STL compliance
Class for node sets in graph.
NodeGraphPartIteratorSafe _endIteratorSafe_
the end iterator (used to speed-up parsings of the NodeGraphPart)
Size size() const
alias for sizeNodes
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
Size sizeNodes() const
returns the number of nodes in the NodeGraphPart
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
NodeGraphPartIteratorSafe NodeIteratorSafe
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
friend class NodeGraphPartIterator
NodeProperty< VAL > nodesPropertyFromFunction(VAL(*f)(const NodeId &), Size size=0) const
a method to create a HashTable with key:NodeId and value:VAL
NodeGraphPartIterator node_const_iterator
types for STL compliance
NodeGraphPartIteratorSafe node_const_iterator_safe
types for STL compliance
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
virtual void eraseNode(const NodeId id)
erase the node with the given id
NodeGraphPartIterator node_iterator
types for STL compliance
Size _holes_size_
value for holes configuration
node_iterator_safe beginSafe() const
a begin iterator to parse the set of nodes contained in the NodeGraphPart
NodeGraphPart & operator=(const NodeGraphPart &p)
copy operator
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
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
bool exists(const NodeId id) const
alias for existsNode
bool emptyNodes() const
indicates whether there exists nodes in the NodeGraphPart
const node_iterator_safe & endSafe() const noexcept
the end iterator to parse the set of nodes contained in the NodeGraphPart
bool empty() const
alias for emptyNodes
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
NodeId nextNodeId() const
returns a new node id, not yet used by any node
bool _holes_resize_policy_
value for holes configuration
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
virtual void clearNodes()
remove all the nodes from the NodeGraphPart
friend class NodeGraphPartIteratorSafe
virtual NodeId addNode()
insert a new node and return its id
NodeProperty< VAL > nodesPropertyFromVal(const VAL &a, Size size=0) const
a method to create a hashMap with key:NodeId and value:VAL
NodeGraphPartIteratorSafe node_iterator_safe
types for STL compliance
node_iterator begin() const noexcept
a begin iterator to parse the set of nodes contained in the NodeGraphPart
NodeGraphPartIterator NodeIterator
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
NodeGraphPartIteratorSafe NodeConstIteratorSafe
List< VAL > listMapNodes(VAL(*f)(const NodeId &)) const
a method to create a list of VAL from a set of nodes (using for every nodee, say x,...
NodeGraphPartIterator NodeConstIterator
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
const node_iterator & end() const noexcept
the end iterator to parse the set of nodes contained in the NodeGraphPart
void _addHole_(NodeId id)
to add a hole.
void populateNodesFromProperty(const NodeProperty< T > &h)
populateNodesFromProperty clears *this and fills it with the keys of "h"
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 operator==(const NodeGraphPart &p) const
check whether two NodeGraphParts contain the same nodes
std::vector< NodeId > addNodes(Size n)
insert n nodes
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
Writes a graph in the dot language, statement after statement, in the order of the calls.
Definition dotWriter.h:100
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
Size NodeId
Type for node ids.
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
STL namespace.
Inline implementation of the base node set class for graphs.
Implementation of the NodeGraphPart.
Convenience header for the signal/listener pattern.
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:102
The look shared by all the graphs: size, direction, layout, colors of the arcs and of the nodes.
Definition dotStyle.h:76