MorphoGraphX
Edge.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef EDGE_H
12 #define EDGE_H
13 
14 #include <Config.hpp>
15 
22 #include <stdint.h>
23 #include <QtCore/QHash>
24 #include <UnorderedMap.hpp>
25 
26 namespace std {
27 using namespace tr1;
28 } // namespace std
29 
30 namespace mgx {
31 namespace graph {
35 typedef intptr_t edge_identity_t;
36 
45 template <typename EdgeContent> class Edge {
46 public:
50  typedef edge_identity_t identity_t;
51 
55  typedef EdgeContent content_t;
56 
60  typedef EdgeContent* pointer;
61 
65  Edge();
66 
76  Edge(identity_t src, identity_t tgt, EdgeContent* content);
77 
81  Edge(const Edge& copy);
82 
89  EdgeContent* operator->() const {
90  return _content;
91  }
92 
99  EdgeContent& operator*() const {
100  return *_content;
101  }
102 
106  template <typename R> R& operator->*(R EdgeContent::*ptr) {
107  return _content->*ptr;
108  }
112  template <typename R> const R& operator->*(R EdgeContent::*ptr) const {
113  return _content->*ptr;
114  }
115 
119  Edge& operator=(const Edge& other);
120 
126  bool operator==(const Edge& other) const {
127  return _content == other._content;
128  }
134  bool operator!=(const Edge& other) const {
135  return _content != other._content;
136  }
142  bool operator>(const Edge& other) const {
143  return _content > other._content;
144  }
150  bool operator<(const Edge& other) const {
151  return _content < other._content;
152  }
153 
157  bool isNull() const {
158  return _content == 0;
159  }
160 
164  operator bool() const { return _content != 0; }
165 
172  identity_t source() const {
173  return _source;
174  }
181  identity_t target() const {
182  return _target;
183  }
184 
188  void clear()
189  {
190  _source = 0;
191  _target = 0;
192  _content = 0;
193  }
194 
195  static Edge null;
196 
197 protected:
201  identity_t _source;
205  identity_t _target;
209  EdgeContent* _content;
210 };
211 
212 template <typename EdgeContent> Edge<EdgeContent> Edge<EdgeContent>::null;
213 
214 template <typename EdgeContent>
216  : _source(0)
217  , _target(0)
218  , _content(0)
219 {
220 }
221 
222 template <typename EdgeContent>
224  : _source(copy._source)
225  , _target(copy._target)
226  , _content(copy._content)
227 {
228 }
229 
230 template <typename EdgeContent>
231 Edge<EdgeContent>::Edge(identity_t src, identity_t tgt, EdgeContent* content)
232  : _source(src)
233  , _target(tgt)
234  , _content(content)
235 {
236 }
237 
238 template <typename EdgeContent> Edge<EdgeContent>& Edge<EdgeContent>::operator=(const Edge<EdgeContent>& other)
239 {
240  _source = other._source;
241  _target = other._target;
242  _content = other._content;
243  return *this;
244 }
245 
250 template <typename T> void copy_symmetric(T& e2, const T& e1) {
251  e2 = e1;
252 }
253 
263 template <typename EdgeContent> struct Arc {
267  typedef edge_identity_t identity_t;
268 
269  typedef EdgeContent content_t;
270 
271  typedef Edge<EdgeContent> edge_t;
272 
276  Arc();
277 
281  Arc(const Arc& copy);
282 
286  Arc(identity_t src, identity_t tgt, EdgeContent* c1, EdgeContent* c2);
287 
291  operator edge_t() const { return edge_t(_source, _target, main); }
292 
296  Arc inv() const;
297 
301  Arc operator-() const {
302  return inv();
303  }
304 
308  ~Arc() {
309  sync();
310  }
311 
315  bool isNull() const {
316  return main == 0;
317  }
318 
322  operator bool() const { return main != 0; }
323 
327  EdgeContent& operator*() const {
328  return *main;
329  }
333  EdgeContent* operator->() const {
334  return main;
335  }
336 
343  identity_t source() const {
344  return _source;
345  }
352  identity_t target() const {
353  return _target;
354  }
355 
359  void sync() const
360  {
361  if(main)
362  copy_symmetric(*other, *main);
363  }
364 
365 protected:
366  identity_t _source, _target;
367  mutable EdgeContent* main, *other;
368 };
369 
370 template <typename EdgeContent>
372  : _source(0)
373  , _target(0)
374  , main(0)
375  , other(0)
376 {
377 }
378 
379 template <typename EdgeContent>
380 Arc<EdgeContent>::Arc(identity_t src, identity_t tgt, EdgeContent* e1, EdgeContent* e2)
381  : _source(src)
382  , _target(tgt)
383  , main(e1)
384  , other(e2)
385 {
386 }
387 
388 template <typename EdgeContent>
390  : _source(copy._source)
391  , _target(copy._target)
392  , main(copy.main)
393  , other(copy.other)
394 {
395 }
396 
397 template <typename EdgeContent> Arc<EdgeContent> Arc<EdgeContent>::inv() const
398 {
399  sync();
400  return Arc(_target, _source, other, main);
401 }
402 } // namespace graph
403 } // namespace mgx
404 
405 namespace std {
406 namespace tr1 {
407 template <typename EdgeContent> struct hash<mgx::graph::Edge<EdgeContent> > {
408  size_t operator()(const mgx::graph::Edge<EdgeContent>& e) const {
409  return e.source() + e.target();
410  }
411 };
412 } // namespace tr1
413 } // namespace std
414 
415 template <typename EdgeContent> uint qHash(const mgx::graph::Edge<EdgeContent>& e)
416 {
417  return qHash(uint(e.source() >> 4)) ^ qHash(uint(e.target() >> 4));
418 }
419 
420 #endif
edge_identity_t identity_t
Type of the identity of a vertex.
Definition: Edge.hpp:50
bool operator<(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:150
bool isNull() const
Test if an edge is null.
Definition: Edge.hpp:157
R & operator->*(R EdgeContent::*ptr)
Access to the data via pointer to member.
Definition: Edge.hpp:106
EdgeContent content_t
Type of the content of the edge.
Definition: Edge.hpp:55
void sync() const
Synchronize both sides of the arc.
Definition: Edge.hpp:359
Edge of a vv graph.
Definition: Edge.hpp:45
bool operator==(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:126
identity_t _source
Identity of the source of the edge.
Definition: Edge.hpp:201
Type of a undirected edge (or arc)
Definition: Edge.hpp:263
EdgeContent * operator->() const
Data access.
Definition: Edge.hpp:89
STL namespace.
identity_t source() const
Returns the identifier of the source of the edge.
Definition: Edge.hpp:172
~Arc()
Destroy and copy the content of the arcs.
Definition: Edge.hpp:308
edge_identity_t identity_t
Type of the identity of a vertex.
Definition: Edge.hpp:267
EdgeContent & operator*() const
Reference the content of the arc.
Definition: Edge.hpp:327
identity_t _target
Identity of the target of the edge.
Definition: Edge.hpp:205
identity_t source() const
Returns the identifier of the source of the edge.
Definition: Edge.hpp:343
Edge & operator=(const Edge &other)
Change the reference help by the object.
Definition: Edge.hpp:238
EdgeContent * _content
Content of the edge.
Definition: Edge.hpp:209
Arc()
Cosntruct an empty (null) arc.
Definition: Edge.hpp:371
EdgeContent * pointer
Type of the equivalent pointer.
Definition: Edge.hpp:60
const R & operator->*(R EdgeContent::*ptr) const
Constant access to the data via pointer to member.
Definition: Edge.hpp:112
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Arc operator-() const
Unary &#39;-&#39; operator synchronize edges and returns opposite arc.
Definition: Edge.hpp:301
void copy_symmetric(T &e2, const T &e1)
Default copy of the arc uses the = operator.
Definition: Edge.hpp:250
EdgeContent & operator*() const
Data access.
Definition: Edge.hpp:99
identity_t target() const
Returns the identifier of the target of the edge.
Definition: Edge.hpp:181
void clear()
Reset an edge weak pointer to null.
Definition: Edge.hpp:188
bool operator>(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:142
bool isNull() const
Test if the content is null.
Definition: Edge.hpp:315
Edge()
Creates a null edge.
Definition: Edge.hpp:215
intptr_t edge_identity_t
Type of the identifier of an edge.
Definition: Edge.hpp:35
identity_t target() const
Returns the identifier of the target of the edge.
Definition: Edge.hpp:352
bool operator!=(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:134
Arc inv() const
Synchronize the edges and returns the opposite arc.
Definition: Edge.hpp:397
EdgeContent * operator->() const
Reference the content of the arc as a pointer.
Definition: Edge.hpp:333