MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Edge.hpp
Go to the documentation of this file.
1 #ifndef EDGE_H
2 #define EDGE_H
3 
4 #include <Config.hpp>
5 
12 #include <stdint.h>
13 #include <QtCore/QHash>
14 #include <UnorderedMap.hpp>
15 
16 namespace std {
17 using namespace tr1;
18 } // namespace std
19 
20 namespace mgx {
21 namespace graph {
25 typedef intptr_t edge_identity_t;
26 
35 template <typename EdgeContent> class Edge {
36 public:
40  typedef edge_identity_t identity_t;
41 
45  typedef EdgeContent content_t;
46 
50  typedef EdgeContent* pointer;
51 
55  Edge();
56 
66  Edge(identity_t src, identity_t tgt, EdgeContent* content);
67 
71  Edge(const Edge& copy);
72 
79  EdgeContent* operator->() const {
80  return _content;
81  }
82 
89  EdgeContent& operator*() const {
90  return *_content;
91  }
92 
96  template <typename R> R& operator->*(R EdgeContent::*ptr) {
97  return _content->*ptr;
98  }
102  template <typename R> const R& operator->*(R EdgeContent::*ptr) const {
103  return _content->*ptr;
104  }
105 
109  Edge& operator=(const Edge& other);
110 
116  bool operator==(const Edge& other) const {
117  return _content == other._content;
118  }
124  bool operator!=(const Edge& other) const {
125  return _content != other._content;
126  }
132  bool operator>(const Edge& other) const {
133  return _content > other._content;
134  }
140  bool operator<(const Edge& other) const {
141  return _content < other._content;
142  }
143 
147  bool isNull() const {
148  return _content == 0;
149  }
150 
154  operator bool() const { return _content != 0; }
155 
162  identity_t source() const {
163  return _source;
164  }
171  identity_t target() const {
172  return _target;
173  }
174 
178  void clear()
179  {
180  _source = 0;
181  _target = 0;
182  _content = 0;
183  }
184 
185  static Edge null;
186 
187 protected:
199  EdgeContent* _content;
200 };
201 
202 template <typename EdgeContent> Edge<EdgeContent> Edge<EdgeContent>::null;
203 
204 template <typename EdgeContent>
206  : _source(0)
207  , _target(0)
208  , _content(0)
209 {
210 }
211 
212 template <typename EdgeContent>
214  : _source(copy._source)
215  , _target(copy._target)
216  , _content(copy._content)
217 {
218 }
219 
220 template <typename EdgeContent>
221 Edge<EdgeContent>::Edge(identity_t src, identity_t tgt, EdgeContent* content)
222  : _source(src)
223  , _target(tgt)
224  , _content(content)
225 {
226 }
227 
228 template <typename EdgeContent> Edge<EdgeContent>& Edge<EdgeContent>::operator=(const Edge<EdgeContent>& other)
229 {
230  _source = other._source;
231  _target = other._target;
232  _content = other._content;
233  return *this;
234 }
235 
240 template <typename T> void copy_symmetric(T& e2, const T& e1) {
241  e2 = e1;
242 }
243 
253 template <typename EdgeContent> struct Arc {
257  typedef edge_identity_t identity_t;
258 
259  typedef EdgeContent content_t;
260 
261  typedef Edge<EdgeContent> edge_t;
262 
266  Arc();
267 
271  Arc(const Arc& copy);
272 
276  Arc(identity_t src, identity_t tgt, EdgeContent* c1, EdgeContent* c2);
277 
281  operator edge_t() const { return edge_t(_source, _target, main); }
282 
286  Arc inv() const;
287 
291  Arc operator-() const {
292  return inv();
293  }
294 
298  ~Arc() {
299  sync();
300  }
301 
305  bool isNull() const {
306  return main == 0;
307  }
308 
312  operator bool() const { return main != 0; }
313 
317  EdgeContent& operator*() const {
318  return *main;
319  }
323  EdgeContent* operator->() const {
324  return main;
325  }
326 
333  identity_t source() const {
334  return _source;
335  }
342  identity_t target() const {
343  return _target;
344  }
345 
349  void sync() const
350  {
351  if(main)
352  copy_symmetric(*other, *main);
353  }
354 
355 protected:
356  identity_t _source, _target;
357  mutable EdgeContent* main, *other;
358 };
359 
360 template <typename EdgeContent>
362  : _source(0)
363  , _target(0)
364  , main(0)
365  , other(0)
366 {
367 }
368 
369 template <typename EdgeContent>
370 Arc<EdgeContent>::Arc(identity_t src, identity_t tgt, EdgeContent* e1, EdgeContent* e2)
371  : _source(src)
372  , _target(tgt)
373  , main(e1)
374  , other(e2)
375 {
376 }
377 
378 template <typename EdgeContent>
380  : _source(copy._source)
381  , _target(copy._target)
382  , main(copy.main)
383  , other(copy.other)
384 {
385 }
386 
387 template <typename EdgeContent> Arc<EdgeContent> Arc<EdgeContent>::inv() const
388 {
389  sync();
390  return Arc(_target, _source, other, main);
391 }
392 } // namespace graph
393 } // namespace mgx
394 
395 namespace std {
396 namespace tr1 {
397 template <typename EdgeContent> struct hash<mgx::graph::Edge<EdgeContent> > {
398  size_t operator()(const mgx::graph::Edge<EdgeContent>& e) const {
399  return e.source() + e.target();
400  }
401 };
402 } // namespace tr1
403 } // namespace std
404 
405 template <typename EdgeContent> uint qHash(const mgx::graph::Edge<EdgeContent>& e)
406 {
407  return qHash(uint(e.source() >> 4)) ^ qHash(uint(e.target() >> 4));
408 }
409 
410 #endif
edge_identity_t identity_t
Type of the identity of a vertex.
Definition: Edge.hpp:40
bool operator<(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:140
bool isNull() const
Test if an edge is null.
Definition: Edge.hpp:147
R & operator->*(R EdgeContent::*ptr)
Access to the data via pointer to member.
Definition: Edge.hpp:96
EdgeContent content_t
Type of the content of the edge.
Definition: Edge.hpp:45
void sync() const
Synchronize both sides of the arc.
Definition: Edge.hpp:349
Edge of a vv graph.
Definition: Edge.hpp:35
bool operator==(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:116
identity_t _source
Identity of the source of the edge.
Definition: Edge.hpp:191
Type of a undirected edge (or arc)
Definition: Edge.hpp:253
EdgeContent * operator->() const
Data access.
Definition: Edge.hpp:79
identity_t source() const
Returns the identifier of the source of the edge.
Definition: Edge.hpp:162
~Arc()
Destroy and copy the content of the arcs.
Definition: Edge.hpp:298
edge_identity_t identity_t
Type of the identity of a vertex.
Definition: Edge.hpp:257
EdgeContent & operator*() const
Reference the content of the arc.
Definition: Edge.hpp:317
identity_t _target
Identity of the target of the edge.
Definition: Edge.hpp:195
identity_t source() const
Returns the identifier of the source of the edge.
Definition: Edge.hpp:333
Edge & operator=(const Edge &other)
Change the reference help by the object.
Definition: Edge.hpp:228
EdgeContent * _content
Content of the edge.
Definition: Edge.hpp:199
Arc()
Cosntruct an empty (null) arc.
Definition: Edge.hpp:361
EdgeContent * pointer
Type of the equivalent pointer.
Definition: Edge.hpp:50
const R & operator->*(R EdgeContent::*ptr) const
Constant access to the data via pointer to member.
Definition: Edge.hpp:102
Arc operator-() const
Unary '-' operator synchronize edges and returns opposite arc.
Definition: Edge.hpp:291
EdgeContent & operator*() const
Data access.
Definition: Edge.hpp:89
identity_t target() const
Returns the identifier of the target of the edge.
Definition: Edge.hpp:171
void clear()
Reset an edge weak pointer to null.
Definition: Edge.hpp:178
bool operator>(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:132
bool isNull() const
Test if the content is null.
Definition: Edge.hpp:305
Edge()
Creates a null edge.
Definition: Edge.hpp:205
identity_t target() const
Returns the identifier of the target of the edge.
Definition: Edge.hpp:342
bool operator!=(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:124
Arc inv() const
Synchronize the edges and returns the opposite arc.
Definition: Edge.hpp:387
EdgeContent * operator->() const
Reference the content of the arc as a pointer.
Definition: Edge.hpp:323