19 #ifndef STATICPROPERTY_H 20 #define STATICPROPERTY_H 22 #include <tulip/Graph.h> 23 #include <tulip/GraphParallelTools.h> 24 #include <tulip/NumericProperty.h> 27 template <
typename TYPE>
28 class NodeStaticProperty :
public std::vector<TYPE> {
33 NodeStaticProperty(
const Graph *g) : graph(g) {
36 this->resize(graph->numberOfNodes());
39 inline typename std::vector<TYPE>::const_reference operator[](
unsigned int i)
const {
40 return std::vector<TYPE>::operator[](i);
43 inline typename std::vector<TYPE>::reference operator[](
unsigned int i) {
44 return std::vector<TYPE>::operator[](i);
47 inline typename std::vector<TYPE>::const_reference operator[](node n)
const {
48 return (*
this)[graph->nodePos(n)];
51 inline typename std::vector<TYPE>::reference operator[](node n) {
52 return (*
this)[graph->nodePos(n)];
56 inline typename std::vector<TYPE>::const_reference getNodeValue(node n)
const {
61 inline void setNodeValue(node n, TYPE val) {
66 void setAll(
const TYPE &val) {
67 TLP_PARALLEL_MAP_INDICES(graph->numberOfNodes(), [&](
unsigned int i) { (*this)[i] = val; });
71 void addNodeValue(node n, TYPE val) {
72 unsigned int nPos = graph->nodePos(n);
74 if (nPos + 1 > this->size())
75 this->resize(nPos + 1);
81 template <
typename PROP_PTR>
82 void copyFromProperty(PROP_PTR prop) {
84 graph, [&](
const node n,
unsigned int i) { (*this)[i] = prop->getNodeValue(n); });
88 void copyFromNumericProperty(
const NumericProperty *prop) {
90 graph, [&](
const node n,
unsigned int i) { (*this)[i] = prop->getNodeDoubleValue(n); });
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();
99 for (
unsigned int i = 0; i < nbNodes; ++i)
100 prop->setNodeValue(nodes[i], (*
this)[i]);
105 class NodeStaticProperty<bool> :
public std::vector<unsigned char> {
110 NodeStaticProperty(
const Graph *g) : graph(g) {
113 this->resize(graph->numberOfNodes());
116 inline const Graph *getGraph()
const {
120 inline bool operator[](
unsigned int i)
const {
121 return static_cast<bool>(std::vector<unsigned char>::operator[](i));
124 inline std::vector<unsigned char>::reference operator[](
unsigned int i) {
125 return std::vector<unsigned char>::operator[](i);
128 inline bool operator[](node n)
const {
129 return (*
this)[graph->nodePos(n)];
132 inline std::vector<unsigned char>::reference operator[](node n) {
133 return (*
this)[graph->nodePos(n)];
137 inline bool getNodeValue(node n)
const {
138 return (*
this)[graph->nodePos(n)];
142 inline void setNodeValue(node n,
bool val) {
143 (*this)[graph->nodePos(n)] = val;
147 void setAll(
const bool &val) {
148 TLP_PARALLEL_MAP_INDICES(graph->numberOfNodes(), [&](
unsigned int i) { (*this)[i] = val; });
152 void addNodeValue(node n,
bool val) {
153 unsigned int nPos = graph->nodePos(n);
155 if (nPos + 1 > this->size())
156 this->resize(nPos + 1);
162 template <
typename PROP_PTR>
163 void copyFromProperty(PROP_PTR prop) {
165 graph, [&](
const node n,
unsigned int i) { (*this)[i] = prop->getNodeValue(n); });
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();
174 for (
unsigned int i = 0; i < nbNodes; ++i)
175 prop->setNodeValue(nodes[i], (*
this)[i]);
179 template <
typename TYPE>
180 class EdgeStaticProperty :
public std::vector<TYPE> {
185 EdgeStaticProperty(
const Graph *g) : graph(g) {
188 this->resize(graph->numberOfEdges());
191 inline const Graph *getGraph()
const {
195 inline typename std::vector<TYPE>::const_reference operator[](
unsigned int i)
const {
196 return std::vector<TYPE>::operator[](i);
199 inline typename std::vector<TYPE>::reference operator[](
unsigned int i) {
200 return std::vector<TYPE>::operator[](i);
203 inline typename std::vector<TYPE>::const_reference operator[](edge e)
const {
204 return (*
this)[graph->edgePos(e)];
207 inline typename std::vector<TYPE>::reference operator[](edge e) {
208 return (*
this)[graph->edgePos(e)];
212 inline typename std::vector<TYPE>::const_reference getEdgeValue(edge e)
const {
217 inline void setEdgeValue(edge e, TYPE val) {
221 void setAll(
const TYPE &val) {
222 TLP_PARALLEL_MAP_INDICES(graph->numberOfEdges(), [&](
unsigned int i) { (*this)[i] = val; });
226 void addEdgeValue(edge e, TYPE val) {
227 unsigned int ePos = graph->edgePos(e);
229 if (ePos + 1 > this->size())
230 this->resize(ePos + 1);
236 template <
typename PROP_PTR>
237 void copyFromProperty(PROP_PTR prop) {
239 graph, [&](
const edge e,
unsigned int i) { (*this)[i] = prop->getEdgeValue(e); });
243 void copyFromNumericProperty(
const NumericProperty *prop) {
245 graph, [&](
const edge e,
unsigned int i) { (*this)[i] = prop->getEdgeDoubleValue(e); });
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();
254 for (
unsigned int i = 0; i < nbEdges; ++i)
255 prop->setEdgeValue(edges[i], (*
this)[i]);
260 class EdgeStaticProperty<bool> :
public std::vector<unsigned char> {
265 EdgeStaticProperty(
const Graph *g) : graph(g) {
268 this->resize(graph->numberOfEdges());
271 inline bool operator[](
unsigned int i)
const {
272 return static_cast<bool>(std::vector<unsigned char>::operator[](i));
275 inline std::vector<unsigned char>::reference operator[](
unsigned int i) {
276 return std::vector<unsigned char>::operator[](i);
279 inline bool operator[](edge e)
const {
280 return (*
this)[graph->edgePos(e)];
283 inline std::vector<unsigned char>::reference operator[](edge e) {
284 return (*
this)[graph->edgePos(e)];
288 inline bool getEdgeValue(edge e)
const {
289 return (*
this)[graph->edgePos(e)];
293 inline void setEdgeValue(edge e,
bool val) {
294 (*this)[graph->edgePos(e)] = val;
298 void setAll(
const bool &val) {
299 TLP_PARALLEL_MAP_INDICES(graph->numberOfEdges(), [&](
unsigned int i) { (*this)[i] = val; });
303 void addEdgeValue(edge e,
bool val) {
304 unsigned int ePos = graph->edgePos(e);
306 if (ePos + 1 > this->size())
307 this->resize(ePos + 1);
313 template <
typename PROP_PTR>
314 void copyFromProperty(PROP_PTR prop) {
316 graph, [&](
const edge e,
unsigned int i) { (*this)[i] = prop->getEdgeValue(e); });
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();
325 for (
unsigned int i = 0; i < nbEdges; ++i)
326 prop->setEdgeValue(edges[i], (*
this)[i]);
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)