Tulip  5.4.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 #include <tulip/Graph.h>
23 #include <tulip/GraphParallelTools.h>
24 #include <tulip/NumericProperty.h>
25 
26 namespace tlp {
27 template <typename TYPE>
28 class NodeStaticProperty : public std::vector<TYPE> {
29  const Graph *graph;
30 
31 public:
32  // constructor
33  NodeStaticProperty(const Graph *g) : graph(g) {
34  assert(g);
35  // set the vector size to the number of graph nodes
36  this->resize(graph->numberOfNodes());
37  }
38 
39  inline typename std::vector<TYPE>::const_reference operator[](unsigned int i) const {
40  return std::vector<TYPE>::operator[](i);
41  }
42 
43  inline typename std::vector<TYPE>::reference operator[](unsigned int i) {
44  return std::vector<TYPE>::operator[](i);
45  }
46 
47  inline typename std::vector<TYPE>::const_reference operator[](node n) const {
48  return (*this)[graph->nodePos(n)];
49  }
50 
51  inline typename std::vector<TYPE>::reference operator[](node n) {
52  return (*this)[graph->nodePos(n)];
53  }
54 
55  // get the stored value of a node
56  inline typename std::vector<TYPE>::const_reference getNodeValue(node n) const {
57  return (*this)[n];
58  }
59 
60  // set the stored value of a node
61  inline void setNodeValue(node n, TYPE val) {
62  (*this)[n] = val;
63  }
64 
65  // set all to same values
66  void setAll(const TYPE &val) {
67  TLP_PARALLEL_MAP_INDICES(graph->numberOfNodes(), [&](unsigned int i) { (*this)[i] = val; });
68  }
69 
70  // add a value for a newly created node
71  void addNodeValue(node n, TYPE val) {
72  unsigned int nPos = graph->nodePos(n);
73 
74  if (nPos + 1 > this->size())
75  this->resize(nPos + 1);
76 
77  (*this)[nPos] = val;
78  }
79 
80  // get values from a typed instance of PropertyInterface
81  template <typename PROP_PTR>
82  void copyFromProperty(PROP_PTR prop) {
84  graph, [&](const node n, unsigned int i) { (*this)[i] = prop->getNodeValue(n); });
85  }
86 
87  // get values from a NumericProperty
88  void copyFromNumericProperty(const NumericProperty *prop) {
90  graph, [&](const node n, unsigned int i) { (*this)[i] = prop->getNodeDoubleValue(n); });
91  }
92 
93  // copy values into a typed typed instance of PropertyInterface
94  template <typename PROP_PTR>
95  void copyToProperty(PROP_PTR prop) {
96  const std::vector<node> &nodes = graph->nodes();
97  unsigned int nbNodes = nodes.size();
98 
99  for (unsigned int i = 0; i < nbNodes; ++i)
100  prop->setNodeValue(nodes[i], (*this)[i]);
101  }
102 };
103 
104 template <>
105 class NodeStaticProperty<bool> : public std::vector<unsigned char> {
106  const Graph *graph;
107 
108 public:
109  // constructor
110  NodeStaticProperty(const Graph *g) : graph(g) {
111  assert(g);
112  // set the vector size to the number of graph nodes
113  this->resize(graph->numberOfNodes());
114  }
115 
116  inline const Graph *getGraph() const {
117  return graph;
118  }
119 
120  inline bool operator[](unsigned int i) const {
121  return static_cast<bool>(std::vector<unsigned char>::operator[](i));
122  }
123 
124  inline std::vector<unsigned char>::reference operator[](unsigned int i) {
125  return std::vector<unsigned char>::operator[](i);
126  }
127 
128  inline bool operator[](node n) const {
129  return (*this)[graph->nodePos(n)];
130  }
131 
132  inline std::vector<unsigned char>::reference operator[](node n) {
133  return (*this)[graph->nodePos(n)];
134  }
135 
136  // get the stored value of a node
137  inline bool getNodeValue(node n) const {
138  return (*this)[graph->nodePos(n)];
139  }
140 
141  // set the stored value of a node
142  inline void setNodeValue(node n, bool val) {
143  (*this)[graph->nodePos(n)] = val;
144  }
145 
146  // set all to same values
147  void setAll(const bool &val) {
148  TLP_PARALLEL_MAP_INDICES(graph->numberOfNodes(), [&](unsigned int i) { (*this)[i] = val; });
149  }
150 
151  // add a value for a newly created node
152  void addNodeValue(node n, bool val) {
153  unsigned int nPos = graph->nodePos(n);
154 
155  if (nPos + 1 > this->size())
156  this->resize(nPos + 1);
157 
158  (*this)[nPos] = val;
159  }
160 
161  // get values from a typed instance of PropertyInterface
162  template <typename PROP_PTR>
163  void copyFromProperty(PROP_PTR prop) {
165  graph, [&](const node n, unsigned int i) { (*this)[i] = prop->getNodeValue(n); });
166  }
167 
168  // copy values into a typed instance of PropertyInterface
169  template <typename PROP_PTR>
170  void copyToProperty(PROP_PTR prop) {
171  const std::vector<node> &nodes = graph->nodes();
172  unsigned int nbNodes = nodes.size();
173 
174  for (unsigned int i = 0; i < nbNodes; ++i)
175  prop->setNodeValue(nodes[i], (*this)[i]);
176  }
177 };
178 
179 template <typename TYPE>
180 class EdgeStaticProperty : public std::vector<TYPE> {
181  const Graph *graph;
182 
183 public:
184  // constructor
185  EdgeStaticProperty(const Graph *g) : graph(g) {
186  assert(g);
187  // set the vector size to the number of graph edges
188  this->resize(graph->numberOfEdges());
189  }
190 
191  inline const Graph *getGraph() const {
192  return graph;
193  }
194 
195  inline typename std::vector<TYPE>::const_reference operator[](unsigned int i) const {
196  return std::vector<TYPE>::operator[](i);
197  }
198 
199  inline typename std::vector<TYPE>::reference operator[](unsigned int i) {
200  return std::vector<TYPE>::operator[](i);
201  }
202 
203  inline typename std::vector<TYPE>::const_reference operator[](edge e) const {
204  return (*this)[graph->edgePos(e)];
205  }
206 
207  inline typename std::vector<TYPE>::reference operator[](edge e) {
208  return (*this)[graph->edgePos(e)];
209  }
210 
211  // get the stored value of a edge
212  inline typename std::vector<TYPE>::const_reference getEdgeValue(edge e) const {
213  return (*this)[e];
214  }
215 
216  // set the stored value of a edge
217  inline void setEdgeValue(edge e, TYPE val) {
218  (*this)[e] = val;
219  }
220 
221  void setAll(const TYPE &val) {
222  TLP_PARALLEL_MAP_INDICES(graph->numberOfEdges(), [&](unsigned int i) { (*this)[i] = val; });
223  }
224 
225  // add a value for a newly created edge
226  void addEdgeValue(edge e, TYPE val) {
227  unsigned int ePos = graph->edgePos(e);
228 
229  if (ePos + 1 > this->size())
230  this->resize(ePos + 1);
231 
232  (*this)[ePos] = val;
233  }
234 
235  // get values from a typed instance of PropertyInterface
236  template <typename PROP_PTR>
237  void copyFromProperty(PROP_PTR prop) {
239  graph, [&](const edge e, unsigned int i) { (*this)[i] = prop->getEdgeValue(e); });
240  }
241 
242  // get values from a NumericProperty
243  void copyFromNumericProperty(const NumericProperty *prop) {
245  graph, [&](const edge e, unsigned int i) { (*this)[i] = prop->getEdgeDoubleValue(e); });
246  }
247 
248  // copy values into a typed instance of PropertyInterface
249  template <typename PROP_PTR>
250  void copyToProperty(PROP_PTR prop) {
251  const std::vector<edge> &edges = graph->edges();
252  unsigned int nbEdges = edges.size();
253 
254  for (unsigned int i = 0; i < nbEdges; ++i)
255  prop->setEdgeValue(edges[i], (*this)[i]);
256  }
257 };
258 
259 template <>
260 class EdgeStaticProperty<bool> : public std::vector<unsigned char> {
261  const Graph *graph;
262 
263 public:
264  // constructor
265  EdgeStaticProperty(const Graph *g) : graph(g) {
266  assert(g);
267  // set the vector size to the number of graph edges
268  this->resize(graph->numberOfEdges());
269  }
270 
271  inline bool operator[](unsigned int i) const {
272  return static_cast<bool>(std::vector<unsigned char>::operator[](i));
273  }
274 
275  inline std::vector<unsigned char>::reference operator[](unsigned int i) {
276  return std::vector<unsigned char>::operator[](i);
277  }
278 
279  inline bool operator[](edge e) const {
280  return (*this)[graph->edgePos(e)];
281  }
282 
283  inline std::vector<unsigned char>::reference operator[](edge e) {
284  return (*this)[graph->edgePos(e)];
285  }
286 
287  // get the stored value of a edge
288  inline bool getEdgeValue(edge e) const {
289  return (*this)[graph->edgePos(e)];
290  }
291 
292  // set the stored value of a edge
293  inline void setEdgeValue(edge e, bool val) {
294  (*this)[graph->edgePos(e)] = val;
295  }
296 
297  // set all to same values
298  void setAll(const bool &val) {
299  TLP_PARALLEL_MAP_INDICES(graph->numberOfEdges(), [&](unsigned int i) { (*this)[i] = val; });
300  }
301 
302  // add a value for a newly created edge
303  void addEdgeValue(edge e, bool val) {
304  unsigned int ePos = graph->edgePos(e);
305 
306  if (ePos + 1 > this->size())
307  this->resize(ePos + 1);
308 
309  (*this)[ePos] = val;
310  }
311 
312  // get values from a typed instance of PropertyInterface
313  template <typename PROP_PTR>
314  void copyFromProperty(PROP_PTR prop) {
316  graph, [&](const edge e, unsigned int i) { (*this)[i] = prop->getEdgeValue(e); });
317  }
318 
319  // copy values into a typed instance of PropertyInterface
320  template <typename PROP_PTR>
321  void copyToProperty(PROP_PTR prop) {
322  const std::vector<edge> &edges = graph->edges();
323  unsigned int nbEdges = edges.size();
324 
325  for (unsigned int i = 0; i < nbEdges; ++i)
326  prop->setEdgeValue(edges[i], (*this)[i]);
327  }
328 };
329 } // namespace tlp
330 
331 #endif
void TLP_PARALLEL_MAP_NODES_AND_INDICES(const tlp::Graph *graph, const NodeFunction &nodeFunction)
void TLP_PARALLEL_MAP_EDGES_AND_INDICES(const tlp::Graph *graph, const EdgeFunction &edgeFunction)