Tulip  4.2.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 1 and Inria Bordeaux - Sud Ouest
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 <iostream>
20 #include <cstdlib>
21 
22 template <class Tnode, class Tedge>
24  graph = sg;
25  name = n;
26  nodeDefaultValue = Tnode::defaultValue();
27  edgeDefaultValue = Tedge::defaultValue();
28  nodeProperties.setAll(Tnode::defaultValue());
29  edgeProperties.setAll(Tedge::defaultValue());
30  metaValueCalculator = NULL;
31 }
32 //=============================================================
33 template <class Tnode, class Tedge>
35  return nodeDefaultValue;
36 }
37 //=============================================================
38 template <class Tnode, class Tedge>
40  return edgeDefaultValue;
41 }
42 //=============================================================
43 template <class Tnode, class Tedge>
44 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue tlp::AbstractProperty<Tnode,Tedge>::getNodeValue(const tlp::node n ) const {
45  assert(n.isValid());
46  return nodeProperties.get(n.id);
47 }
48 //=============================================================
49 template <class Tnode, class Tedge>
50 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue tlp::AbstractProperty<Tnode,Tedge>::getEdgeValue(const tlp::edge e) const {
51  assert(e.isValid());
52  return edgeProperties.get(e.id);
53 }
54 //=============================================================
55 template <class Tnode, class Tedge>
56 void tlp::AbstractProperty<Tnode,Tedge>::setNodeValue(const tlp::node n,const typename Tnode::RealType &v) {
57  assert(n.isValid());
58  notifyBeforeSetNodeValue(n);
59  nodeProperties.set(n.id,v);
60  notifyAfterSetNodeValue(n);
61 }
62 //=============================================================
63 template <class Tnode, class Tedge>
64 void tlp::AbstractProperty<Tnode,Tedge>::setEdgeValue(const tlp::edge e,const typename Tedge::RealType &v) {
65  assert(e.isValid());
66  notifyBeforeSetEdgeValue(e);
67  edgeProperties.set(e.id,v);
68  notifyAfterSetEdgeValue(e);
69 }
70 //=============================================================
71 template <class Tnode, class Tedge>
72 void tlp::AbstractProperty<Tnode,Tedge>::setAllNodeValue(const typename Tnode::RealType &v) {
73  notifyBeforeSetAllNodeValue();
74  nodeDefaultValue = v;
75  nodeProperties.setAll(v);
76  notifyAfterSetAllNodeValue();
77 }
78 //============================================================
79 template <class Tnode, class Tedge>
80 void tlp::AbstractProperty<Tnode,Tedge>::setAllEdgeValue(const typename Tedge::RealType &v) {
81  notifyBeforeSetAllEdgeValue();
82  edgeDefaultValue = v;
83  edgeProperties.setAll(v);
84  notifyAfterSetAllEdgeValue();
85 }
86 
87 template <class Tnode, class Tedge>
89  const typename Tnode::RealType& n1Value = getNodeValue(n1);
90  const typename Tnode::RealType& n2Value = getNodeValue(n2);
91  return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
92 }
93 //============================================================
94 template <class Tnode, class Tedge>
96  const typename Tedge::RealType& e1Value = getEdgeValue(e1);
97  const typename Tedge::RealType& e2Value = getEdgeValue(e2);
98  return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
99 }
100 //============================================================
101 // define template iterator class to iterate over graph elts
102 // belonging to a given graph instance
103 // used by the two methods below
104 #ifndef DOXYGEN_NOTFOR_DEVEL
105 template <typename ELT_TYPE>
106 class GraphEltIterator :public tlp::Iterator<ELT_TYPE> {
107 public:
108  ELT_TYPE next() {
109  ELT_TYPE tmp = curElt;
110 
111  if ((_hasnext = it->hasNext())) {
112  curElt = it->next();
113 
114  while (!(_hasnext = (!graph || graph->isElement(curElt)))) {
115  if (!it->hasNext()) break;
116 
117  curElt=it->next();
118  }
119  }
120 
121  return tmp;
122  }
123  GraphEltIterator(const tlp::Graph* g, tlp::Iterator<ELT_TYPE>* itN)
124  :it(itN), graph(g), curElt(ELT_TYPE()), _hasnext(false) {
125  next();
126  }
127 
128  bool hasNext() {
129  return (_hasnext);
130  }
131  ~GraphEltIterator() {
132  delete it;
133  }
134 
135 private:
136  tlp::Iterator<ELT_TYPE> *it;
137  const tlp::Graph* graph;
138  ELT_TYPE curElt;
139  bool _hasnext;
140 };
141 #endif // DOXYGEN_NOTFOR_DEVEL
142 //============================================================
143 template <class Tnode, class Tedge>
145  tlp::Iterator<tlp::node> *it =
146  new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue, false));
147 
148  if (name.empty())
149  // we always need to check that nodes belong to graph
150  // for non registered properties, because deleted nodes are not erased
151  // from them
152  return new GraphEltIterator<tlp::node>(g != NULL ? g : graph, it);
153 
154  return ((g == NULL) || (g == graph)) ? it : new GraphEltIterator<tlp::node>(g, it);
155 }
156 //============================================================
157 template <class Tnode, class Tedge>
159  tlp::Iterator<tlp::edge>* it =
160  new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue, false));
161 
162  if (name.empty())
163  // we always need to check that edges belong to graph
164  // for non registered properties, because deleted edges are not erased
165  // from them
166  return new GraphEltIterator<tlp::edge>(g != NULL ? g : graph, it);
167 
168  return ((g == NULL) || (g == graph)) ? it : new GraphEltIterator<tlp::edge>(g, it);
169 }
170 //============================================================
171 template <typename vectType, typename eltType>
172 tlp::AbstractVectorProperty<vectType, eltType>::AbstractVectorProperty(tlp::Graph* g, std::string name) :AbstractProperty<vectType, vectType>(g, name) {}
173 //============================================================
174 template <typename vectType, typename eltType>
175 void tlp::AbstractVectorProperty<vectType, eltType>::setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
176  assert(n.isValid());
177  bool isNotDefault;
178  typename vectType::RealType& vect =
179  AbstractProperty<vectType, vectType>::nodeProperties.get(n, isNotDefault);
180  assert(vect.size() > i);
181  this->notifyBeforeSetNodeValue(n);
182 
183  if (isNotDefault)
184  vect[i] = v;
185  else {
186  typename vectType::RealType tmp(vect);
187  tmp[i] = v;
188  AbstractProperty<vectType, vectType>::nodeProperties.set(n.id, tmp);
189  }
190 
191  this->notifyAfterSetNodeValue(n);
192 }
193 //============================================================
194 template <typename vectType, typename eltType>
195 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
196 tlp::AbstractVectorProperty<vectType, eltType>::getNodeEltValue(const node n, unsigned int i) const {
197  assert(n.isValid());
198  const typename vectType::RealType& vect =
199  AbstractProperty<vectType, vectType>::nodeProperties.get(n);
200  assert(vect.size() > i);
201  return vect[i];
202 }
203 //============================================================
204 template <typename vectType, typename eltType>
205 void tlp::AbstractVectorProperty<vectType, eltType>::pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
206  assert(n.isValid());
207  bool isNotDefault;
208  typename vectType::RealType& vect =
209  AbstractProperty<vectType, vectType>::nodeProperties.get(n, isNotDefault);
210  this->notifyBeforeSetNodeValue(n);
211 
212  if (isNotDefault)
213  vect.push_back(v);
214  else {
215  typename vectType::RealType tmp(vect);
216  tmp.push_back(v);
217  AbstractProperty<vectType, vectType>::nodeProperties.set(n, tmp);
218  }
219 
220  this->notifyAfterSetNodeValue(n);
221 }
222 //============================================================
223 template <typename vectType, typename eltType>
224 void tlp::AbstractVectorProperty<vectType, eltType>::popBackNodeEltValue(const node n) {
225  assert(n.isValid());
226  bool isNotDefault;
227  typename vectType::RealType& vect =
228  AbstractProperty<vectType, vectType>::nodeProperties.get(n, isNotDefault);
229  this->notifyBeforeSetNodeValue(n);
230  assert(isNotDefault);
231  vect.pop_back();
232  this->notifyAfterSetNodeValue(n);
233 }
234 //============================================================
235 template <typename vectType, typename eltType>
236 void tlp::AbstractVectorProperty<vectType, eltType>::resizeNodeValue(const node n, size_t size, typename eltType::RealType elt) {
237  assert(n.isValid());
238  bool isNotDefault;
239  typename vectType::RealType& vect =
240  AbstractProperty<vectType, vectType>::nodeProperties.get(n, isNotDefault);
241  assert(isNotDefault);
242  this->notifyBeforeSetNodeValue(n);
243  vect.resize(size, elt);
244  this->notifyAfterSetNodeValue(n);
245 }
246 //============================================================
247 template <typename vectType, typename eltType>
248 void tlp::AbstractVectorProperty<vectType, eltType>::setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
249  assert(e.isValid());
250  bool isNotDefault;
251  typename vectType::RealType& vect =
252  AbstractProperty<vectType, vectType>::edgeProperties.get(e, isNotDefault);
253  assert(vect.size() > i);
254  this->notifyBeforeSetEdgeValue(e);
255 
256  if (isNotDefault)
257  vect[i] = v;
258  else {
259  typename vectType::RealType tmp(vect);
260  tmp[i] = v;
261  AbstractProperty<vectType, vectType>::edgeProperties.set(e, tmp);
262  }
263 
264  this->notifyAfterSetEdgeValue(e);
265 }
266 //============================================================
267 template <typename vectType, typename eltType>
268 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
269 tlp::AbstractVectorProperty<vectType, eltType>::getEdgeEltValue(const edge e, unsigned int i) const {
270  assert(e.isValid());
271  const typename vectType::RealType& vect =
272  AbstractProperty<vectType, vectType>::edgeProperties.get(e);
273  assert(vect.size() > i);
274  return vect[i];
275 }//============================================================
276 template <typename vectType, typename eltType>
277 void tlp::AbstractVectorProperty<vectType, eltType>::pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
278  assert(e.isValid());
279  bool isNotDefault;
280  typename vectType::RealType& vect =
281  AbstractProperty<vectType, vectType>::edgeProperties.get(e, isNotDefault);
282  this->notifyBeforeSetEdgeValue(e);
283 
284  if (isNotDefault)
285  vect.push_back(v);
286  else {
287  typename vectType::RealType tmp(vect);
288  tmp.push_back(v);
289  AbstractProperty<vectType, vectType>::edgeProperties.set(e, tmp);
290  }
291 
292  this->notifyAfterSetEdgeValue(e);
293 }
294 //============================================================
295 template <typename vectType, typename eltType>
296 void tlp::AbstractVectorProperty<vectType, eltType>::popBackEdgeEltValue(const edge e) {
297  assert(e.isValid());
298  bool isNotDefault;
299  typename vectType::RealType& vect =
300  AbstractProperty<vectType, vectType>::edgeProperties.get(e, isNotDefault);
301  this->notifyBeforeSetEdgeValue(e);
302  assert(isNotDefault);
303  vect.pop_back();
304  this->notifyAfterSetEdgeValue(e);
305 }
306 //============================================================
307 template <typename vectType, typename eltType>
308 void tlp::AbstractVectorProperty<vectType, eltType>::resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt) {
309  assert(e.isValid());
310  bool isNotDefault;
311  typename vectType::RealType& vect =
312  AbstractProperty<vectType, vectType>::edgeProperties.get(e, isNotDefault);
313  assert(isNotDefault);
314  this->notifyBeforeSetEdgeValue(e);
315  vect.resize(size, elt);
316  this->notifyAfterSetEdgeValue(e);
317 }