aGrUM 2.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-2025 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-2025 *
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 <utility>
52
53#include <agrum/agrum.h>
54
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59
60namespace gum_tests {
61
62 class NodeGraphPartTestSuite;
63}
64
65#endif
66
67namespace gum {
68
69 class NodeGraphPart;
70
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 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
126 bool operator!=(const NodeGraphPartIterator& it) const noexcept;
127
129 NodeGraphPartIterator& operator++() noexcept;
130
132 value_type operator*() const;
133
135
136 protected:
138 void setPos_(NodeId id) noexcept;
139
141 void validate_() noexcept;
142
145
148
149 // is this iterator still valid ?
150 bool valid_{false};
151 };
152
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
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
272 public:
280
281 // something strange with SWIG (with "using", these definitions cored dump
282 // the
283 // swig process)
288
289 Signaler1< NodeId > onNodeAdded;
290 Signaler1< NodeId > onNodeDeleted;
291
292 // ############################################################################
294 // ############################################################################
296
298
305 explicit NodeGraphPart(Size holes_size = HashTableConst::default_size,
306 bool holes_resize_policy = true);
307
309
311
313 virtual ~NodeGraphPart();
314
316
317 // ############################################################################
319 // ############################################################################
321
323
325
327
328 bool operator==(const NodeGraphPart& p) const;
329
331
332 bool operator!=(const NodeGraphPart& p) const;
333
335
336 // ############################################################################
338 // ############################################################################
340
342
345 void populateNodes(const NodeGraphPart& s);
346
348
350 template < typename T >
352
357 NodeId nextNodeId() const;
358
360
361 virtual NodeId addNode();
362
368 std::vector< NodeId > addNodes(Size n);
369
371
375 virtual void addNodeWithId(const NodeId id);
376
378
382 virtual void eraseNode(const NodeId id);
383
385 bool existsNode(const NodeId id) const;
386
388 bool exists(const NodeId id) const;
389
391 bool emptyNodes() const;
392
394 bool empty() const;
395
397 virtual void clearNodes();
398
400 virtual void clear();
401
403 Size sizeNodes() const;
404
406 Size size() const;
407
409 NodeId bound() const;
410
412
419 NodeSet asNodeSet() const;
420
422 const NodeGraphPart& nodes() const;
423
427
430 const node_iterator_safe& endSafe() const noexcept;
431
434 node_iterator begin() const noexcept;
435
438 const node_iterator& end() const noexcept;
439
440 virtual
441 std::string toString() const;
442
444
453 template < typename VAL >
454 NodeProperty< VAL > nodesPropertyFromFunction(VAL (*f)(const NodeId&), Size size = 0) const;
455
457
467 template < typename VAL >
468 NodeProperty< VAL > nodesPropertyFromVal(const VAL& a, Size size = 0) const;
469
473 template < typename VAL >
474 List< VAL > listMapNodes(VAL (*f)(const NodeId&)) const;
475
477
478 private:
481
483
485
488
490 void _clearNodes_();
491
494 void _eraseHole_(NodeId id);
495
498 void _addHole_(NodeId id);
499
500 // ############################################################################
502 // ############################################################################
504
506 bool _inHoles_(NodeId id) const;
507
509 Size _sizeHoles_() const;
510
512
517
520
523
526
529 };
530
532 std::ostream& operator<<(std::ostream&, const NodeGraphPart&);
533
534} /* namespace gum */
535
536#ifndef GUM_NO_INLINE
538#endif // GU%_NO_INLINE
539
541
542#endif // GUM_NODE_GRAPH_PART_H
Generic doubly linked lists.
Definition list.h:379
Listener()
Class constructor.
Definition listener.cpp:54
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
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)
Signaler1< NodeId > onNodeDeleted
Size size() const
alias for sizeNodes
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
Signaler1< NodeId > onNodeAdded
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
NodeId bound() const
returns a number n such that all node ids are strictly lower than n
NodeGraphPartIteratorSafe NodeIteratorSafe
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
void _updateEndIteratorSafe_()
updating endIterator (always at max+1)
virtual void eraseNode(const NodeId id)
erase the node with the given id
friend class gum_tests::NodeGraphPartTestSuite
to enable testunits to use check
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
NodeId nextNodeId() const
returns a new node id, not yet used by any node
bool _holes_resize_policy_
value for holes configuration
virtual ~NodeGraphPart()
destructor
NodeSet * _holes_
the set of nodes not contained in the NodeGraphPart in the interval 1.
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
bool operator!=(const NodeGraphPart &p) const
check whether two NodeGraphParts contain different nodes
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
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.
Declare all signalers.
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:101