Tulip  4.10.0
Better Visualization Through Research
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, const Graph *graph) {
72  if (graph && this->getGraph()->isDescendantGraph(graph)) {
73  node n;
74  forEach(n, graph->getNodes()) {
75  setNodeValue(n, v);
76  }
77  }
78  else if (!graph || this->getGraph() == graph) {
79  Tprop::notifyBeforeSetAllNodeValue();
80  nodeDefaultValue = v;
81  nodeProperties.setAll(v);
82  Tprop::notifyAfterSetAllNodeValue();
83  }
84 }
85 //============================================================
86 template <class Tnode, class Tedge, class Tprop>
87 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setAllEdgeValue(const typename Tedge::RealType &v, const Graph *graph) {
88  if (graph && this->getGraph()->isDescendantGraph(graph)) {
89  edge e;
90  forEach(e, graph->getEdges()) {
91  setEdgeValue(e, v);
92  }
93  }
94  else if (!graph || this->getGraph() == graph) {
95  Tprop::notifyBeforeSetAllEdgeValue();
96  edgeDefaultValue = v;
97  edgeProperties.setAll(v);
98  Tprop::notifyAfterSetAllEdgeValue();
99  }
100 }
101 
102 template <class Tnode, class Tedge, class Tprop>
103 int tlp::AbstractProperty<Tnode,Tedge,Tprop>::compare(const node n1,const node n2)const {
104  const typename Tnode::RealType& n1Value = getNodeValue(n1);
105  const typename Tnode::RealType& n2Value = getNodeValue(n2);
106  return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
107 }
108 //============================================================
109 template <class Tnode, class Tedge, class Tprop>
110 int tlp::AbstractProperty<Tnode,Tedge,Tprop>::compare(const edge e1,const edge e2)const {
111  const typename Tedge::RealType& e1Value = getEdgeValue(e1);
112  const typename Tedge::RealType& e2Value = getEdgeValue(e2);
113  return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
114 }
115 //============================================================
116 // define template iterator class to iterate over graph elts
117 // belonging to a given graph instance
118 // used by the two methods below
119 ///@cond DOXYGEN_HIDDEN
120 template <typename ELT_TYPE>
121 class GraphEltIterator :public tlp::Iterator<ELT_TYPE> {
122 public:
123  ELT_TYPE next() {
124  ELT_TYPE tmp = curElt;
125 
126  if ((_hasnext = it->hasNext())) {
127  curElt = it->next();
128 
129  while (!(_hasnext = (!graph || graph->isElement(curElt)))) {
130  if (!it->hasNext()) break;
131 
132  curElt=it->next();
133  }
134  }
135 
136  return tmp;
137  }
138  GraphEltIterator(const tlp::Graph* g, tlp::Iterator<ELT_TYPE>* itN)
139  :it(itN), graph(g), curElt(ELT_TYPE()), _hasnext(false) {
140  next();
141  }
142 
143  bool hasNext() {
144  return (_hasnext);
145  }
146  ~GraphEltIterator() {
147  delete it;
148  }
149 
150 private:
151  tlp::Iterator<ELT_TYPE> *it;
152  const tlp::Graph* graph;
153  ELT_TYPE curElt;
154  bool _hasnext;
155 };
156 ///@endcond
157 
158 //============================================================
159 template <class Tnode, class Tedge, class Tprop>
160 tlp::Iterator<tlp::node>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedNodes(const Graph* g) const {
161  tlp::Iterator<tlp::node> *it =
162  new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue, false));
163 
164  if (Tprop::name.empty())
165  // we always need to check that nodes belong to graph
166  // for non registered properties, because deleted nodes are not erased
167  // from them
168  return new GraphEltIterator<tlp::node>(g != NULL ? g : Tprop::graph, it);
169 
170  return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::node>(g, it);
171 }
172 //============================================================
173 template <class Tnode, class Tedge, class Tprop>
175  if (g == NULL) {
176  return nodeProperties.numberOfNonDefaultValues();
177  }
178  else {
179  unsigned int ret = 0;
180  node n;
181  forEach(n, getNonDefaultValuatedNodes(g)) {
182  ++ret;
183  }
184  return ret;
185  }
186 }
187 //============================================================
188 template <class Tnode, class Tedge, class Tprop>
190  return Tnode::valueSize();
191 }
192 //============================================================
193 template <class Tnode, class Tedge, class Tprop>
195  Tnode::writeb(oss, nodeDefaultValue);
196 }
197 //============================================================
198 template <class Tnode, class Tedge, class Tprop>
200  node n) const {
201  assert(n.isValid());
202  Tnode::writeb(oss, nodeProperties.get(n.id));
203 }
204 //============================================================
205 template <class Tnode, class Tedge, class Tprop>
207  if (Tnode::readb(iss, nodeDefaultValue)) {
208  nodeProperties.setAll(nodeDefaultValue);
209  return true;
210  }
211 
212  return false;
213 }
214 //============================================================
215 template <class Tnode, class Tedge, class Tprop>
217  node n) {
218  typename Tnode::RealType val;
219 
220  if (Tnode::readb(iss, val)) {
221  nodeProperties.set(n.id, val);
222  return true;
223  }
224 
225  return false;
226 }
227 //============================================================
228 template <class Tnode, class Tedge, class Tprop>
229 tlp::Iterator<tlp::edge>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedEdges(const Graph* g) const {
230  tlp::Iterator<tlp::edge>* it =
231  new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue, false));
232 
233  if (Tprop::name.empty())
234  // we always need to check that edges belong to graph
235  // for non registered properties, because deleted edges are not erased
236  // from them
237  return new GraphEltIterator<tlp::edge>(g != NULL ? g : Tprop::graph, it);
238 
239  return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::edge>(g, it);
240 }
241 //============================================================
242 template <class Tnode, class Tedge, class Tprop>
244  if (g == NULL) {
245  return edgeProperties.numberOfNonDefaultValues();
246  }
247  else {
248  unsigned int ret = 0;
249  edge e;
250  forEach(e, getNonDefaultValuatedEdges(g)) {
251  ++ret;
252  }
253  return ret;
254  }
255 }
256 //============================================================
257 template <class Tnode, class Tedge, class Tprop>
259  return Tedge::valueSize();
260 }
261 //============================================================
262 template <class Tnode, class Tedge, class Tprop>
264  Tedge::writeb(oss, edgeDefaultValue);
265 }
266 //============================================================
267 template <class Tnode, class Tedge, class Tprop>
269  edge e) const {
270  assert(e.isValid());
271  Tedge::writeb(oss, edgeProperties.get(e.id));
272 }
273 //============================================================
274 template <class Tnode, class Tedge, class Tprop>
276  if (Tedge::readb(iss, edgeDefaultValue)) {
277  edgeProperties.setAll(edgeDefaultValue);
278  return true;
279  }
280 
281  return false;
282 }
283 //============================================================
284 template <class Tnode, class Tedge, class Tprop>
286  edge e) {
287  typename Tedge::RealType val;
288 
289  if (Tedge::readb(iss, val)) {
290  edgeProperties.set(e.id, val);
291  return true;
292  }
293 
294  return false;
295 }
296 //============================================================
297 template <typename vectType, typename eltType, typename propType>
298 tlp::AbstractVectorProperty<vectType, eltType, propType>::AbstractVectorProperty(tlp::Graph* g, const std::string& name) :AbstractProperty<vectType, vectType, propType>(g, name) {}
299 //============================================================
300 template <typename vectType, typename eltType, typename propType>
301 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(const node n, const std::string& s, char openChar, char sepChar, char closeChar) {
302  typename vectType::RealType v;
303  std::istringstream iss(s);
304 
305  if (!vectType::read(iss, v, openChar, sepChar, closeChar))
306  return false;
307 
308  this->setNodeValue(n, v);
309  return true;
310 }
311 //============================================================
312 template <typename vectType, typename eltType, typename propType>
313 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(const edge e, const std::string& s, char openChar, char sepChar, char closeChar) {
314  typename vectType::RealType v;
315  std::istringstream iss(s);
316 
317  if (!vectType::read(iss, v, openChar, sepChar, closeChar))
318  return false;
319 
320  this->setEdgeValue(e, v);
321  return true;
322 }
323 //============================================================
324 template <typename vectType, typename eltType, typename propType>
325 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
326  assert(n.isValid());
327  bool isNotDefault;
328  typename vectType::RealType& vect =
330  assert(vect.size() > i);
331  this->propType::notifyBeforeSetNodeValue(n);
332 
333  if (isNotDefault)
334  vect[i] = v;
335  else {
336  typename vectType::RealType tmp(vect);
337  tmp[i] = v;
339  }
340 
341  this->propType::notifyAfterSetNodeValue(n);
342 }
343 //============================================================
344 template <typename vectType, typename eltType, typename propType>
345 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
346 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(const node n, unsigned int i) const {
347  assert(n.isValid());
348  const typename vectType::RealType& vect =
350  assert(vect.size() > i);
351  return vect[i];
352 }
353 //============================================================
354 template <typename vectType, typename eltType, typename propType>
355 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
356  assert(n.isValid());
357  bool isNotDefault;
358  typename vectType::RealType& vect =
360  this->propType::notifyBeforeSetNodeValue(n);
361 
362  if (isNotDefault)
363  vect.push_back(v);
364  else {
365  typename vectType::RealType tmp(vect);
366  tmp.push_back(v);
368  }
369 
370  this->propType::notifyAfterSetNodeValue(n);
371 }
372 //============================================================
373 template <typename vectType, typename eltType, typename propType>
374 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(const node n) {
375  assert(n.isValid());
376  bool isNotDefault;
377  typename vectType::RealType& vect =
379  this->propType::notifyBeforeSetNodeValue(n);
380  assert(isNotDefault);
381  vect.pop_back();
382  this->propType::notifyAfterSetNodeValue(n);
383 }
384 //============================================================
385 template <typename vectType, typename eltType, typename propType>
386 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(const node n, size_t size, typename eltType::RealType elt) {
387  assert(n.isValid());
388  bool isNotDefault;
389  typename vectType::RealType& vect =
391  assert(isNotDefault);
392  this->propType::notifyBeforeSetNodeValue(n);
393  vect.resize(size, elt);
394  this->propType::notifyAfterSetNodeValue(n);
395 }
396 //============================================================
397 template <typename vectType, typename eltType, typename propType>
398 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
399  assert(e.isValid());
400  bool isNotDefault;
401  typename vectType::RealType& vect =
403  assert(vect.size() > i);
404  this->propType::notifyBeforeSetEdgeValue(e);
405 
406  if (isNotDefault)
407  vect[i] = v;
408  else {
409  typename vectType::RealType tmp(vect);
410  tmp[i] = v;
412  }
413 
414  this->propType::notifyAfterSetEdgeValue(e);
415 }
416 //============================================================
417 template <typename vectType, typename eltType, typename propType>
418 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
419 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(const edge e, unsigned int i) const {
420  assert(e.isValid());
421  const typename vectType::RealType& vect =
423  assert(vect.size() > i);
424  return vect[i];
425 }//============================================================
426 template <typename vectType, typename eltType, typename propType>
427 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
428  assert(e.isValid());
429  bool isNotDefault;
430  typename vectType::RealType& vect =
432  this->propType::notifyBeforeSetEdgeValue(e);
433 
434  if (isNotDefault)
435  vect.push_back(v);
436  else {
437  typename vectType::RealType tmp(vect);
438  tmp.push_back(v);
440  }
441 
442  this->propType::notifyAfterSetEdgeValue(e);
443 }
444 //============================================================
445 template <typename vectType, typename eltType, typename propType>
446 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(const edge e) {
447  assert(e.isValid());
448  bool isNotDefault;
449  typename vectType::RealType& vect =
451  this->propType::notifyBeforeSetEdgeValue(e);
452  assert(isNotDefault);
453  vect.pop_back();
454  this->propType::notifyAfterSetEdgeValue(e);
455 }
456 //============================================================
457 template <typename vectType, typename eltType, typename propType>
458 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt) {
459  assert(e.isValid());
460  bool isNotDefault;
461  typename vectType::RealType& vect =
463  assert(isNotDefault);
464  this->propType::notifyBeforeSetEdgeValue(e);
465  vect.resize(size, elt);
466  this->propType::notifyAfterSetEdgeValue(e);
467 }
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.
virtual Iterator< edge > * getEdges() const =0
Get an iterator over all the graph&#39;s edges.
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 Iterator< node > * getNodes() const =0
Gets an iterator over this graph&#39;s nodes.
virtual void setAllEdgeValue(const typename Tedge::RealType &v, const Graph *graph=NULL)
Sets the value of all edges and notify the observers. All previous values are lost and the given valu...
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, const Graph *graph=NULL)
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