Tulip  5.1.0
Large graphs analysis and drawing
StaticProperty.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 #ifndef STATICPROPERTY_H
20 #define STATICPROPERTY_H
21 
22 #ifdef _OPENMP
23 #include <omp.h>
24 #endif
25 #include <tulip/Graph.h>
26 
27 namespace tlp {
28 template <typename TYPE>
29 class NodeStaticProperty :public std::vector<TYPE> {
30  const Graph* graph;
31 public:
32 
33  // constructor
34  NodeStaticProperty(const Graph *g) :graph(g) {
35  assert(g);
36  // set the vector size to the number of graph nodes
37  this->resize(graph->numberOfNodes());
38  }
39 
40  // get the stored value of a node
41  inline typename std::vector<TYPE>::const_reference getNodeValue(node n) const {
42  return (*this)[graph->nodePos(n)];
43  }
44 
45  // set the stored value of a node
46  inline typename std::vector<TYPE>::const_reference setNodeValue(node n,
47  TYPE val) {
48  return (*this)[graph->nodePos(n)] = val;
49  }
50 
51  // set all to same values
52  void setAll(const TYPE &val) {
53 #ifdef _OPENMP
54  #pragma omp parallel for
55 #endif
56 
57  for (OMP_ITER_TYPE i = 0; i < static_cast<OMP_ITER_TYPE>(this->size()); ++i)
58  (*this)[i] = val;
59  }
60 
61  // add a value for a newly created node
62  void addNodeValue(node n, TYPE val) {
63  unsigned int nPos = graph->nodePos(n);
64 
65  if (nPos + 1 > this->size())
66  this->resize(nPos + 1);
67 
68  setNodeValue(n, val);
69  }
70 
71  // get values from a typed instance of PropertyInterface
72  template<typename PROP_PTR>
73  void copyFromProperty(PROP_PTR prop) {
74  const std::vector<node>& nodes = graph->nodes();
75  unsigned int nbNodes = nodes.size();
76 #ifdef _OPENMP
77  #pragma omp parallel for
78 #endif
79 
80  for (OMP_ITER_TYPE i = 0; i < nbNodes; ++i)
81  (*this)[i] = prop->getNodeValue(nodes[i]);
82  }
83 
84  // copy values into a typed typed instance of PropertyInterface
85  template<typename PROP_PTR>
86  void copyToProperty(PROP_PTR prop) {
87  const std::vector<node>& nodes = graph->nodes();
88  unsigned int nbNodes = nodes.size();
89 
90  for (unsigned int i = 0; i < nbNodes; ++i)
91  prop->setNodeValue(nodes[i], (*this)[i]);
92  }
93 };
94 
95 // vector<bool> does not support // update
96 // so do an iterative specialization
97 template<> template<typename PROP_PTR>
98 void NodeStaticProperty<bool>::copyFromProperty(PROP_PTR prop) {
99  const std::vector<node>& nodes = graph->nodes();
100  unsigned int nbNodes = nodes.size();
101 
102  for (unsigned int i = 0; i < nbNodes; ++i)
103  (*this)[i] = prop->getNodeValue(nodes[i]);
104 }
105 
106 template <typename TYPE>
107 class EdgeStaticProperty :public std::vector<TYPE> {
108  const Graph* graph;
109 public:
110 
111  // constructor
112  EdgeStaticProperty(const Graph *g) :graph(g) {
113  assert(g);
114  // set the vector size to the number of graph edges
115  this->resize(graph->numberOfEdges());
116  }
117 
118  // get the stored value of a edge
119  inline typename std::vector<TYPE>::const_reference getEdgeValue(edge n) const {
120  return (*this)[graph->edgePos(n)];
121  }
122 
123  // set the stored value of a edge
124  inline typename std::vector<TYPE>::const_reference setEdgeValue(edge n,
125  TYPE val) {
126  return (*this)[graph->edgePos(n)] = val;
127  }
128 
129  void setAll(const TYPE &val) {
130 #ifdef _OPENMP
131  #pragma omp parallel for
132 #endif
133 
134  for (OMP_ITER_TYPE i = 0; i < static_cast<OMP_ITER_TYPE>(this->size()); ++i)
135  (*this)[i] = val;
136  }
137 
138  // add a value for a newly created edge
139  void addEdgeValue(edge n, TYPE val) {
140  unsigned int nPos = graph->edgePos(n);
141 
142  if (nPos + 1 > this->size())
143  this->resize(nPos + 1);
144 
145  setEdgeValue(n, val);
146  }
147 
148  // get values from a typed instance of PropertyInterface
149  template<typename PROP_PTR>
150  void copyFromProperty(PROP_PTR prop) {
151  const std::vector<edge>& edges = graph->edges();
152  unsigned int nbEdges = edges.size();
153 #ifdef _OPENMP
154  #pragma omp parallel for
155 #endif
156 
157  for (OMP_ITER_TYPE i = 0; i < nbEdges; ++i)
158  (*this)[i] = prop->getEdgeValue(edges[i]);
159  }
160 
161  // copy values into a typed typed instance of PropertyInterface
162  template<typename PROP_PTR>
163  void copyToProperty(PROP_PTR prop) {
164  const std::vector<edge>& edges = graph->edges();
165  unsigned int nbEdges = edges.size();
166 
167  for (unsigned int i = 0; i < nbEdges; ++i)
168  prop->setEdgeValue(edges[i], (*this)[i]);
169  }
170 };
171 
172 // vector<bool> does not support // update
173 // so do an iterative specialization
174 template<> template<typename PROP_PTR>
175 void EdgeStaticProperty<bool>::copyFromProperty(PROP_PTR prop) {
176  const std::vector<edge>& edges = graph->edges();
177  unsigned int nbEdges = edges.size();
178 
179  for (unsigned int i = 0; i < nbEdges; ++i)
180  (*this)[i] = prop->getEdgeValue(edges[i]);
181 }
182 }
183 
184 #endif