aGrUM  0.16.0
edgeGraphPart.cpp
Go to the documentation of this file.
1 
30 
31 #ifdef GUM_NO_INLINE
33 #endif // GUM_NOINLINE
35 
36 namespace gum {
37 
39  EdgeGraphPart::EdgeGraphPart(Size edges_size, bool edges_resize_policy) :
40  __edges(edges_size, edges_resize_policy) {
41  GUM_CONSTRUCTOR(EdgeGraphPart);
42  }
43 
45  GUM_CONS_CPY(EdgeGraphPart);
46 
47  // copy the set of neighbours
48  __neighbours.resize(s.__neighbours.capacity());
49 
50  for (const auto& elt : s.__neighbours) {
51  NodeSet* newneigh = new NodeSet(*elt.second);
52  __neighbours.insert(elt.first, newneigh);
53  }
54 
55  // send signals to indicate that there are new edges
56  if (onEdgeAdded.hasListener())
57  for (const auto& edge : __edges)
58  GUM_EMIT2(onEdgeAdded, edge.first(), edge.second());
59  }
60 
62  GUM_DESTRUCTOR(EdgeGraphPart);
63  // be sure to deallocate all the neighbours sets
64  clearEdges();
65  }
66 
68  for (const auto& elt : __neighbours)
69  delete elt.second;
70 
71  __neighbours.clear();
72 
73  if (onEdgeDeleted.hasListener()) {
74  EdgeSet tmp = __edges;
75  __edges.clear();
76 
77  for (const auto& edge : tmp)
78  GUM_EMIT2(onEdgeDeleted, edge.first(), edge.second());
79  } else {
80  __edges.clear();
81  }
82  }
83 
85  // avoid self assignment
86  if (this != &s) {
87  clearEdges();
88 
89  __edges = s.__edges;
90 
91  // copy the set of neighbours
92  __neighbours.resize(s.__neighbours.capacity());
93 
94  for (const auto& elt : s.__neighbours) {
95  NodeSet* newneigh = new NodeSet(*elt.second);
96  __neighbours.insert(elt.first, newneigh);
97  }
98 
99  if (onEdgeAdded.hasListener())
100  for (const auto& edge : __edges)
101  GUM_EMIT2(onEdgeAdded, edge.first(), edge.second());
102  }
103 
104  return *this;
105  }
106 
107  const std::string EdgeGraphPart::toString() const {
108  std::stringstream s;
109  bool first = true;
110  s << "{";
111 
112  for (const auto edge : __edges) {
113  if (first)
114  first = false;
115  else
116  s << ",";
117 
118  s << edge;
119  }
120 
121  s << "}";
122 
123  return s.str();
124  }
125 
126  const std::vector< NodeId >
127  EdgeGraphPart::undirectedPath(const NodeId n1, const NodeId n2) const {
128  // not recursive version => use a FIFO for simulating the recursion
129  List< NodeId > nodeFIFO;
130  nodeFIFO.pushBack(n2);
131 
132  // mark[node] = predecessor if visited, else mark[node] does not exist
134  mark.insert(n2, n2);
135 
136  NodeId current;
137 
138  while (!nodeFIFO.empty()) {
139  current = nodeFIFO.front();
140  nodeFIFO.popFront();
141 
142  // check the neighbour
143  for (const auto new_one : neighbours(current)) {
144  if (mark.exists(new_one)) // if this node is already marked, stop
145  continue;
146 
147  mark.insert(new_one, current);
148 
149  if (new_one == n1) {
150  std::vector< NodeId > v;
151 
152  for (current = n1; current != n2; current = mark[current])
153  v.push_back(current);
154 
155  v.push_back(n2);
156 
157  return v;
158  }
159 
160  nodeFIFO.pushBack(new_one);
161  }
162  }
163 
164  GUM_ERROR(NotFound, "no path found");
165  }
166 
167  std::ostream& operator<<(std::ostream& stream, const EdgeGraphPart& set) {
168  stream << set.toString();
169  return stream;
170  }
171 
172 } /* namespace gum */
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1970
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:75
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1964
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The class for generic Hash Tables.
Definition: hashTable.h:679
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:165
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:605
#define GUM_EMIT2(signal, arg1, arg2)
Definition: signaler2.h:42
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1592
virtual ~EdgeGraphPart()
destructor
Signaler2< NodeId, NodeId > onEdgeDeleted
Definition: edgeGraphPart.h:80
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:1831
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Signaler2< NodeId, NodeId > onEdgeAdded
Definition: edgeGraphPart.h:79
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
EdgeSet __edges
the set of all the edges contained within the EdgeGraphPart