Tulip  5.7.4
Large graphs analysis and drawing
StaticProperty.h
1 /*
2  *
3  * This file is part of Tulip (https://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  // constructors
33  NodeStaticProperty(const Graph *g) : std::vector<TYPE>(g->numberOfNodes()), graph(g) {}
34 
35  NodeStaticProperty(const Graph *g, TYPE val)
36  : std::vector<TYPE>(g->numberOfNodes(), val), graph(g) {}
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  // get values from a NumericProperty
87  void copyFromNumericProperty(const NumericProperty *prop) {
89  graph, [&](const node n, unsigned int i) { (*this)[i] = prop->getNodeDoubleValue(n); });
90  }
91 
92  // copy values into a typed typed instance of PropertyInterface
93  template <typename PROP_PTR>
94  void copyToProperty(PROP_PTR prop) {
95  const std::vector<node> &nodes = graph->nodes();
96  unsigned int nbNodes = nodes.size();
97 
98  for (unsigned int i = 0; i < nbNodes; ++i)
99  prop->setNodeValue(nodes[i], (*this)[i]);
100  }
101 };
102 
103 template <>
104 class NodeStaticProperty<bool> : public std::vector<unsigned char> {
105  const Graph *graph;
106 
107 public:
108  // constructors
109  NodeStaticProperty(const Graph *g) : std::vector<unsigned char>(g->numberOfNodes()), graph(g) {}
110 
111  NodeStaticProperty(const Graph *g, bool b)
112  : std::vector<unsigned char>(g->numberOfNodes(), b), graph(g) {}
113 
114  inline const Graph *getGraph() const {
115  return graph;
116  }
117 
118  inline bool operator[](unsigned int i) const {
119  return static_cast<bool>(std::vector<unsigned char>::operator[](i));
120  }
121 
122  inline std::vector<unsigned char>::reference operator[](unsigned int i) {
123  return std::vector<unsigned char>::operator[](i);
124  }
125 
126  inline bool operator[](node n) const {
127  return (*this)[graph->nodePos(n)];
128  }
129 
130  inline std::vector<unsigned char>::reference operator[](node n) {
131  return (*this)[graph->nodePos(n)];
132  }
133 
134  // get the stored value of a node
135  inline bool getNodeValue(node n) const {
136  return (*this)[graph->nodePos(n)];
137  }
138 
139  // set the stored value of a node
140  inline void setNodeValue(node n, bool val) {
141  (*this)[graph->nodePos(n)] = val;
142  }
143 
144  // set all to same values
145  void setAll(const bool &val) {
146  TLP_PARALLEL_MAP_INDICES(graph->numberOfNodes(), [&](unsigned int i) { (*this)[i] = val; });
147  }
148 
149  // add a value for a newly created node
150  void addNodeValue(node n, bool val) {
151  unsigned int nPos = graph->nodePos(n);
152 
153  if (nPos + 1 > this->size())
154  this->resize(nPos + 1);
155 
156  (*this)[nPos] = val;
157  }
158 
159  // get values from a typed instance of PropertyInterface
160  template <typename PROP_PTR>
161  void copyFromProperty(PROP_PTR prop) {
163  graph, [&](const node n, unsigned int i) { (*this)[i] = prop->getNodeValue(n); });
164  }
165 
166  // copy values into a typed instance of PropertyInterface
167  template <typename PROP_PTR>
168  void copyToProperty(PROP_PTR prop) {
169  const std::vector<node> &nodes = graph->nodes();
170  unsigned int nbNodes = nodes.size();
171 
172  for (unsigned int i = 0; i < nbNodes; ++i)
173  prop->setNodeValue(nodes[i], (*this)[i]);
174  }
175 };
176 
177 template <typename TYPE>
178 class EdgeStaticProperty : public std::vector<TYPE> {
179  const Graph *graph;
180 
181 public:
182  // constructors
183  EdgeStaticProperty(const Graph *g) : std::vector<TYPE>(g->numberOfEdges()), graph(g) {}
184  EdgeStaticProperty(const Graph *g, TYPE val)
185  : std::vector<TYPE>(g->numberOfEdges(), val), graph(g) {}
186 
187  inline const Graph *getGraph() const {
188  return graph;
189  }
190 
191  inline typename std::vector<TYPE>::const_reference operator[](unsigned int i) const {
192  return std::vector<TYPE>::operator[](i);
193  }
194 
195  inline typename std::vector<TYPE>::reference operator[](unsigned int i) {
196  return std::vector<TYPE>::operator[](i);
197  }
198 
199  inline typename std::vector<TYPE>::const_reference operator[](edge e) const {
200  return (*this)[graph->edgePos(e)];
201  }
202 
203  inline typename std::vector<TYPE>::reference operator[](edge e) {
204  return (*this)[graph->edgePos(e)];
205  }
206 
207  // get the stored value of a edge
208  inline typename std::vector<TYPE>::const_reference getEdgeValue(edge e) const {
209  return (*this)[e];
210  }
211 
212  // set the stored value of a edge
213  inline void setEdgeValue(edge e, TYPE val) {
214  (*this)[e] = val;
215  }
216 
217  void setAll(const TYPE &val) {
218  TLP_PARALLEL_MAP_INDICES(graph->numberOfEdges(), [&](unsigned int i) { (*this)[i] = val; });
219  }
220 
221  // add a value for a newly created edge
222  void addEdgeValue(edge e, TYPE val) {
223  unsigned int ePos = graph->edgePos(e);
224 
225  if (ePos + 1 > this->size())
226  this->resize(ePos + 1);
227 
228  (*this)[ePos] = val;
229  }
230 
231  // get values from a typed instance of PropertyInterface
232  template <typename PROP_PTR>
233  void copyFromProperty(PROP_PTR prop) {
235  graph, [&](const edge e, unsigned int i) { (*this)[i] = prop->getEdgeValue(e); });
236  }
237 
238  // get values from a NumericProperty
239  void copyFromNumericProperty(const NumericProperty *prop) {
241  graph, [&](const edge e, unsigned int i) { (*this)[i] = prop->getEdgeDoubleValue(e); });
242  }
243 
244  // copy values into a typed instance of PropertyInterface
245  template <typename PROP_PTR>
246  void copyToProperty(PROP_PTR prop) {
247  const std::vector<edge> &edges = graph->edges();
248  unsigned int nbEdges = edges.size();
249 
250  for (unsigned int i = 0; i < nbEdges; ++i)
251  prop->setEdgeValue(edges[i], (*this)[i]);
252  }
253 };
254 
255 template <>
256 class EdgeStaticProperty<bool> : public std::vector<unsigned char> {
257  const Graph *graph;
258 
259 public:
260  // constructors
261  EdgeStaticProperty(const Graph *g) : std::vector<unsigned char>(g->numberOfEdges()), graph(g) {}
262 
263  EdgeStaticProperty(const Graph *g, bool b)
264  : std::vector<unsigned char>(g->numberOfEdges(), b), graph(g) {}
265 
266  inline bool operator[](unsigned int i) const {
267  return static_cast<bool>(std::vector<unsigned char>::operator[](i));
268  }
269 
270  inline std::vector<unsigned char>::reference operator[](unsigned int i) {
271  return std::vector<unsigned char>::operator[](i);
272  }
273 
274  inline bool operator[](edge e) const {
275  return (*this)[graph->edgePos(e)];
276  }
277 
278  inline std::vector<unsigned char>::reference operator[](edge e) {
279  return (*this)[graph->edgePos(e)];
280  }
281 
282  // get the stored value of a edge
283  inline bool getEdgeValue(edge e) const {
284  return (*this)[graph->edgePos(e)];
285  }
286 
287  // set the stored value of a edge
288  inline void setEdgeValue(edge e, bool val) {
289  (*this)[graph->edgePos(e)] = val;
290  }
291 
292  // set all to same values
293  void setAll(const bool &val) {
294  TLP_PARALLEL_MAP_INDICES(graph->numberOfEdges(), [&](unsigned int i) { (*this)[i] = val; });
295  }
296 
297  // add a value for a newly created edge
298  void addEdgeValue(edge e, bool val) {
299  unsigned int ePos = graph->edgePos(e);
300 
301  if (ePos + 1 > this->size())
302  this->resize(ePos + 1);
303 
304  (*this)[ePos] = val;
305  }
306 
307  // get values from a typed instance of PropertyInterface
308  template <typename PROP_PTR>
309  void copyFromProperty(PROP_PTR prop) {
311  graph, [&](const edge e, unsigned int i) { (*this)[i] = prop->getEdgeValue(e); });
312  }
313 
314  // copy values into a typed instance of PropertyInterface
315  template <typename PROP_PTR>
316  void copyToProperty(PROP_PTR prop) {
317  const std::vector<edge> &edges = graph->edges();
318  unsigned int nbEdges = edges.size();
319 
320  for (unsigned int i = 0; i < nbEdges; ++i)
321  prop->setEdgeValue(edges[i], (*this)[i]);
322  }
323 };
324 } // namespace tlp
325 
326 #endif
void TLP_PARALLEL_MAP_EDGES_AND_INDICES(const tlp::Graph *graph, const EdgeFunction &edgeFunction)
void TLP_PARALLEL_MAP_NODES_AND_INDICES(const tlp::Graph *graph, const NodeFunction &nodeFunction)