aGrUM  0.14.2
edgeGraphPart_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more defirsts. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // to ease parsing by IDE
29 
30 namespace gum {
31 
32  INLINE bool EdgeGraphPart::emptyEdges() const { return __edges.empty(); }
33 
34  INLINE Size EdgeGraphPart::sizeEdges() const { return __edges.size(); }
35 
36  INLINE const EdgeSet& EdgeGraphPart::edges() const { return __edges; }
37 
38  INLINE bool EdgeGraphPart::existsEdge(const Edge& edge) const {
39  return __edges.contains(edge);
40  }
41 
42  INLINE bool EdgeGraphPart::existsEdge(const NodeId first,
43  const NodeId second) const {
44  return __neighbours.exists(first) && __neighbours[first]->exists(second);
45  }
46 
47  INLINE void EdgeGraphPart::__checkNeighbours(const NodeId id) const {
48  if (!__neighbours.exists(id)) { __neighbours.insert(id, new NodeSet); }
49  }
50 
51  INLINE void EdgeGraphPart::addEdge(const NodeId first, const NodeId second) {
52  Edge edge(first, second);
53  __edges.insert(edge);
54  __checkNeighbours(first);
55  __checkNeighbours(second);
56  __neighbours[first]->insert(second);
57  __neighbours[second]->insert(first);
58 
59  GUM_EMIT2(onEdgeAdded, first, second);
60  }
61 
62  INLINE void EdgeGraphPart::eraseEdge(const Edge& edge) {
63  if (existsEdge(edge)) {
64  // ASSUMING first and second exists in __neighbours (if not, it is an
65  // error)
66  NodeId id1 = edge.first(), id2 = edge.second();
67 
68  __neighbours[id1]->erase(id2);
69  __neighbours[id2]->erase(id1);
70  __edges.erase(edge);
71  GUM_EMIT2(onEdgeDeleted, id1, id2);
72  }
73  }
74 
75  INLINE const NodeSet& EdgeGraphPart::neighbours(const NodeId id) const {
77  return *(__neighbours[id]);
78  }
79 
80  INLINE void EdgeGraphPart::eraseNeighbours(const NodeId id) {
81  if (__neighbours.exists(id)) {
82  const NodeSet& set = neighbours(id);
83 
84  for (auto iter = set.beginSafe(); iter != set.endSafe();
85  ++iter) { // safe iterator needed here
86  // warning: use this erase so that you actually use the virtualized
87  // edge removal function
88  eraseEdge(Edge(*iter, id));
89  }
90  }
91  }
92 
94  if (__neighbours.exists(id)) {
95  const NodeSet& set = neighbours(id);
96 
97  for (auto iter = set.beginSafe(); iter != set.endSafe();
98  ++iter) { // safe iterator needed here
99  EdgeGraphPart::eraseEdge(Edge(*iter, id));
100  }
101  }
102  }
103 
104  INLINE bool EdgeGraphPart::operator==(const EdgeGraphPart& p) const {
105  return __edges == p.__edges;
106  }
107 
108  INLINE bool EdgeGraphPart::operator!=(const EdgeGraphPart& p) const {
109  return __edges != p.__edges;
110  }
111 
112 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
NodeId second() const
returns the node ID of the other extremal node ID
bool operator==(const EdgeGraphPart &p) const
tests whether two EdgeGraphParts contain the same edges
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:704
bool emptyEdges() const
indicates wether the EdgeGraphPart contains any edge
virtual void addEdge(const NodeId n1, const NodeId n2)
insert a new edge into the EdgeGraphPart
NodeProperty< NodeSet *> __neighbours
for each node, the set of its adjacent edges
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:653
Classes for undirected edge sets.
Definition: edgeGraphPart.h:72
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
void __checkNeighbours(const NodeId id) const
when the EdgeGraphPart contains no edge adjacent to a given node, this function adds an empty set ent...
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
NodeId first() const
returns one extremal node ID (whichever one it is is unspecified)
#define GUM_EMIT2(signal, arg1, arg2)
Definition: signaler2.h:40
bool existsEdge(const Edge &edge) const
indicates whether a given edge exists
Signaler2< NodeId, NodeId > onEdgeDeleted
Definition: edgeGraphPart.h:77
bool operator!=(const EdgeGraphPart &p) const
tests whether two EdgeGraphParts contain different edges
The base class for all undirected edges.
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
void unvirtualizedEraseNeighbours(const NodeId id)
same function as eraseNeighbours but without any virtual call to an erase
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
virtual void eraseEdge(const Edge &edge)
removes an edge from the EdgeGraphPart
Signaler2< NodeId, NodeId > onEdgeAdded
Definition: edgeGraphPart.h:76
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:698
void eraseNeighbours(const NodeId id)
erase all the edges adjacent to a given node
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
EdgeSet __edges
the set of all the edges contained within the EdgeGraphPart