aGrUM  0.14.2
edgeGraphPart.cpp
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 details. *
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 
28 #ifdef GUM_NO_INLINE
30 #endif // GUM_NOINLINE
32 
33 namespace gum {
34 
36  EdgeGraphPart::EdgeGraphPart(Size edges_size, bool edges_resize_policy) :
37  __edges(edges_size, edges_resize_policy) {
38  GUM_CONSTRUCTOR(EdgeGraphPart);
39  }
40 
42  GUM_CONS_CPY(EdgeGraphPart);
43 
44  // copy the set of neighbours
45  __neighbours.resize(s.__neighbours.capacity());
46 
47  for (const auto& elt : s.__neighbours) {
48  NodeSet* newneigh = new NodeSet(*elt.second);
49  __neighbours.insert(elt.first, newneigh);
50  }
51 
52  // send signals to indicate that there are new edges
53  if (onEdgeAdded.hasListener())
54  for (const auto& edge : __edges)
55  GUM_EMIT2(onEdgeAdded, edge.first(), edge.second());
56  }
57 
59  GUM_DESTRUCTOR(EdgeGraphPart);
60  // be sure to deallocate all the neighbours sets
61  clearEdges();
62  }
63 
65  for (const auto& elt : __neighbours)
66  delete elt.second;
67 
68  __neighbours.clear();
69 
70  if (onEdgeDeleted.hasListener()) {
71  EdgeSet tmp = __edges;
72  __edges.clear();
73 
74  for (const auto& edge : tmp)
75  GUM_EMIT2(onEdgeDeleted, edge.first(), edge.second());
76  } else {
77  __edges.clear();
78  }
79  }
80 
82  // avoid self assignment
83  if (this != &s) {
84  clearEdges();
85 
86  __edges = s.__edges;
87 
88  // copy the set of neighbours
89  __neighbours.resize(s.__neighbours.capacity());
90 
91  for (const auto& elt : s.__neighbours) {
92  NodeSet* newneigh = new NodeSet(*elt.second);
93  __neighbours.insert(elt.first, newneigh);
94  }
95 
96  if (onEdgeAdded.hasListener())
97  for (const auto& edge : __edges)
98  GUM_EMIT2(onEdgeAdded, edge.first(), edge.second());
99  }
100 
101  return *this;
102  }
103 
104  const std::string EdgeGraphPart::toString() const {
105  std::stringstream s;
106  bool first = true;
107  s << "{";
108 
109  for (const auto edge : __edges) {
110  if (first)
111  first = false;
112  else
113  s << ",";
114 
115  s << edge;
116  }
117 
118  s << "}";
119 
120  return s.str();
121  }
122 
123  const std::vector< NodeId >
124  EdgeGraphPart::undirectedPath(const NodeId n1, const NodeId n2) const {
125  // not recursive version => use a FIFO for simulating the recursion
126  List< NodeId > nodeFIFO;
127  nodeFIFO.pushBack(n2);
128 
129  // mark[node] = predecessor if visited, else mark[node] does not exist
131  mark.insert(n2, n2);
132 
133  NodeId current;
134 
135  while (!nodeFIFO.empty()) {
136  current = nodeFIFO.front();
137  nodeFIFO.popFront();
138 
139  // check the neighbour
140  for (const auto new_one : neighbours(current)) {
141  if (mark.exists(new_one)) // if this node is already marked, stop
142  continue;
143 
144  mark.insert(new_one, current);
145 
146  if (new_one == n1) {
147  std::vector< NodeId > v;
148 
149  for (current = n1; current != n2; current = mark[current])
150  v.push_back(current);
151 
152  v.push_back(n2);
153 
154  return v;
155  }
156 
157  nodeFIFO.pushBack(new_one);
158  }
159  }
160 
161  GUM_ERROR(NotFound, "no path found");
162  }
163 
164  std::ostream& operator<<(std::ostream& stream, const EdgeGraphPart& set) {
165  stream << set.toString();
166  return stream;
167  }
168 
169 } /* namespace gum */
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1967
const std::vector< NodeId > undirectedPath(const NodeId node1, const NodeId node2) const
returns a possible path from node1 to node2 in the edge set
EdgeGraphPart(Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
EdgeGraphPart & operator=(const EdgeGraphPart &s)
copy operator
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
NodeProperty< NodeSet *> __neighbours
for each node, the set of its adjacent edges
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Classes for undirected edge sets.
Definition: edgeGraphPart.h:72
Generic doubly linked lists.
Definition: list.h:369
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1961
Inline implementation of classes for undirected edge sets.
The class for generic Hash Tables.
Definition: hashTable.h:676
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
virtual void clearEdges()
removes all the edges from the EdgeGraphPart
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
#define GUM_EMIT2(signal, arg1, arg2)
Definition: signaler2.h:40
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1589
virtual ~EdgeGraphPart()
destructor
Signaler2< NodeId, NodeId > onEdgeDeleted
Definition: edgeGraphPart.h:77
const std::string toString() const
to friendly display the content of the EdgeGraphPart
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1828
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:372
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Signaler2< NodeId, NodeId > onEdgeAdded
Definition: edgeGraphPart.h:76
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
some utils for topology : NodeId, Edge, Arc and consorts ...
EdgeSet __edges
the set of all the edges contained within the EdgeGraphPart