Tulip  4.8.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
AbstractProperty.cxx
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
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 #include <cstdlib>
20 
21 template <class Tnode, class Tedge, class Tprop>
23  Tprop::graph = sg;
24  Tprop::name = n;
25  nodeDefaultValue = Tnode::defaultValue();
26  edgeDefaultValue = Tedge::defaultValue();
27  nodeProperties.setAll(Tnode::defaultValue());
28  edgeProperties.setAll(Tedge::defaultValue());
29  Tprop::metaValueCalculator = NULL;
30 }
31 //=============================================================
32 template <class Tnode, class Tedge, class Tprop>
34  return nodeDefaultValue;
35 }
36 //=============================================================
37 template <class Tnode, class Tedge, class Tprop>
39  return edgeDefaultValue;
40 }
41 //=============================================================
42 template <class Tnode, class Tedge, class Tprop>
43 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNodeValue(const tlp::node n ) const {
44  assert(n.isValid());
45  return nodeProperties.get(n.id);
46 }
47 //=============================================================
48 template <class Tnode, class Tedge, class Tprop>
49 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue tlp::AbstractProperty<Tnode,Tedge,Tprop>::getEdgeValue(const tlp::edge e) const {
50  assert(e.isValid());
51  return edgeProperties.get(e.id);
52 }
53 //=============================================================
54 template <class Tnode, class Tedge, class Tprop>
55 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setNodeValue(const tlp::node n,const typename Tnode::RealType &v) {
56  assert(n.isValid());
57  Tprop::notifyBeforeSetNodeValue(n);
58  nodeProperties.set(n.id,v);
59  Tprop::notifyAfterSetNodeValue(n);
60 }
61 //=============================================================
62 template <class Tnode, class Tedge, class Tprop>
63 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setEdgeValue(const tlp::edge e,const typename Tedge::RealType &v) {
64  assert(e.isValid());
65  Tprop::notifyBeforeSetEdgeValue(e);
66  edgeProperties.set(e.id,v);
67  Tprop::notifyAfterSetEdgeValue(e);
68 }
69 //=============================================================
70 template <class Tnode, class Tedge, class Tprop>
71 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setAllNodeValue(const typename Tnode::RealType &v) {
72  Tprop::notifyBeforeSetAllNodeValue();
73  nodeDefaultValue = v;
74  nodeProperties.setAll(v);
75  Tprop::notifyAfterSetAllNodeValue();
76 }
77 //============================================================
78 template <class Tnode, class Tedge, class Tprop>
79 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setAllEdgeValue(const typename Tedge::RealType &v) {
80  Tprop::notifyBeforeSetAllEdgeValue();
81  edgeDefaultValue = v;
82  edgeProperties.setAll(v);
83  Tprop::notifyAfterSetAllEdgeValue();
84 }
85 
86 template <class Tnode, class Tedge, class Tprop>
88  const typename Tnode::RealType& n1Value = getNodeValue(n1);
89  const typename Tnode::RealType& n2Value = getNodeValue(n2);
90  return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
91 }
92 //============================================================
93 template <class Tnode, class Tedge, class Tprop>
94 int tlp::AbstractProperty<Tnode,Tedge,Tprop>::compare(const edge e1,const edge e2)const {
95  const typename Tedge::RealType& e1Value = getEdgeValue(e1);
96  const typename Tedge::RealType& e2Value = getEdgeValue(e2);
97  return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
98 }
99 //============================================================
100 // define template iterator class to iterate over graph elts
101 // belonging to a given graph instance
102 // used by the two methods below
103 #ifndef DOXYGEN_NOTFOR_DEVEL
104 template <typename ELT_TYPE>
105 class GraphEltIterator :public tlp::Iterator<ELT_TYPE> {
106 public:
107  ELT_TYPE next() {
108  ELT_TYPE tmp = curElt;
109 
110  if ((_hasnext = it->hasNext())) {
111  curElt = it->next();
112 
113  while (!(_hasnext = (!graph || graph->isElement(curElt)))) {
114  if (!it->hasNext()) break;
115 
116  curElt=it->next();
117  }
118  }
119 
120  return tmp;
121  }
122  GraphEltIterator(const tlp::Graph* g, tlp::Iterator<ELT_TYPE>* itN)
123  :it(itN), graph(g), curElt(ELT_TYPE()), _hasnext(false) {
124  next();
125  }
126 
127  bool hasNext() {
128  return (_hasnext);
129  }
130  ~GraphEltIterator() {
131  delete it;
132  }
133 
134 private:
135  tlp::Iterator<ELT_TYPE> *it;
136  const tlp::Graph* graph;
137  ELT_TYPE curElt;
138  bool _hasnext;
139 };
140 #endif // DOXYGEN_NOTFOR_DEVEL
141 //============================================================
142 template <class Tnode, class Tedge, class Tprop>
143 tlp::Iterator<tlp::node>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedNodes(const Graph* g) const {
144  tlp::Iterator<tlp::node> *it =
145  new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue, false));
146 
147  if (Tprop::name.empty())
148  // we always need to check that nodes belong to graph
149  // for non registered properties, because deleted nodes are not erased
150  // from them
151  return new GraphEltIterator<tlp::node>(g != NULL ? g : Tprop::graph, it);
152 
153  return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::node>(g, it);
154 }
155 //============================================================
156 template <class Tnode, class Tedge, class Tprop>
158  if (g == NULL) {
159  return nodeProperties.numberOfNonDefaultValues();
160  }
161  else {
162  unsigned int ret = 0;
163  node n;
164  forEach(n, getNonDefaultValuatedNodes(g)) {
165  ++ret;
166  }
167  return ret;
168  }
169 }
170 //============================================================
171 template <class Tnode, class Tedge, class Tprop>
173  return Tnode::valueSize();
174 }
175 //============================================================
176 template <class Tnode, class Tedge, class Tprop>
178  Tnode::writeb(oss, nodeDefaultValue);
179 }
180 //============================================================
181 template <class Tnode, class Tedge, class Tprop>
183  node n) const {
184  assert(n.isValid());
185  Tnode::writeb(oss, nodeProperties.get(n.id));
186 }
187 //============================================================
188 template <class Tnode, class Tedge, class Tprop>
190  if (Tnode::readb(iss, nodeDefaultValue)) {
191  nodeProperties.setAll(nodeDefaultValue);
192  return true;
193  }
194 
195  return false;
196 }
197 //============================================================
198 template <class Tnode, class Tedge, class Tprop>
200  node n) {
201  typename Tnode::RealType val;
202 
203  if (Tnode::readb(iss, val)) {
204  nodeProperties.set(n.id, val);
205  return true;
206  }
207 
208  return false;
209 }
210 //============================================================
211 template <class Tnode, class Tedge, class Tprop>
212 tlp::Iterator<tlp::edge>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedEdges(const Graph* g) const {
213  tlp::Iterator<tlp::edge>* it =
214  new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue, false));
215 
216  if (Tprop::name.empty())
217  // we always need to check that edges belong to graph
218  // for non registered properties, because deleted edges are not erased
219  // from them
220  return new GraphEltIterator<tlp::edge>(g != NULL ? g : Tprop::graph, it);
221 
222  return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::edge>(g, it);
223 }
224 //============================================================
225 template <class Tnode, class Tedge, class Tprop>
227  if (g == NULL) {
228  return edgeProperties.numberOfNonDefaultValues();
229  }
230  else {
231  unsigned int ret = 0;
232  edge e;
233  forEach(e, getNonDefaultValuatedEdges(g)) {
234  ++ret;
235  }
236  return ret;
237  }
238 }
239 //============================================================
240 template <class Tnode, class Tedge, class Tprop>
242  return Tedge::valueSize();
243 }
244 //============================================================
245 template <class Tnode, class Tedge, class Tprop>
247  Tedge::writeb(oss, edgeDefaultValue);
248 }
249 //============================================================
250 template <class Tnode, class Tedge, class Tprop>
252  edge e) const {
253  assert(e.isValid());
254  Tedge::writeb(oss, edgeProperties.get(e.id));
255 }
256 //============================================================
257 template <class Tnode, class Tedge, class Tprop>
259  if (Tedge::readb(iss, edgeDefaultValue)) {
260  edgeProperties.setAll(edgeDefaultValue);
261  return true;
262  }
263 
264  return false;
265 }
266 //============================================================
267 template <class Tnode, class Tedge, class Tprop>
269  edge e) {
270  typename Tedge::RealType val;
271 
272  if (Tedge::readb(iss, val)) {
273  edgeProperties.set(e.id, val);
274  return true;
275  }
276 
277  return false;
278 }
279 //============================================================
280 template <typename vectType, typename eltType, typename propType>
281 tlp::AbstractVectorProperty<vectType, eltType, propType>::AbstractVectorProperty(tlp::Graph* g, const std::string& name) :AbstractProperty<vectType, vectType, propType>(g, name) {}
282 //============================================================
283 template <typename vectType, typename eltType, typename propType>
284 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(const node n, const std::string& s, char openChar, char sepChar, char closeChar) {
285  typename vectType::RealType v;
286  std::istringstream iss(s);
287 
288  if (!vectType::read(iss, v, openChar, sepChar, closeChar))
289  return false;
290 
291  this->setNodeValue(n, v);
292  return true;
293 }
294 //============================================================
295 template <typename vectType, typename eltType, typename propType>
296 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(const edge e, const std::string& s, char openChar, char sepChar, char closeChar) {
297  typename vectType::RealType v;
298  std::istringstream iss(s);
299 
300  if (!vectType::read(iss, v, openChar, sepChar, closeChar))
301  return false;
302 
303  this->setEdgeValue(e, v);
304  return true;
305 }
306 //============================================================
307 template <typename vectType, typename eltType, typename propType>
308 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
309  assert(n.isValid());
310  bool isNotDefault;
311  typename vectType::RealType& vect =
312  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
313  assert(vect.size() > i);
314  this->propType::notifyBeforeSetNodeValue(n);
315 
316  if (isNotDefault)
317  vect[i] = v;
318  else {
319  typename vectType::RealType tmp(vect);
320  tmp[i] = v;
321  AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n.id, tmp);
322  }
323 
324  this->propType::notifyAfterSetNodeValue(n);
325 }
326 //============================================================
327 template <typename vectType, typename eltType, typename propType>
328 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
329 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(const node n, unsigned int i) const {
330  assert(n.isValid());
331  const typename vectType::RealType& vect =
332  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n);
333  assert(vect.size() > i);
334  return vect[i];
335 }
336 //============================================================
337 template <typename vectType, typename eltType, typename propType>
338 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
339  assert(n.isValid());
340  bool isNotDefault;
341  typename vectType::RealType& vect =
342  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
343  this->propType::notifyBeforeSetNodeValue(n);
344 
345  if (isNotDefault)
346  vect.push_back(v);
347  else {
348  typename vectType::RealType tmp(vect);
349  tmp.push_back(v);
350  AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n, tmp);
351  }
352 
353  this->propType::notifyAfterSetNodeValue(n);
354 }
355 //============================================================
356 template <typename vectType, typename eltType, typename propType>
357 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(const node n) {
358  assert(n.isValid());
359  bool isNotDefault;
360  typename vectType::RealType& vect =
361  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
362  this->propType::notifyBeforeSetNodeValue(n);
363  assert(isNotDefault);
364  vect.pop_back();
365  this->propType::notifyAfterSetNodeValue(n);
366 }
367 //============================================================
368 template <typename vectType, typename eltType, typename propType>
369 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(const node n, size_t size, typename eltType::RealType elt) {
370  assert(n.isValid());
371  bool isNotDefault;
372  typename vectType::RealType& vect =
373  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
374  assert(isNotDefault);
375  this->propType::notifyBeforeSetNodeValue(n);
376  vect.resize(size, elt);
377  this->propType::notifyAfterSetNodeValue(n);
378 }
379 //============================================================
380 template <typename vectType, typename eltType, typename propType>
381 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
382  assert(e.isValid());
383  bool isNotDefault;
384  typename vectType::RealType& vect =
385  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
386  assert(vect.size() > i);
387  this->propType::notifyBeforeSetEdgeValue(e);
388 
389  if (isNotDefault)
390  vect[i] = v;
391  else {
392  typename vectType::RealType tmp(vect);
393  tmp[i] = v;
394  AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
395  }
396 
397  this->propType::notifyAfterSetEdgeValue(e);
398 }
399 //============================================================
400 template <typename vectType, typename eltType, typename propType>
401 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
402 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(const edge e, unsigned int i) const {
403  assert(e.isValid());
404  const typename vectType::RealType& vect =
405  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e);
406  assert(vect.size() > i);
407  return vect[i];
408 }//============================================================
409 template <typename vectType, typename eltType, typename propType>
410 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
411  assert(e.isValid());
412  bool isNotDefault;
413  typename vectType::RealType& vect =
414  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
415  this->propType::notifyBeforeSetEdgeValue(e);
416 
417  if (isNotDefault)
418  vect.push_back(v);
419  else {
420  typename vectType::RealType tmp(vect);
421  tmp.push_back(v);
422  AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
423  }
424 
425  this->propType::notifyAfterSetEdgeValue(e);
426 }
427 //============================================================
428 template <typename vectType, typename eltType, typename propType>
429 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(const edge e) {
430  assert(e.isValid());
431  bool isNotDefault;
432  typename vectType::RealType& vect =
433  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
434  this->propType::notifyBeforeSetEdgeValue(e);
435  assert(isNotDefault);
436  vect.pop_back();
437  this->propType::notifyAfterSetEdgeValue(e);
438 }
439 //============================================================
440 template <typename vectType, typename eltType, typename propType>
441 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt) {
442  assert(e.isValid());
443  bool isNotDefault;
444  typename vectType::RealType& vect =
445  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
446  assert(isNotDefault);
447  this->propType::notifyBeforeSetEdgeValue(e);
448  vect.resize(size, elt);
449  this->propType::notifyAfterSetEdgeValue(e);
450 }
virtual void setAllEdgeValue(const typename Tedge::RealType &v)
Sets the value of all edges and notify the observers. All previous values are lost and the given valu...
This class extends upon PropertyInterface, and adds type-safe methods to get and set the node and edg...
virtual tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue getNodeValue(const node n) const
Returns the value associated with the node n in this property. If there is no value, it returns the default node value.
bool isValid() const
isValid checks if the node is valid. An invalid node is a node whose id is UINT_MAX.
Definition: Node.h:90
virtual void setEdgeValue(const edge e, const typename Tedge::RealType &v)
Set the value of an edge and notify the observers of a modification.
virtual Tnode::RealType getNodeDefaultValue() const
Gets the default node value of the property.
virtual void setNodeValue(const node n, const typename Tnode::RealType &v)
Sets the value of a node and notify the observers of a modification.
The edge struct represents an edge in a Graph object.
Definition: Edge.h:39
The node struct represents a node in a Graph object.
Definition: Node.h:39
bool isValid() const
isValid checks if the edge is valid. An invalid edge is an edge whose id is UINT_MAX.
Definition: Edge.h:90
virtual tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue getEdgeValue(const edge e) const
Returns the value associated to the edge e in this property. If there is no value, it returns the default edge value.
virtual Tedge::RealType getEdgeDefaultValue() const
Gets the default edge value of the property.
unsigned int id
id The identifier of the node.
Definition: Node.h:43
virtual void setAllNodeValue(const typename Tnode::RealType &v)
Sets the value of all nodes and notify the observers. All previous values are lost and the given valu...
unsigned int id
id The identifier of the edge.
Definition: Edge.h:43