MorphoGraphX  2.0-1-227
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 
20 #include <stdint.h>
21 #include <QtCore/QHash>
22 
23 #include <Config.hpp>
24 #include <Common.hpp>
25 
26 
27 namespace mgx
28 {
32  typedef intptr_t edge_identity_t;
33 
42  template <typename EdgeContent> class Edge {
43  public:
48 
52  typedef EdgeContent content_t;
53 
57  typedef EdgeContent* pointer;
58 
62  Edge();
63 
73  Edge(identity_t src, identity_t tgt, EdgeContent* content);
74 
78  Edge(const Edge& copy);
79 
86  EdgeContent* operator->() const {
87  return _content;
88  }
89 
96  EdgeContent& operator*() const {
97  return *_content;
98  }
99 
103  template <typename R> R& operator->*(R EdgeContent::*ptr) {
104  return _content->*ptr;
105  }
109  template <typename R> const R& operator->*(R EdgeContent::*ptr) const {
110  return _content->*ptr;
111  }
112 
116  Edge& operator=(const Edge& other);
117 
123  bool operator==(const Edge& other) const {
124  return _content == other._content;
125  }
131  bool operator!=(const Edge& other) const {
132  return _content != other._content;
133  }
139  bool operator>(const Edge& other) const {
140  return _content > other._content;
141  }
147  bool operator<(const Edge& other) const {
148  return _content < other._content;
149  }
150 
154  bool isNull() const {
155  return _content == 0;
156  }
157 
161  operator bool() const { return _content != 0; }
162 
169  identity_t source() const {
170  return _source;
171  }
178  identity_t target() const {
179  return _target;
180  }
181 
185  void clear()
186  {
187  _source = 0;
188  _target = 0;
189  _content = 0;
190  }
191 
192  static Edge null;
193 
194  protected:
206  EdgeContent* _content;
207  };
208 
209  template <typename EdgeContent> Edge<EdgeContent> Edge<EdgeContent>::null;
210 
211  template <typename EdgeContent>
213  : _source(0)
214  , _target(0)
215  , _content(0)
216  {
217  }
218 
219  template <typename EdgeContent>
221  : _source(copy._source)
222  , _target(copy._target)
223  , _content(copy._content)
224  {
225  }
226 
227  template <typename EdgeContent>
228  Edge<EdgeContent>::Edge(identity_t src, identity_t tgt, EdgeContent* content)
229  : _source(src)
230  , _target(tgt)
231  , _content(content)
232  {
233  }
234 
235  template <typename EdgeContent> Edge<EdgeContent>& Edge<EdgeContent>::operator=(const Edge<EdgeContent>& other)
236  {
237  _source = other._source;
238  _target = other._target;
239  _content = other._content;
240  return *this;
241  }
242 
247  template <typename T> void copy_symmetric(T& e2, const T& e1) {
248  e2 = e1;
249  }
250 
260  template <typename EdgeContent> struct Arc {
265 
266  typedef EdgeContent content_t;
267 
269 
273  Arc();
274 
278  Arc(const Arc& copy);
279 
283  Arc(identity_t src, identity_t tgt, EdgeContent* c1, EdgeContent* c2);
284 
288  operator edge_t() const { return edge_t(_source, _target, main); }
289 
293  Arc inv() const;
294 
298  Arc operator-() const {
299  return inv();
300  }
301 
305  ~Arc() {
306  sync();
307  }
308 
312  bool isNull() const {
313  return main == 0;
314  }
315 
319  operator bool() const { return main != 0; }
320 
324  EdgeContent& operator*() const {
325  return *main;
326  }
330  EdgeContent* operator->() const {
331  return main;
332  }
333 
340  identity_t source() const {
341  return _source;
342  }
349  identity_t target() const {
350  return _target;
351  }
352 
356  void sync() const
357  {
358  if(main)
360  }
361 
362  protected:
364  mutable EdgeContent* main, *other;
365  };
366 
367  template <typename EdgeContent>
369  : _source(0)
370  , _target(0)
371  , main(0)
372  , other(0)
373  {
374  }
375 
376  template <typename EdgeContent>
377  Arc<EdgeContent>::Arc(identity_t src, identity_t tgt, EdgeContent* e1, EdgeContent* e2)
378  : _source(src)
379  , _target(tgt)
380  , main(e1)
381  , other(e2)
382  {
383  }
384 
385  template <typename EdgeContent>
387  : _source(copy._source)
388  , _target(copy._target)
389  , main(copy.main)
390  , other(copy.other)
391  {
392  }
393 
394  template <typename EdgeContent> Arc<EdgeContent> Arc<EdgeContent>::inv() const
395  {
396  sync();
397  return Arc(_target, _source, other, main);
398  }
399 }
400 
401 namespace std
402 {
404  template <typename EdgeContent> struct hash<mgx::Edge<EdgeContent> > {
405  size_t operator()(const mgx::Edge<EdgeContent>& e) const {
406  return e.source() + e.target();
407  }
408  };
410 }
411 
412 template <typename EdgeContent> uint qHash(const mgx::Edge<EdgeContent>& e)
413 {
414  return qHash(uint(e.source() >> 4)) ^ qHash(uint(e.target() >> 4));
415 }
416 
417 #endif
std::hash< mgx::Edge< EdgeContent > >::operator()
size_t operator()(const mgx::Edge< EdgeContent > &e) const
Definition: Edge.hpp:405
mgx::Arc::content_t
EdgeContent content_t
Definition: Edge.hpp:266
Common.hpp
mgx::Arc::operator->
EdgeContent * operator->() const
Reference the content of the arc as a pointer.
Definition: Edge.hpp:330
mgx::Edge::target
identity_t target() const
Returns the identifier of the target of the edge.
Definition: Edge.hpp:178
mgx::edge_identity_t
intptr_t edge_identity_t
Type of the identifier of an edge.
Definition: Edge.hpp:32
mgx::Edge::content_t
EdgeContent content_t
Type of the content of the edge.
Definition: Edge.hpp:52
mgx::Edge::Edge
Edge()
Creates a null edge.
Definition: Edge.hpp:212
mgx::Edge::source
identity_t source() const
Returns the identifier of the source of the edge.
Definition: Edge.hpp:169
mgx::Edge::operator*
EdgeContent & operator*() const
Data access.
Definition: Edge.hpp:96
mgx::Edge
Definition: Edge.hpp:42
mgx::Edge::operator->
EdgeContent * operator->() const
Data access.
Definition: Edge.hpp:86
mgx::Arc::main
EdgeContent * main
Definition: Edge.hpp:364
mgx::Edge::operator==
bool operator==(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:123
uint
unsigned int uint
Definition: MorphoGraphX.hpp:14
mgx::Edge::operator->*
const R & operator->*(R EdgeContent::*ptr) const
Constant access to the data via pointer to member.
Definition: Edge.hpp:109
mgx::c2
CU_HOST_DEVICE const const T T c2
Definition: Geometry.hpp:234
mgx::Edge::operator!=
bool operator!=(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:131
mgx::Edge::clear
void clear()
Reset an edge weak pointer to null.
Definition: Edge.hpp:185
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::copy_symmetric
void copy_symmetric(T &e2, const T &e1)
Default copy of the arc uses the = operator.
Definition: Edge.hpp:247
mgx::Edge::operator->*
R & operator->*(R EdgeContent::*ptr)
Access to the data via pointer to member.
Definition: Edge.hpp:103
mgx::Edge::_source
identity_t _source
Identity of the source of the edge.
Definition: Edge.hpp:198
mgx::Edge::identity_t
edge_identity_t identity_t
Type of the identity of a vertex.
Definition: Edge.hpp:47
HASH_NS_EXIT
#define HASH_NS_EXIT
Definition: Common.hpp:14
mgx::Arc::isNull
bool isNull() const
Test if the content is null.
Definition: Edge.hpp:312
mgx::Edge::operator=
Edge & operator=(const Edge &other)
Change the reference help by the object.
Definition: Edge.hpp:235
mgx::Arc::source
identity_t source() const
Returns the identifier of the source of the edge.
Definition: Edge.hpp:340
mgx::Arc::edge_t
Edge< EdgeContent > edge_t
Definition: Edge.hpp:268
mgx::Edge::_target
identity_t _target
Identity of the target of the edge.
Definition: Edge.hpp:202
mgx::c1
CU_HOST_DEVICE const const T c1
Definition: Geometry.hpp:234
mgx::Arc::identity_t
edge_identity_t identity_t
Type of the identity of a vertex.
Definition: Edge.hpp:264
mgx::Arc::inv
Arc inv() const
Synchronize the edges and returns the opposite arc.
Definition: Edge.hpp:394
mgx::Arc::operator-
Arc operator-() const
Unary '-' operator synchronize edges and returns opposite arc.
Definition: Edge.hpp:298
HASH_NS_ENTER
#define HASH_NS_ENTER
Definition: Common.hpp:13
mgx::Arc::Arc
Arc()
Cosntruct an empty (null) arc.
Definition: Edge.hpp:368
mgx::Arc::~Arc
~Arc()
Destroy and copy the content of the arcs.
Definition: Edge.hpp:305
mgx::Edge::isNull
bool isNull() const
Test if an edge is null.
Definition: Edge.hpp:154
mgx::Edge::_content
EdgeContent * _content
Content of the edge.
Definition: Edge.hpp:206
mgx::Arc::_target
identity_t _target
Definition: Edge.hpp:363
mgx::Arc::sync
void sync() const
Synchronize both sides of the arc.
Definition: Edge.hpp:356
mgx::Arc::other
EdgeContent * other
Definition: Edge.hpp:364
mgx::Arc::_source
identity_t _source
Definition: Edge.hpp:363
mgx::Arc::target
identity_t target() const
Returns the identifier of the target of the edge.
Definition: Edge.hpp:349
mgx::Arc::operator*
EdgeContent & operator*() const
Reference the content of the arc.
Definition: Edge.hpp:324
mgx::Edge::pointer
EdgeContent * pointer
Type of the equivalent pointer.
Definition: Edge.hpp:57
mgx::Edge::operator<
bool operator<(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:147
qHash
uint qHash(const mgx::Edge< EdgeContent > &e)
Definition: Edge.hpp:412
mgx::Arc
Type of a undirected edge (or arc)
Definition: Edge.hpp:260
mgx::Edge::operator>
bool operator>(const Edge &other) const
Comparison operators.
Definition: Edge.hpp:139