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