aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
edgeGraphPart_inl.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#pragma once
42
43
50
51// to ease parsing by IDE
53
54namespace gum {
55
56 INLINE bool EdgeGraphPart::emptyEdges() const { return _edges_.empty(); }
57
58 INLINE Size EdgeGraphPart::sizeEdges() const { return _edges_.size(); }
59
60 INLINE const EdgeSet& EdgeGraphPart::edges() const { return _edges_; }
61
62 INLINE bool EdgeGraphPart::existsEdge(const Edge& edge) const { return _edges_.contains(edge); }
63
64 INLINE bool EdgeGraphPart::existsEdge(NodeId first, NodeId second) const {
65 return _neighbours_.exists(first) && _neighbours_[first]->exists(second);
66 }
67
69 if (!_neighbours_.exists(id)) { _neighbours_.insert(id, new NodeSet); }
70 }
71
72 INLINE void EdgeGraphPart::addEdge(NodeId first, NodeId second) {
73 Edge edge(first, second);
74 _edges_.insert(edge);
75 _checkNeighbours_(first);
76 _checkNeighbours_(second);
77 _neighbours_[first]->insert(second);
78 _neighbours_[second]->insert(first);
79
80 GUM_EMIT2(onEdgeAdded, first, second);
81 }
82
83 INLINE void EdgeGraphPart::eraseEdge(const Edge& edge) {
84 if (existsEdge(edge)) {
85 // ASSUMING first and second exists in _neighbours_ (if not, it is an
86 // error)
87 NodeId id1 = edge.first();
88 NodeId id2 = edge.second();
89
90 _neighbours_[id1]->erase(id2);
91 _neighbours_[id2]->erase(id1);
92 _edges_.erase(edge);
93 GUM_EMIT2(onEdgeDeleted, id1, id2);
94 }
95 }
96
97 INLINE const NodeSet& EdgeGraphPart::neighbours(NodeId id) const {
98 if (_neighbours_.exists(id)) return *(_neighbours_[id]);
99 else return emptyNodeSet;
100 }
101
103 if (_neighbours_.exists(id)) {
104 const NodeSet& set = *(_neighbours_[id]);
105
106 for (auto iter = set.beginSafe(); iter != set.endSafe();
107 ++iter) { // safe iterator needed here
108 // warning: use this erases so that you actually use the virtualized
109 // edge removal function
110 eraseEdge(Edge(*iter, id));
111 }
112 }
113 }
114
116 if (_neighbours_.exists(id)) {
117 const NodeSet& set = *(_neighbours_[id]);
118
119 for (auto iter = set.beginSafe(); iter != set.endSafe();
120 ++iter) { // safe iterator needed here
121 EdgeGraphPart::eraseEdge(Edge(*iter, id));
122 }
123 }
124 }
125
126 INLINE bool EdgeGraphPart::operator==(const EdgeGraphPart& p) const {
127 return _edges_ == p._edges_;
128 }
129
130} /* namespace gum */
void unvirtualizedEraseNeighbours(NodeId id)
same function as eraseNeighbours but without any virtual call to an erase
Signaler< NodeId, NodeId > onEdgeAdded
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
void _checkNeighbours_(NodeId id)
when the EdgeGraphPart contains no edge adjacent to a given node, this function adds an empty set ent...
bool operator==(const EdgeGraphPart &p) const
tests whether two EdgeGraphParts contain the same edges
EdgeGraphPart(Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
virtual void addEdge(NodeId n1, NodeId n2)
insert a new edge into the EdgeGraphPart
EdgeSet _edges_
the set of all the edges contained within the EdgeGraphPart
virtual void eraseEdge(const Edge &edge)
removes an edge from the EdgeGraphPart
void eraseNeighbours(NodeId id)
erase all the edges adjacent to a given node
NodeProperty< NodeSet * > _neighbours_
for each node, the set of its adjacent edges
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
Signaler< NodeId, NodeId > onEdgeDeleted
bool existsEdge(const Edge &edge) const
indicates whether a given edge exists
bool emptyEdges() const
indicates wether the EdgeGraphPart contains any edge
const NodeSet & neighbours(NodeId id) const
returns the set of node neighbours to a given node
The base class for all undirected edges.
GUM_NODISCARD NodeId first() const
returns one extremal node ID (whichever one it is is unspecified)
GUM_NODISCARD NodeId second() const
returns the node ID of the other extremal node ID
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:385
static const iterator_safe & endSafe() noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:397
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Set< Edge > EdgeSet
Some typdefs and define for shortcuts ...
Size NodeId
Type for node ids.
const NodeSet emptyNodeSet
Some typdefs and define for shortcuts ...
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
#define GUM_EMIT2(signal, arg1, arg2)
Definition signaler.h:290