aGrUM 2.3.2
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-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#pragma once
41
42
49
50// to ease parsing by IDE
52
53namespace gum {
54
55 INLINE bool EdgeGraphPart::emptyEdges() const { return _edges_.empty(); }
56
57 INLINE Size EdgeGraphPart::sizeEdges() const { return _edges_.size(); }
58
59 INLINE const EdgeSet& EdgeGraphPart::edges() const { return _edges_; }
60
61 INLINE bool EdgeGraphPart::existsEdge(const Edge& edge) const { return _edges_.contains(edge); }
62
63 INLINE bool EdgeGraphPart::existsEdge(NodeId first, NodeId second) const {
64 return _neighbours_.exists(first) && _neighbours_[first]->exists(second);
65 }
66
68 if (!_neighbours_.exists(id)) { _neighbours_.insert(id, new NodeSet); }
69 }
70
71 INLINE void EdgeGraphPart::addEdge(NodeId first, NodeId second) {
72 Edge edge(first, second);
73 _edges_.insert(edge);
74 _checkNeighbours_(first);
75 _checkNeighbours_(second);
76 _neighbours_[first]->insert(second);
77 _neighbours_[second]->insert(first);
78
79 GUM_EMIT2(onEdgeAdded, first, second);
80 }
81
82 INLINE void EdgeGraphPart::eraseEdge(const Edge& edge) {
83 if (existsEdge(edge)) {
84 // ASSUMING first and second exists in _neighbours_ (if not, it is an
85 // error)
86 NodeId id1 = edge.first();
87 NodeId id2 = edge.second();
88
89 _neighbours_[id1]->erase(id2);
90 _neighbours_[id2]->erase(id1);
91 _edges_.erase(edge);
92 GUM_EMIT2(onEdgeDeleted, id1, id2);
93 }
94 }
95
96 INLINE const NodeSet& EdgeGraphPart::neighbours(NodeId id) const {
97 if (_neighbours_.exists(id)) return *(_neighbours_[id]);
98 else return emptyNodeSet;
99 }
100
102 if (_neighbours_.exists(id)) {
103 const NodeSet& set = *(_neighbours_[id]);
104
105 for (auto iter = set.beginSafe(); iter != set.endSafe();
106 ++iter) { // safe iterator needed here
107 // warning: use this erases so that you actually use the virtualized
108 // edge removal function
109 eraseEdge(Edge(*iter, id));
110 }
111 }
112 }
113
115 if (_neighbours_.exists(id)) {
116 const NodeSet& set = *(_neighbours_[id]);
117
118 for (auto iter = set.beginSafe(); iter != set.endSafe();
119 ++iter) { // safe iterator needed here
120 EdgeGraphPart::eraseEdge(Edge(*iter, id));
121 }
122 }
123 }
124
125 INLINE bool EdgeGraphPart::operator==(const EdgeGraphPart& p) const {
126 return _edges_ == p._edges_;
127 }
128
129} /* namespace gum */
void unvirtualizedEraseNeighbours(NodeId id)
same function as eraseNeighbours but without any virtual call to an erase
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
Signaler2< NodeId, NodeId > onEdgeDeleted
NodeProperty< NodeSet * > _neighbours_
for each node, the set of its adjacent edges
Signaler2< NodeId, NodeId > onEdgeAdded
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
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:414
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:426
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 signaler2.h:61