aGrUM  0.16.0
arcGraphPart.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  ArcGraphPart::ArcGraphPart(Size arcs_size, bool arcs_resize_policy) :
40  __arcs(arcs_size, arcs_resize_policy) {
41  GUM_CONSTRUCTOR(ArcGraphPart);
42  }
43 
45  GUM_CONS_CPY(ArcGraphPart);
46 
47  // copy the sets of parents
48  const NodeProperty< NodeSet* >& pars = s.__parents;
49  __parents.resize(pars.capacity());
50 
51  for (const auto& elt : pars) {
52  NodeSet* newpar = new NodeSet(*elt.second);
53  __parents.insert(elt.first, newpar);
54  }
55 
56  // copy the sets of children
58  __children.resize(children.capacity());
59 
60  for (const auto& elt : children) {
61  NodeSet* newchildren = new NodeSet(*elt.second);
62  __children.insert(elt.first, newchildren);
63  }
64 
65  // send signals to indicate that there are new arcs
66  if (onArcAdded.hasListener()) {
67  for (const auto& arc : __arcs) {
68  GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
69  }
70  }
71  }
72 
74  GUM_DESTRUCTOR(ArcGraphPart);
75  // be sure to deallocate all the parents and children sets
76  clearArcs();
77  }
78 
80  for (const auto& elt : __parents)
81  delete elt.second;
82 
83  __parents.clear();
84 
85  for (const auto& elt : __children)
86  delete elt.second;
87 
88  __children.clear();
89 
90  // we need this copy only if at least one onArcDeleted listener exists
91  if (onArcDeleted.hasListener()) {
92  ArcSet tmp = __arcs;
93  __arcs.clear();
94 
95  for (const auto& arc : tmp)
96  GUM_EMIT2(onArcDeleted, arc.tail(), arc.head());
97  } else {
98  __arcs.clear();
99  }
100  }
101 
103  // avoid self assignment
104  if (this != &s) {
105  // copy the arcs
106  clearArcs();
107  __arcs = s.__arcs;
108 
109  // copy the sets of parents
110  __parents.resize(s.__parents.capacity());
111 
112  for (const auto& elt : s.__parents) {
113  NodeSet* newpar = new NodeSet(*elt.second);
114  __parents.insert(elt.first, newpar);
115  }
116 
117  // copy the sets of children
118  __children.resize(s.__children.capacity());
119 
120  for (const auto& elt : s.__children) {
121  NodeSet* newchildren = new NodeSet(*elt.second);
122  __children.insert(elt.first, newchildren);
123  }
124 
125  if (onArcAdded.hasListener()) {
126  for (const auto& arc : __arcs) {
127  GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
128  }
129  }
130  }
131 
132  return *this;
133  }
134 
135  const std::string ArcGraphPart::toString() const {
136  std::stringstream s;
137  bool first = true;
138  s << "{";
139 
140  for (const auto& arc : __arcs) {
141  if (first) {
142  first = false;
143  } else {
144  s << ",";
145  }
146 
147  s << arc;
148  }
149 
150  s << "}";
151 
152  return s.str();
153  }
154 
155  const std::vector< NodeId > ArcGraphPart::directedPath(const NodeId n1,
156  const NodeId n2) const {
157  // not recursive version => use a FIFO for simulating the recursion
158  List< NodeId > nodeFIFO;
159  nodeFIFO.pushBack(n2);
160 
161  // mark[node] = successor if visited, else mark[node] does not exist
163  mark.insert(n2, n2);
164 
165  NodeId current;
166 
167  while (!nodeFIFO.empty()) {
168  current = nodeFIFO.front();
169  nodeFIFO.popFront();
170 
171  // check the parents
172 
173  for (const auto new_one : parents(current)) {
174  if (mark.exists(new_one)) // if this node is already marked, do not
175  continue; // check it again
176 
177  mark.insert(new_one, current);
178 
179  if (new_one == n1) {
180  std::vector< NodeId > v;
181 
182  for (current = n1; current != n2; current = mark[current])
183  v.push_back(current);
184 
185  v.push_back(n2);
186 
187  return v;
188  }
189 
190  nodeFIFO.pushBack(new_one);
191  }
192  }
193 
194  GUM_ERROR(NotFound, "no path found");
195  }
196 
197  const std::vector< NodeId >
199  // not recursive version => use a FIFO for simulating the recursion
200  List< NodeId > nodeFIFO;
201  nodeFIFO.pushBack(n2);
202 
203  // mark[node] = successor if visited, else mark[node] does not exist
205  mark.insert(n2, n2);
206 
207  NodeId current;
208 
209  while (!nodeFIFO.empty()) {
210  current = nodeFIFO.front();
211  nodeFIFO.popFront();
212 
213  // check the parents
214  for (const auto new_one : parents(current)) {
215  if (mark.exists(new_one)) // the node has already been visited
216  continue;
217 
218  mark.insert(new_one, current);
219 
220  if (new_one == n1) {
221  std::vector< NodeId > v;
222 
223  for (current = n1; current != n2; current = mark[current])
224  v.push_back(current);
225 
226  v.push_back(n2);
227 
228  return v;
229  }
230 
231  nodeFIFO.pushBack(new_one);
232  }
233 
234  // check the children
235  for (const auto new_one : children(current)) {
236  if (mark.exists(new_one)) // the node has already been visited
237  continue;
238 
239  mark.insert(new_one, current);
240 
241  if (new_one == n1) {
242  std::vector< NodeId > v;
243 
244  for (current = n1; current != n2; current = mark[current])
245  v.push_back(current);
246 
247  v.push_back(n2);
248 
249  return v;
250  }
251 
252  nodeFIFO.pushBack(new_one);
253  }
254  }
255 
256  GUM_ERROR(NotFound, "no path found");
257  }
258 
259  std::ostream& operator<<(std::ostream& stream, const ArcGraphPart& set) {
260  stream << set.toString();
261  return stream;
262  }
263 
264 } /* 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 > directedUnorientedPath(const NodeId node1, const NodeId node2) const
returns an unoriented (directed) path from node1 to node2 in the arc set
const std::string toString() const
to friendly display the content of the ArcGraphPart
ArcGraphPart & operator=(const ArcGraphPart &s)
copy operator
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
void clearArcs()
removes all the arcs from the ArcGraphPart
Classes for directed edge sets.
Definition: arcGraphPart.h:79
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
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
The class for generic Hash Tables.
Definition: hashTable.h:679
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1592
const std::vector< NodeId > directedPath(const NodeId node1, const NodeId node2) const
returns a directed path from node1 to node2 belonging to the set of arcs
NodeProperty< NodeSet *> __parents
for each arc, the sets of its parents
Definition: arcGraphPart.h:272
Size capacity() const noexcept
Returns the number of slots in the &#39;nodes&#39; vector of the hashtable.
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1831
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Signaler2< NodeId, NodeId > onArcAdded
Definition: arcGraphPart.h:83
virtual ~ArcGraphPart()
destructor
NodeProperty< NodeSet *> __children
for each arc, the set of its children
Definition: arcGraphPart.h:275
Signaler2< NodeId, NodeId > onArcDeleted
Definition: arcGraphPart.h:84
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
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Set< Arc > __arcs
the set of all the arcs contained within the ArcGraphPart
Definition: arcGraphPart.h:269
ArcGraphPart(Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
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.