aGrUM 3.2.0
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 class NodeGraphPart;
64
69 class GUM_SHARED_PUBLIC NodeGraphPartIterator {
70 friend class NodeGraphPart;
71
72 public:
75 using iterator_category = std::forward_iterator_tag;
80 using const_pointer = const value_type*;
81 using difference_type = std::ptrdiff_t;
83
84 // ############################################################################
86 // ############################################################################
88
92 explicit NodeGraphPartIterator(const NodeGraphPart& nodes) noexcept;
93
96
99
101 virtual ~NodeGraphPartIterator() noexcept;
102
104
105 // ############################################################################
107 // ############################################################################
109
111 NodeGraphPartIterator& operator=(const NodeGraphPartIterator& it) noexcept;
112
114 NodeGraphPartIterator& operator=(NodeGraphPartIterator&& it) noexcept;
115
117 bool operator==(const NodeGraphPartIterator& it) const noexcept;
118
119
121 NodeGraphPartIterator& operator++() noexcept;
122
124 value_type operator*() const;
125
127
128 protected:
130 void setPos_(NodeId id) noexcept;
131
133 void validate_() noexcept;
134
137
140
141 // is this iterator still valid ?
142 bool valid_{false};
143 };
144
149 class GUM_SHARED_PUBLIC NodeGraphPartIteratorSafe final:
151 public Listener {
152 friend class NodeGraphPart;
153
154 public:
157 using iterator_category = std::forward_iterator_tag;
162 using const_pointer = const value_type*;
163 using difference_type = std::ptrdiff_t;
165
166 // ############################################################################
168 // ############################################################################
170
172 explicit NodeGraphPartIteratorSafe(const NodeGraphPart& nodes);
173
176
179
182
184
185 // ############################################################################
187 // ############################################################################
189
192
195
197
198 // ############################################################################
200 // ############################################################################
202
204
208 void whenNodeDeleted(const void* src, NodeId id);
209
211 };
212
264
265 class GUM_SHARED_PUBLIC NodeGraphPart {
266 public:
274
275 // something strange with SWIG (with "using", these definitions cored dump
276 // the
277 // swig process)
282
285
286 // ############################################################################
288 // ############################################################################
290
292
299 explicit NodeGraphPart(Size holes_size = HashTableConst::default_size,
300 bool holes_resize_policy = true);
301
303
305
308
310 virtual ~NodeGraphPart();
311
313
314 // ############################################################################
316 // ############################################################################
318
320
322
325
327
328 bool operator==(const NodeGraphPart& p) const;
329
330
332
333 // ############################################################################
335 // ############################################################################
337
339
342 void populateNodes(const NodeGraphPart& s);
343
345
347 template < typename T >
349
354 NodeId nextNodeId() const;
355
357
358 virtual NodeId addNode();
359
365 std::vector< NodeId > addNodes(Size n);
366
368
372 virtual void addNodeWithId(const NodeId id);
373
375
379 virtual void eraseNode(const NodeId id);
380
382 bool existsNode(const NodeId id) const;
383
385 bool exists(const NodeId id) const;
386
388 bool emptyNodes() const;
389
391 bool empty() const;
392
394 virtual void clearNodes();
395
397 virtual void clear();
398
400 Size sizeNodes() const;
401
403 Size size() const;
404
406 NodeId bound() const;
407
409
416 NodeSet asNodeSet() const;
417
419 const NodeGraphPart& nodes() const;
420
424
427 const node_iterator_safe& endSafe() const noexcept;
428
431 node_iterator begin() const noexcept;
432
435 const node_iterator& end() const noexcept;
436
437 virtual
438 std::string toString() const;
439
441 std::string nameFromId(NodeId id) const;
442
444 std::optional< NodeId > idFromName(const std::string& name) const;
445
448 void setName(NodeId id, const std::string& name);
449
451 bool hasName(NodeId id) const;
452
454 std::string dotNodeLabel(NodeId id) const;
455
457
466 template < typename VAL >
467 NodeProperty< VAL > nodesPropertyFromFunction(VAL (*f)(const NodeId&), Size size = 0) const;
468
470
480 template < typename VAL >
481 NodeProperty< VAL > nodesPropertyFromVal(const VAL& a, Size size = 0) const;
482
486 template < typename VAL >
487 List< VAL > listMapNodes(VAL (*f)(const NodeId&)) const;
488
490
491 private:
494
497
499 void _clearNodes_();
500
503 void _eraseHole_(NodeId id);
504
507 void _addHole_(NodeId id);
508
509 // ############################################################################
511 // ############################################################################
513
515 bool _inHoles_(NodeId id) const;
516
518 Size _sizeHoles_() const;
519
521
526
528 std::unique_ptr< Bijection< NodeId, std::string > > _names_;
529
530 std::unique_ptr< Bijection< NodeId, std::string > > _cloneNames_() const;
531
534
537
540
543 };
544
546 std::ostream& operator<<(std::ostream&, const NodeGraphPart&);
547
548} /* namespace gum */
549
550#ifndef GUM_NO_INLINE
552#endif // GU%_NO_INLINE
553
555
556#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
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 dotNodeLabel(NodeId id) const
returns " [label=\"...\"]" with DOT-escaped name, or "" if 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
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
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