aGrUM 3.1.1
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
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
150 friend class NodeGraphPart;
151
152 public:
155 using iterator_category = std::forward_iterator_tag;
160 using const_pointer = const value_type*;
161 using difference_type = std::ptrdiff_t;
163
164 // ############################################################################
166 // ############################################################################
168
170 explicit NodeGraphPartIteratorSafe(const NodeGraphPart& nodes);
171
174
177
180
182
183 // ############################################################################
185 // ############################################################################
187
190
193
195
196 // ############################################################################
198 // ############################################################################
200
202
206 void whenNodeDeleted(const void* src, NodeId id);
207
209 };
210
262
264 public:
272
273 // something strange with SWIG (with "using", these definitions cored dump
274 // the
275 // swig process)
280
283
284 // ############################################################################
286 // ############################################################################
288
290
297 explicit NodeGraphPart(Size holes_size = HashTableConst::default_size,
298 bool holes_resize_policy = true);
299
301
303
306
308 virtual ~NodeGraphPart();
309
311
312 // ############################################################################
314 // ############################################################################
316
318
320
323
325
326 bool operator==(const NodeGraphPart& p) const;
327
328
330
331 // ############################################################################
333 // ############################################################################
335
337
340 void populateNodes(const NodeGraphPart& s);
341
343
345 template < typename T >
347
352 NodeId nextNodeId() const;
353
355
356 virtual NodeId addNode();
357
363 std::vector< NodeId > addNodes(Size n);
364
366
370 virtual void addNodeWithId(const NodeId id);
371
373
377 virtual void eraseNode(const NodeId id);
378
380 bool existsNode(const NodeId id) const;
381
383 bool exists(const NodeId id) const;
384
386 bool emptyNodes() const;
387
389 bool empty() const;
390
392 virtual void clearNodes();
393
395 virtual void clear();
396
398 Size sizeNodes() const;
399
401 Size size() const;
402
404 NodeId bound() const;
405
407
414 NodeSet asNodeSet() const;
415
417 const NodeGraphPart& nodes() const;
418
422
425 const node_iterator_safe& endSafe() const noexcept;
426
429 node_iterator begin() const noexcept;
430
433 const node_iterator& end() const noexcept;
434
435 virtual
436 std::string toString() const;
437
439 std::string nameFromId(NodeId id) const;
440
442 std::optional< NodeId > idFromName(const std::string& name) const;
443
446 void setName(NodeId id, const std::string& name);
447
449 bool hasName(NodeId id) const;
450
452 std::string dotNodeLabel(NodeId id) const;
453
455
464 template < typename VAL >
465 NodeProperty< VAL > nodesPropertyFromFunction(VAL (*f)(const NodeId&), Size size = 0) const;
466
468
478 template < typename VAL >
479 NodeProperty< VAL > nodesPropertyFromVal(const VAL& a, Size size = 0) const;
480
484 template < typename VAL >
485 List< VAL > listMapNodes(VAL (*f)(const NodeId&)) const;
486
488
489 private:
492
495
497 void _clearNodes_();
498
501 void _eraseHole_(NodeId id);
502
505 void _addHole_(NodeId id);
506
507 // ############################################################################
509 // ############################################################################
511
513 bool _inHoles_(NodeId id) const;
514
516 Size _sizeHoles_() const;
517
519
524
526 std::unique_ptr< Bijection< NodeId, std::string > > _names_;
527
528 std::unique_ptr< Bijection< NodeId, std::string > > _cloneNames_() const;
529
532
535
538
541 };
542
544 std::ostream& operator<<(std::ostream&, const NodeGraphPart&);
545
546} /* namespace gum */
547
548#ifndef GUM_NO_INLINE
550#endif // GU%_NO_INLINE
551
553
554#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
~NodeGraphPartIteratorSafe() final
destructor
NodeGraphPartIteratorSafe & operator=(const NodeGraphPartIteratorSafe &it)
copy assignment operator
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
bool operator==(const NodeGraphPartIterator &it) const noexcept
checks whether two iterators point toward the same node
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
virtual ~NodeGraphPartIterator() noexcept
destructor
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
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
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