MorphoGraphX  2.0-1-227
Vertex.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 VERTEX_HPP
12 #define VERTEX_HPP
13 
22 #include <Config.hpp>
23 
24 #include <Mangling.hpp>
25 
26 #include <iostream>
27 #include <map>
28 #include <memory>
29 #include <stdint.h>
30 #include <typeinfo>
31 #include <utility>
32 
33 #include <Common.hpp>
34 
35 namespace mgx
36 {
40  typedef intptr_t vertex_identity_t;
41 
45  extern mgx_EXPORT size_t vertex_counter;
46 
58  template <typename VertexContent> class Vertex
59  {
60 
61  protected:
65  struct CountedContent : public VertexContent {
66  CountedContent() : VertexContent(), num(vertex_counter++)
67  {
68  count = 1u; // To be used with tbb::atomic
69  }
70 
72  : VertexContent(copy), count(copy.count), num(vertex_counter++) {}
73 
74  CountedContent(const VertexContent& copy)
75  : VertexContent(copy), num(vertex_counter++)
76  {
77  count = 1u; // To be used with tbb::atomic
78  }
79 
80  public:
84  unsigned int count;
85 
86  size_t num;
87  };
88 
89  public:
94 
98  typedef VertexContent content_t;
99 
103  typedef VertexContent* pointer;
104 
116  Vertex();
117 
136  explicit Vertex(identity_t id); // bad idea to allow implicit conversion
137 
143  Vertex(const Vertex& copy);
144 
148  ~Vertex();
149 
155  VertexContent* operator->() const
156  {
157  return content;
158  }
164  VertexContent& operator*() const {
165  return *content;
166  }
167 
177  Vertex& operator=(const Vertex& other);
178 
179  Vertex& operator=(const identity_t& id);
180 
181  Vertex& operator=(const VertexContent* value);
182 
188  bool operator==(const Vertex& other) const {
189  return id() == other.id();
190  }
196  bool operator!=(const Vertex& other) const {
197  return id() != other.id();
198  }
204  bool operator>(const Vertex& other) const {
205  return id() > other.id();
206  }
212  bool operator<(const Vertex& other) const {
213  return id() < other.id();
214  }
220  bool operator>=(const Vertex& other) const {
221  return id() >= other.id();
222  }
228  bool operator<=(const Vertex& other) const {
229  return id() <= other.id();
230  }
231 
235  bool isNull() const {
236  return content == 0;
237  }
238 
242  identity_t id() const {
243  return (identity_t)content;
244  }
245 
249  size_t num() const {
250  return content->num;
251  }
252 
256  operator bool() const { return content; }
257 
258  static Vertex null;
259 
260  unsigned int count() const
261  {
262  if(content)
263  return content->count;
264  return 0;
265  }
266 
267  protected:
276 
280  void release();
281 
285  void acquire();
286  };
287 
288  template <typename VertexContent> Vertex<VertexContent> Vertex<VertexContent>::null(0);
289 
290  template <typename VertexContent> Vertex<VertexContent>::~Vertex()
291  {
292  this->release();
293  }
294 
295  template <typename VertexContent>
297  {
298  content = new CountedContent();
299  }
300 
301  template <typename VertexContent>
303  : content(reinterpret_cast<CountedContent*>(id))
304  {
305  acquire();
306  }
307 
308  template <typename VertexContent>
310  : content(copy.content)
311  {
312  acquire();
313  }
314 
315  template <typename VertexContent>
317  {
318  if((identity_t)content == id)
319  return *this;
320  this->release();
321  content = reinterpret_cast<CountedContent*>(id);
322  acquire();
323  return *this;
324  }
325 
326  template <typename VertexContent>
328  {
329  const CountedContent* cid = dynamic_cast<const CountedContent*>(id);
330  if(cid != 0)
331  return *this = (const identity_t&)cid;
332  else {
333  return *this = (const identity_t&)(*(new CountedContent(*id)));
334  }
335  }
336 
337  template <typename VertexContent> void Vertex<VertexContent>::acquire()
338  {
339  if(content) {
340  if(content->count == 0)
341  content = 0;
342  else
343  #pragma omp atomic
344  ++(content->count);
345  }
346  }
347 
348  template <typename VertexContent> void Vertex<VertexContent>::release()
349  {
350  if(content) {
351  #pragma omp atomic
352  --(content->count);
353  if(content->count == 0) {
354  delete content;
355  }
356  }
357  }
358 
359  template <typename VertexContent>
361  {
362  if(content == other.content) {
363  return *this;
364  } else
365  this->release();
366  content = other.content;
367  acquire();
368  return *this;
369  }
370 
371  template <typename VertexContent, typename Alloc, typename charT>
372  std::basic_ostream<charT>& operator<<(std::basic_ostream<charT>& ss, const Vertex<VertexContent>& v)
373  {
374  ss << "Vertex<" << demangle(typeid(VertexContent).name()) << ">(" << v.id() << ")";
375  return ss;
376  }
377 }
378 
379 namespace std
380 {
382  template <typename VertexContent>
383  struct hash<mgx::Vertex<VertexContent> >
384  {
385  size_t operator()(const mgx::Vertex<VertexContent> &v) const
386  {
387  hash<size_t> h;
388  return h((size_t)v.id());
389  //return ((size_t)v.id()) >> 2; // Shift to consider memory alignment!
390  }
391  };
393 }
394 
395 
396 #endif // VERTEX_HPP
mgx::Vertex::content
CountedContent * content
Content of the vertex.
Definition: Vertex.hpp:275
mgx::operator<<
std::ostream & operator<<(std::ostream &s, const Colorbar::Position &pos)
mgx::vertex_counter
mgx_EXPORT size_t vertex_counter
Number used to enumerate the vertices of all types.
Common.hpp
mgx::Vertex::content_t
VertexContent content_t
Type of the content of the vertex.
Definition: Vertex.hpp:98
mgx::Vertex::CountedContent
Type of the reference counted content.
Definition: Vertex.hpp:65
mgx::Vertex::operator=
Vertex & operator=(const Vertex &other)
Change the vertex held by the current object.
Definition: Vertex.hpp:360
mgx::Vertex::operator!=
bool operator!=(const Vertex &other) const
Comparison operators.
Definition: Vertex.hpp:196
mgx::Vertex::num
size_t num() const
Return a number unique to each vertex, globally.
Definition: Vertex.hpp:249
mgx::Vertex::operator>
bool operator>(const Vertex &other) const
Comparison operators.
Definition: Vertex.hpp:204
mgx::vertex_identity_t
intptr_t vertex_identity_t
Type of the identifier of a vertex.
Definition: Vertex.hpp:40
mgx::Vertex::isNull
bool isNull() const
Test if a vertex is a null vertex.
Definition: Vertex.hpp:235
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Vertex::acquire
void acquire()
Acquire the current pointer.
Definition: Vertex.hpp:337
Mangling.hpp
mgx::Vertex::null
static Vertex null
Definition: Vertex.hpp:258
mgx::Vertex::~Vertex
~Vertex()
Desctructor.
Definition: Vertex.hpp:290
mgx::Vertex::CountedContent::CountedContent
CountedContent(const VertexContent &copy)
Definition: Vertex.hpp:74
mgx::Vertex::CountedContent::num
size_t num
Definition: Vertex.hpp:86
HASH_NS_EXIT
#define HASH_NS_EXIT
Definition: Common.hpp:14
mgx::Vertex::Vertex
Vertex()
Creates a new vertex with a new content.
Definition: Vertex.hpp:296
mgx::Vertex::operator<
bool operator<(const Vertex &other) const
Comparison operators.
Definition: Vertex.hpp:212
mgx::demangle
mgx_EXPORT std::string demangle(std::string s)
Demangle all the words in the string.
mgx::Vertex::id
identity_t id() const
Return the identifier of a vertex.
Definition: Vertex.hpp:242
mgx::Vertex::release
void release()
Release the current pointer.
Definition: Vertex.hpp:348
HASH_NS_ENTER
#define HASH_NS_ENTER
Definition: Common.hpp:13
mgx::Vertex::operator->
VertexContent * operator->() const
Access to the data.
Definition: Vertex.hpp:155
mgx::Vertex::CountedContent::CountedContent
CountedContent()
Definition: Vertex.hpp:66
mgx::Vertex::operator<=
bool operator<=(const Vertex &other) const
Comparison operators.
Definition: Vertex.hpp:228
mgx::Vertex::operator==
bool operator==(const Vertex &other) const
Comparison operators.
Definition: Vertex.hpp:188
mgx::Vertex::pointer
VertexContent * pointer
Type of the equivalent pointer.
Definition: Vertex.hpp:103
mgx::Vertex::operator>=
bool operator>=(const Vertex &other) const
Comparison operators.
Definition: Vertex.hpp:220
mgx::Vertex::operator*
VertexContent & operator*() const
Access to the data.
Definition: Vertex.hpp:164
mgx::Vertex::CountedContent::CountedContent
CountedContent(const CountedContent &copy)
Definition: Vertex.hpp:71
mgx::Vertex::count
unsigned int count() const
Definition: Vertex.hpp:260
std::hash< mgx::Vertex< VertexContent > >::operator()
size_t operator()(const mgx::Vertex< VertexContent > &v) const
Definition: Vertex.hpp:385
mgx::Vertex::identity_t
vertex_identity_t identity_t
Type of the identifier of the vertex.
Definition: Vertex.hpp:93
mgx::Vertex::CountedContent::count
unsigned int count
Reference count on the vertex.
Definition: Vertex.hpp:84
mgx::Vertex
Definition: Vertex.hpp:58