Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/cxx/AbstractProperty.cxx
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 #include <cstdlib>
00020 
00021 template <class Tnode, class Tedge, class Tprop>
00022 tlp::AbstractProperty<Tnode,Tedge,Tprop>::AbstractProperty(tlp::Graph *sg, const std::string& n) {
00023   Tprop::graph = sg;
00024   Tprop::name = n;
00025   nodeDefaultValue = Tnode::defaultValue();
00026   edgeDefaultValue = Tedge::defaultValue();
00027   nodeProperties.setAll(Tnode::defaultValue());
00028   edgeProperties.setAll(Tedge::defaultValue());
00029   Tprop::metaValueCalculator = NULL;
00030 }
00031 //=============================================================
00032 template <class Tnode, class Tedge, class Tprop>
00033 typename Tnode::RealType tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNodeDefaultValue() const {
00034   return nodeDefaultValue;
00035 }
00036 //=============================================================
00037 template <class Tnode, class Tedge, class Tprop>
00038 typename Tedge::RealType tlp::AbstractProperty<Tnode,Tedge,Tprop>::getEdgeDefaultValue() const {
00039   return edgeDefaultValue;
00040 }
00041 //=============================================================
00042 template <class Tnode, class Tedge, class Tprop>
00043 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNodeValue(const tlp::node n ) const {
00044   assert(n.isValid());
00045   return nodeProperties.get(n.id);
00046 }
00047 //=============================================================
00048 template <class Tnode, class Tedge, class Tprop>
00049 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue tlp::AbstractProperty<Tnode,Tedge,Tprop>::getEdgeValue(const tlp::edge e) const {
00050   assert(e.isValid());
00051   return edgeProperties.get(e.id);
00052 }
00053 //=============================================================
00054 template <class Tnode, class Tedge, class Tprop>
00055 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setNodeValue(const tlp::node n,const typename Tnode::RealType &v) {
00056   assert(n.isValid());
00057   Tprop::notifyBeforeSetNodeValue(n);
00058   nodeProperties.set(n.id,v);
00059   Tprop::notifyAfterSetNodeValue(n);
00060 }
00061 //=============================================================
00062 template <class Tnode, class Tedge, class Tprop>
00063 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setEdgeValue(const tlp::edge e,const typename Tedge::RealType &v) {
00064   assert(e.isValid());
00065   Tprop::notifyBeforeSetEdgeValue(e);
00066   edgeProperties.set(e.id,v);
00067   Tprop::notifyAfterSetEdgeValue(e);
00068 }
00069 //=============================================================
00070 template <class Tnode, class Tedge, class Tprop>
00071 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setAllNodeValue(const typename Tnode::RealType &v) {
00072   Tprop::notifyBeforeSetAllNodeValue();
00073   nodeDefaultValue = v;
00074   nodeProperties.setAll(v);
00075   Tprop::notifyAfterSetAllNodeValue();
00076 }
00077 //============================================================
00078 template <class Tnode, class Tedge, class Tprop>
00079 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::setAllEdgeValue(const typename Tedge::RealType &v) {
00080   Tprop::notifyBeforeSetAllEdgeValue();
00081   edgeDefaultValue = v;
00082   edgeProperties.setAll(v);
00083   Tprop::notifyAfterSetAllEdgeValue();
00084 }
00085 
00086 template <class Tnode, class Tedge, class Tprop>
00087 int tlp::AbstractProperty<Tnode,Tedge,Tprop>::compare(const node n1,const node n2)const {
00088   const typename Tnode::RealType& n1Value = getNodeValue(n1);
00089   const typename Tnode::RealType& n2Value = getNodeValue(n2);
00090   return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
00091 }
00092 //============================================================
00093 template <class Tnode, class Tedge, class Tprop>
00094 int tlp::AbstractProperty<Tnode,Tedge,Tprop>::compare(const edge e1,const edge e2)const {
00095   const typename Tedge::RealType& e1Value = getEdgeValue(e1);
00096   const typename Tedge::RealType& e2Value = getEdgeValue(e2);
00097   return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
00098 }
00099 //============================================================
00100 // define template iterator class to iterate over graph elts
00101 // belonging to a given graph instance
00102 // used by the two methods below
00103 #ifndef DOXYGEN_NOTFOR_DEVEL
00104 template <typename ELT_TYPE>
00105 class GraphEltIterator :public tlp::Iterator<ELT_TYPE> {
00106 public:
00107   ELT_TYPE next() {
00108     ELT_TYPE tmp = curElt;
00109 
00110     if ((_hasnext = it->hasNext())) {
00111       curElt = it->next();
00112 
00113       while (!(_hasnext = (!graph || graph->isElement(curElt)))) {
00114         if (!it->hasNext()) break;
00115 
00116         curElt=it->next();
00117       }
00118     }
00119 
00120     return tmp;
00121   }
00122   GraphEltIterator(const tlp::Graph* g, tlp::Iterator<ELT_TYPE>* itN)
00123     :it(itN), graph(g), curElt(ELT_TYPE()), _hasnext(false) {
00124     next();
00125   }
00126 
00127   bool hasNext() {
00128     return (_hasnext);
00129   }
00130   ~GraphEltIterator() {
00131     delete it;
00132   }
00133 
00134 private:
00135   tlp::Iterator<ELT_TYPE> *it;
00136   const tlp::Graph* graph;
00137   ELT_TYPE curElt;
00138   bool _hasnext;
00139 };
00140 #endif // DOXYGEN_NOTFOR_DEVEL
00141 //============================================================
00142 template <class Tnode, class Tedge, class Tprop>
00143 tlp::Iterator<tlp::node>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedNodes(const Graph* g) const {
00144   tlp::Iterator<tlp::node> *it =
00145     new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue, false));
00146 
00147   if (Tprop::name.empty())
00148     // we always need to check that nodes belong to graph
00149     // for non registered properties, because deleted nodes are not erased
00150     // from them
00151     return new GraphEltIterator<tlp::node>(g != NULL ? g : Tprop::graph, it);
00152 
00153   return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::node>(g, it);
00154 }
00155 //============================================================
00156 template <class Tnode, class Tedge, class Tprop>
00157 unsigned int tlp::AbstractProperty<Tnode,Tedge,Tprop>::numberOfNonDefaultValuatedNodes() const {
00158   return nodeProperties.numberOfNonDefaultValues();
00159 }
00160 //============================================================
00161 template <class Tnode, class Tedge, class Tprop>
00162 unsigned int tlp::AbstractProperty<Tnode,Tedge,Tprop>::nodeValueSize() const {
00163   return Tnode::valueSize();
00164 }
00165 //============================================================
00166 template <class Tnode, class Tedge, class Tprop>
00167 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::writeNodeDefaultValue(std::ostream& oss) const {
00168   Tnode::writeb(oss, nodeDefaultValue);
00169 }
00170 //============================================================
00171 template <class Tnode, class Tedge, class Tprop>
00172 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::writeNodeValue(std::ostream& oss,
00173     node n) const {
00174   assert(n.isValid());
00175   Tnode::writeb(oss, nodeProperties.get(n.id));
00176 }
00177 //============================================================
00178 template <class Tnode, class Tedge, class Tprop>
00179 bool tlp::AbstractProperty<Tnode,Tedge,Tprop>::readNodeDefaultValue(std::istream& iss) {
00180   if (Tnode::readb(iss, nodeDefaultValue)) {
00181     nodeProperties.setAll(nodeDefaultValue);
00182     return true;
00183   }
00184 
00185   return false;
00186 }
00187 //============================================================
00188 template <class Tnode, class Tedge, class Tprop>
00189 bool tlp::AbstractProperty<Tnode,Tedge,Tprop>::readNodeValue(std::istream& iss,
00190     node n) {
00191   typename Tnode::RealType val;
00192 
00193   if (Tnode::readb(iss, val)) {
00194     nodeProperties.set(n.id, val);
00195     return true;
00196   }
00197 
00198   return false;
00199 }
00200 //============================================================
00201 template <class Tnode, class Tedge, class Tprop>
00202 tlp::Iterator<tlp::edge>* tlp::AbstractProperty<Tnode,Tedge,Tprop>::getNonDefaultValuatedEdges(const Graph* g) const {
00203   tlp::Iterator<tlp::edge>* it =
00204     new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue, false));
00205 
00206   if (Tprop::name.empty())
00207     // we always need to check that edges belong to graph
00208     // for non registered properties, because deleted edges are not erased
00209     // from them
00210     return new GraphEltIterator<tlp::edge>(g != NULL ? g : Tprop::graph, it);
00211 
00212   return ((g == NULL) || (g == Tprop::graph)) ? it : new GraphEltIterator<tlp::edge>(g, it);
00213 }
00214 //============================================================
00215 template <class Tnode, class Tedge, class Tprop>
00216 unsigned int tlp::AbstractProperty<Tnode,Tedge,Tprop>::numberOfNonDefaultValuatedEdges() const {
00217   return edgeProperties.numberOfNonDefaultValues();
00218 }
00219 //============================================================
00220 template <class Tnode, class Tedge, class Tprop>
00221 unsigned int tlp::AbstractProperty<Tnode,Tedge,Tprop>::edgeValueSize() const {
00222   return Tedge::valueSize();
00223 }
00224 //============================================================
00225 template <class Tnode, class Tedge, class Tprop>
00226 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::writeEdgeDefaultValue(std::ostream& oss) const {
00227   Tedge::writeb(oss, edgeDefaultValue);
00228 }
00229 //============================================================
00230 template <class Tnode, class Tedge, class Tprop>
00231 void tlp::AbstractProperty<Tnode,Tedge,Tprop>::writeEdgeValue(std::ostream& oss,
00232     edge e) const {
00233   assert(e.isValid());
00234   Tedge::writeb(oss, edgeProperties.get(e.id));
00235 }
00236 //============================================================
00237 template <class Tnode, class Tedge, class Tprop>
00238 bool tlp::AbstractProperty<Tnode,Tedge,Tprop>::readEdgeDefaultValue(std::istream& iss) {
00239   if (Tedge::readb(iss, edgeDefaultValue)) {
00240     edgeProperties.setAll(edgeDefaultValue);
00241     return true;
00242   }
00243 
00244   return false;
00245 }
00246 //============================================================
00247 template <class Tnode, class Tedge, class Tprop>
00248 bool tlp::AbstractProperty<Tnode,Tedge,Tprop>::readEdgeValue(std::istream& iss,
00249     edge e) {
00250   typename Tedge::RealType val;
00251 
00252   if (Tedge::readb(iss, val)) {
00253     edgeProperties.set(e.id, val);
00254     return true;
00255   }
00256 
00257   return false;
00258 }
00259 //============================================================
00260 template <typename vectType, typename eltType, typename propType>
00261 tlp::AbstractVectorProperty<vectType, eltType, propType>::AbstractVectorProperty(tlp::Graph* g, const std::string& name) :AbstractProperty<vectType, vectType, propType>(g, name) {}
00262 //============================================================
00263 template <typename vectType, typename eltType, typename propType>
00264 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(const node n, const std::string& s, char openChar, char sepChar, char closeChar) {
00265   typename vectType::RealType v;
00266   std::istringstream iss(s);
00267 
00268   if (!vectType::read(iss, v, openChar, sepChar, closeChar))
00269     return false;
00270 
00271   this->setNodeValue(n, v);
00272   return true;
00273 }
00274 //============================================================
00275 template <typename vectType, typename eltType, typename propType>
00276 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(const edge e, const std::string& s, char openChar, char sepChar, char closeChar) {
00277   typename vectType::RealType v;
00278   std::istringstream iss(s);
00279 
00280   if (!vectType::read(iss, v, openChar, sepChar, closeChar))
00281     return false;
00282 
00283   this->setEdgeValue(e, v);
00284   return true;
00285 }
00286 //============================================================
00287 template <typename vectType, typename eltType, typename propType>
00288 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
00289   assert(n.isValid());
00290   bool isNotDefault;
00291   typename vectType::RealType& vect =
00292     AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
00293   assert(vect.size() > i);
00294   this->propType::notifyBeforeSetNodeValue(n);
00295 
00296   if (isNotDefault)
00297     vect[i] = v;
00298   else {
00299     typename vectType::RealType tmp(vect);
00300     tmp[i] = v;
00301     AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n.id, tmp);
00302   }
00303 
00304   this->propType::notifyAfterSetNodeValue(n);
00305 }
00306 //============================================================
00307 template <typename vectType, typename eltType, typename propType>
00308 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
00309 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(const node n, unsigned int i) const {
00310   assert(n.isValid());
00311   const typename vectType::RealType& vect =
00312     AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n);
00313   assert(vect.size() > i);
00314   return vect[i];
00315 }
00316 //============================================================
00317 template <typename vectType, typename eltType, typename propType>
00318 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
00319   assert(n.isValid());
00320   bool isNotDefault;
00321   typename vectType::RealType& vect =
00322     AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
00323   this->propType::notifyBeforeSetNodeValue(n);
00324 
00325   if (isNotDefault)
00326     vect.push_back(v);
00327   else {
00328     typename vectType::RealType tmp(vect);
00329     tmp.push_back(v);
00330     AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n, tmp);
00331   }
00332 
00333   this->propType::notifyAfterSetNodeValue(n);
00334 }
00335 //============================================================
00336 template <typename vectType, typename eltType, typename propType>
00337 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(const node n) {
00338   assert(n.isValid());
00339   bool isNotDefault;
00340   typename vectType::RealType& vect =
00341     AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
00342   this->propType::notifyBeforeSetNodeValue(n);
00343   assert(isNotDefault);
00344   vect.pop_back();
00345   this->propType::notifyAfterSetNodeValue(n);
00346 }
00347 //============================================================
00348 template <typename vectType, typename eltType, typename propType>
00349 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(const node n, size_t size, typename eltType::RealType elt) {
00350   assert(n.isValid());
00351   bool isNotDefault;
00352   typename vectType::RealType& vect =
00353     AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
00354   assert(isNotDefault);
00355   this->propType::notifyBeforeSetNodeValue(n);
00356   vect.resize(size, elt);
00357   this->propType::notifyAfterSetNodeValue(n);
00358 }
00359 //============================================================
00360 template <typename vectType, typename eltType, typename propType>
00361 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
00362   assert(e.isValid());
00363   bool isNotDefault;
00364   typename vectType::RealType& vect =
00365     AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
00366   assert(vect.size() > i);
00367   this->propType::notifyBeforeSetEdgeValue(e);
00368 
00369   if (isNotDefault)
00370     vect[i] = v;
00371   else {
00372     typename vectType::RealType tmp(vect);
00373     tmp[i] = v;
00374     AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
00375   }
00376 
00377   this->propType::notifyAfterSetEdgeValue(e);
00378 }
00379 //============================================================
00380 template <typename vectType, typename eltType, typename propType>
00381 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
00382 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(const edge e, unsigned int i) const {
00383   assert(e.isValid());
00384   const typename vectType::RealType& vect =
00385     AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e);
00386   assert(vect.size() > i);
00387   return vect[i];
00388 }//============================================================
00389 template <typename vectType, typename eltType, typename propType>
00390 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
00391   assert(e.isValid());
00392   bool isNotDefault;
00393   typename vectType::RealType& vect =
00394     AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
00395   this->propType::notifyBeforeSetEdgeValue(e);
00396 
00397   if (isNotDefault)
00398     vect.push_back(v);
00399   else {
00400     typename vectType::RealType tmp(vect);
00401     tmp.push_back(v);
00402     AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
00403   }
00404 
00405   this->propType::notifyAfterSetEdgeValue(e);
00406 }
00407 //============================================================
00408 template <typename vectType, typename eltType, typename propType>
00409 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(const edge e) {
00410   assert(e.isValid());
00411   bool isNotDefault;
00412   typename vectType::RealType& vect =
00413     AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
00414   this->propType::notifyBeforeSetEdgeValue(e);
00415   assert(isNotDefault);
00416   vect.pop_back();
00417   this->propType::notifyAfterSetEdgeValue(e);
00418 }
00419 //============================================================
00420 template <typename vectType, typename eltType, typename propType>
00421 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt) {
00422   assert(e.isValid());
00423   bool isNotDefault;
00424   typename vectType::RealType& vect =
00425     AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
00426   assert(isNotDefault);
00427   this->propType::notifyBeforeSetEdgeValue(e);
00428   vect.resize(size, elt);
00429   this->propType::notifyAfterSetEdgeValue(e);
00430 }
 All Classes Files Functions Variables Enumerations Enumerator Properties