Tulip  4.7.0
Better Visualization Through Research
 All Classes Namespaces 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  return nodeProperties.numberOfNonDefaultValues();
159 }
160 //============================================================
161 template <class Tnode, class Tedge, class Tprop>
163  return Tnode::valueSize();
164 }
165 //============================================================
166 template <class Tnode, class Tedge, class Tprop>
168  Tnode::writeb(oss, nodeDefaultValue);
169 }
170 //============================================================
171 template <class Tnode, class Tedge, class Tprop>
173  node n) const {
174  assert(n.isValid());
175  Tnode::writeb(oss, nodeProperties.get(n.id));
176 }
177 //============================================================
178 template <class Tnode, class Tedge, class Tprop>
180  if (Tnode::readb(iss, nodeDefaultValue)) {
181  nodeProperties.setAll(nodeDefaultValue);
182  return true;
183  }
184 
185  return false;
186 }
187 //============================================================
188 template <class Tnode, class Tedge, class Tprop>
190  node n) {
191  typename Tnode::RealType val;
192 
193  if (Tnode::readb(iss, val)) {
194  nodeProperties.set(n.id, val);
195  return true;
196  }
197 
198  return false;
199 }
200 //============================================================
201 template <class Tnode, class Tedge, class Tprop>
202 tlp::Iterator<tlp::edge>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedEdges(const Graph* g) const {
203  tlp::Iterator<tlp::edge>* it =
204  new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue, false));
205 
206  if (Tprop::name.empty())
207  // we always need to check that edges belong to graph
208  // for non registered properties, because deleted edges are not erased
209  // from them
210  return new GraphEltIterator<tlp::edge>(g != NULL ? g : Tprop::graph, it);
211 
212  return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::edge>(g, it);
213 }
214 //============================================================
215 template <class Tnode, class Tedge, class Tprop>
217  return edgeProperties.numberOfNonDefaultValues();
218 }
219 //============================================================
220 template <class Tnode, class Tedge, class Tprop>
222  return Tedge::valueSize();
223 }
224 //============================================================
225 template <class Tnode, class Tedge, class Tprop>
227  Tedge::writeb(oss, edgeDefaultValue);
228 }
229 //============================================================
230 template <class Tnode, class Tedge, class Tprop>
232  edge e) const {
233  assert(e.isValid());
234  Tedge::writeb(oss, edgeProperties.get(e.id));
235 }
236 //============================================================
237 template <class Tnode, class Tedge, class Tprop>
239  if (Tedge::readb(iss, edgeDefaultValue)) {
240  edgeProperties.setAll(edgeDefaultValue);
241  return true;
242  }
243 
244  return false;
245 }
246 //============================================================
247 template <class Tnode, class Tedge, class Tprop>
249  edge e) {
250  typename Tedge::RealType val;
251 
252  if (Tedge::readb(iss, val)) {
253  edgeProperties.set(e.id, val);
254  return true;
255  }
256 
257  return false;
258 }
259 //============================================================
260 template <typename vectType, typename eltType, typename propType>
261 tlp::AbstractVectorProperty<vectType, eltType, propType>::AbstractVectorProperty(tlp::Graph* g, const std::string& name) :AbstractProperty<vectType, vectType, propType>(g, name) {}
262 //============================================================
263 template <typename vectType, typename eltType, typename propType>
264 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(const node n, const std::string& s, char openChar, char sepChar, char closeChar) {
265  typename vectType::RealType v;
266  std::istringstream iss(s);
267 
268  if (!vectType::read(iss, v, openChar, sepChar, closeChar))
269  return false;
270 
271  this->setNodeValue(n, v);
272  return true;
273 }
274 //============================================================
275 template <typename vectType, typename eltType, typename propType>
276 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(const edge e, const std::string& s, char openChar, char sepChar, char closeChar) {
277  typename vectType::RealType v;
278  std::istringstream iss(s);
279 
280  if (!vectType::read(iss, v, openChar, sepChar, closeChar))
281  return false;
282 
283  this->setEdgeValue(e, v);
284  return true;
285 }
286 //============================================================
287 template <typename vectType, typename eltType, typename propType>
288 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
289  assert(n.isValid());
290  bool isNotDefault;
291  typename vectType::RealType& vect =
292  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
293  assert(vect.size() > i);
294  this->propType::notifyBeforeSetNodeValue(n);
295 
296  if (isNotDefault)
297  vect[i] = v;
298  else {
299  typename vectType::RealType tmp(vect);
300  tmp[i] = v;
301  AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n.id, tmp);
302  }
303 
304  this->propType::notifyAfterSetNodeValue(n);
305 }
306 //============================================================
307 template <typename vectType, typename eltType, typename propType>
308 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
309 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(const node n, unsigned int i) const {
310  assert(n.isValid());
311  const typename vectType::RealType& vect =
312  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n);
313  assert(vect.size() > i);
314  return vect[i];
315 }
316 //============================================================
317 template <typename vectType, typename eltType, typename propType>
318 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
319  assert(n.isValid());
320  bool isNotDefault;
321  typename vectType::RealType& vect =
322  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
323  this->propType::notifyBeforeSetNodeValue(n);
324 
325  if (isNotDefault)
326  vect.push_back(v);
327  else {
328  typename vectType::RealType tmp(vect);
329  tmp.push_back(v);
330  AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n, tmp);
331  }
332 
333  this->propType::notifyAfterSetNodeValue(n);
334 }
335 //============================================================
336 template <typename vectType, typename eltType, typename propType>
337 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(const node n) {
338  assert(n.isValid());
339  bool isNotDefault;
340  typename vectType::RealType& vect =
341  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
342  this->propType::notifyBeforeSetNodeValue(n);
343  assert(isNotDefault);
344  vect.pop_back();
345  this->propType::notifyAfterSetNodeValue(n);
346 }
347 //============================================================
348 template <typename vectType, typename eltType, typename propType>
349 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(const node n, size_t size, typename eltType::RealType elt) {
350  assert(n.isValid());
351  bool isNotDefault;
352  typename vectType::RealType& vect =
353  AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
354  assert(isNotDefault);
355  this->propType::notifyBeforeSetNodeValue(n);
356  vect.resize(size, elt);
357  this->propType::notifyAfterSetNodeValue(n);
358 }
359 //============================================================
360 template <typename vectType, typename eltType, typename propType>
361 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
362  assert(e.isValid());
363  bool isNotDefault;
364  typename vectType::RealType& vect =
365  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
366  assert(vect.size() > i);
367  this->propType::notifyBeforeSetEdgeValue(e);
368 
369  if (isNotDefault)
370  vect[i] = v;
371  else {
372  typename vectType::RealType tmp(vect);
373  tmp[i] = v;
374  AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
375  }
376 
377  this->propType::notifyAfterSetEdgeValue(e);
378 }
379 //============================================================
380 template <typename vectType, typename eltType, typename propType>
381 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
382 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(const edge e, unsigned int i) const {
383  assert(e.isValid());
384  const typename vectType::RealType& vect =
385  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e);
386  assert(vect.size() > i);
387  return vect[i];
388 }//============================================================
389 template <typename vectType, typename eltType, typename propType>
390 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
391  assert(e.isValid());
392  bool isNotDefault;
393  typename vectType::RealType& vect =
394  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
395  this->propType::notifyBeforeSetEdgeValue(e);
396 
397  if (isNotDefault)
398  vect.push_back(v);
399  else {
400  typename vectType::RealType tmp(vect);
401  tmp.push_back(v);
402  AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
403  }
404 
405  this->propType::notifyAfterSetEdgeValue(e);
406 }
407 //============================================================
408 template <typename vectType, typename eltType, typename propType>
409 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(const edge e) {
410  assert(e.isValid());
411  bool isNotDefault;
412  typename vectType::RealType& vect =
413  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
414  this->propType::notifyBeforeSetEdgeValue(e);
415  assert(isNotDefault);
416  vect.pop_back();
417  this->propType::notifyAfterSetEdgeValue(e);
418 }
419 //============================================================
420 template <typename vectType, typename eltType, typename propType>
421 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt) {
422  assert(e.isValid());
423  bool isNotDefault;
424  typename vectType::RealType& vect =
425  AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
426  assert(isNotDefault);
427  this->propType::notifyBeforeSetEdgeValue(e);
428  vect.resize(size, elt);
429  this->propType::notifyAfterSetEdgeValue(e);
430 }
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