Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/MutableContainer.h
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 ///@cond DOXYGEN_HIDDEN
00020 
00021 #ifndef _TLPMUTABLECONTAINER_
00022 #define _TLPMUTABLECONTAINER_
00023 
00024 #include <deque>
00025 #include <iostream>
00026 #include <string>
00027 #include <cassert>
00028 #include <climits>
00029 #include <cstring>
00030 #include <tulip/tulipconf.h>
00031 #include <tulip/tuliphash.h>
00032 #include <tulip/StoredType.h>
00033 #include <tulip/DataSet.h>
00034 #include <tulip/Iterator.h>
00035 
00036 namespace tlp {
00037 
00038 #ifndef DOXYGEN_NOTFOR_DEVEL
00039 //===================================================================
00040 // we first define an interface
00041 // to make easier the iteration on values
00042 // stored in a MutableContainer for the GraphUpdatesRecorder
00043 class IteratorValue: public Iterator<unsigned int> {
00044 public:
00045   IteratorValue() {}
00046   virtual ~IteratorValue() {}
00047   virtual unsigned int nextValue(DataMem&) = 0;
00048 };
00049 #endif // DOXYGEN_NOTFOR_DEVEL
00050 //===================================================================
00051 template <typename TYPE>
00052 class MutableContainer {
00053   friend class MutableContainerTest;
00054   friend class GraphUpdatesRecorder;
00055 public:
00056   MutableContainer();
00057   ~MutableContainer();
00058   /**
00059    * set the default value
00060    */
00061   void setAll(const TYPE &value);
00062   /**
00063    * set the value associated to i
00064    */
00065   void set(const unsigned int i, const TYPE &value);
00066   /**
00067    * add val to the value associated to i
00068    */
00069   void add(const unsigned int i, TYPE val);
00070   /**
00071    * get the value associated to i
00072    */
00073   typename StoredType<TYPE>::ReturnedConstValue get(const unsigned int i) const;
00074   /**
00075    * get the value associated to i and indicates if it is not the default one
00076    */
00077   typename StoredType<TYPE>::ReturnedValue get(const unsigned int i, bool& isNotDefault) const;
00078   /**
00079    * get the default value
00080    */
00081   typename StoredType<TYPE>::ReturnedValue getDefault() const;
00082   /**
00083    * return if the value associated to i is not the default one
00084    */
00085   bool hasNonDefaultValue(const unsigned int i) const;
00086   /**
00087    * return a pointer on an iterator for all the elements whose associated value
00088    * is equal to a given value or different from the default value.
00089    * A null pointer is returned in case of an iteration on all the elements
00090    * whose value is equal to the default value.
00091    */
00092   Iterator<unsigned int>* findAll(const TYPE &value, bool equal = true) const;
00093   /**
00094    * return the number of non default values
00095    */
00096   unsigned int numberOfNonDefaultValues() const;
00097 private:
00098   MutableContainer(const MutableContainer<TYPE> &) {}
00099   void operator=(const MutableContainer<TYPE> &) {}
00100   typename StoredType<TYPE>::ReturnedConstValue operator[](const unsigned int i) const;
00101   void vecttohash();
00102   void hashtovect();
00103   void compress(unsigned int min, unsigned int max, unsigned int nbElements);
00104   inline void vectset(const unsigned int i, typename StoredType<TYPE>::Value value);
00105   IteratorValue* findAllValues(const TYPE &value, bool equal = true) const;
00106 private:
00107   std::deque<typename StoredType<TYPE>::Value> *vData;
00108   TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData;
00109   unsigned int minIndex,maxIndex;
00110   typename StoredType<TYPE>::Value defaultValue;
00111   enum State { VECT=0, HASH=1 };
00112   State state;
00113   unsigned int elementInserted;
00114   double ratio;
00115   bool compressing;
00116 };
00117 
00118 //===================================================================
00119 // we implement 2 templates with IteratorValue as parent class
00120 // for the two kinds of storage used in a MutableContainer
00121 // one for vector storage
00122 template <typename TYPE>
00123 class IteratorVect : public tlp::IteratorValue {
00124 public:
00125   IteratorVect(const TYPE &value, bool equal, std::deque<typename StoredType<TYPE>::Value> *vData, unsigned int minIndex):
00126     _value(value),
00127     _equal(equal),
00128     _pos(minIndex),
00129     vData(vData),
00130     it(vData->begin()) {
00131     while (it!=(*vData).end() &&
00132            StoredType<TYPE>::equal((*it), _value) != _equal) {
00133       ++it;
00134       ++_pos;
00135     }
00136   }
00137   bool hasNext() {
00138     return (_pos<UINT_MAX && it!=(*vData).end());
00139   }
00140   unsigned int next() {
00141     unsigned int tmp = _pos;
00142 
00143     do {
00144       ++it;
00145       ++_pos;
00146     }
00147     while (it!=(*vData).end() &&
00148            StoredType<TYPE>::equal((*it), _value) != _equal);
00149 
00150     return tmp;
00151   }
00152   unsigned int nextValue(DataMem& val) {
00153     ((TypedValueContainer<TYPE>&) val).value = StoredType<TYPE>::get(*it);
00154     unsigned int pos = _pos;
00155 
00156     do {
00157       ++it;
00158       ++_pos;
00159     }
00160     while (it!=(*vData).end() &&
00161            StoredType<TYPE>::equal((*it), _value) != _equal);
00162 
00163     return pos;
00164   }
00165 private:
00166   const TYPE _value;
00167   bool _equal;
00168   unsigned int _pos;
00169   std::deque<typename StoredType<TYPE>::Value> *vData;
00170   typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it;
00171 };
00172 
00173 // one for hash storage
00174 template <typename TYPE>
00175 class IteratorHash : public IteratorValue {
00176 public:
00177   IteratorHash(const TYPE &value, bool equal, TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData):
00178     _value(value),
00179     _equal(equal),
00180     hData(hData) {
00181     it=(*hData).begin();
00182 
00183     while (it!=(*hData).end() &&
00184            StoredType<TYPE>::equal((*it).second,_value) != _equal)
00185       ++it;
00186   }
00187   bool hasNext() {
00188     return (it!=(*hData).end());
00189   }
00190   unsigned int next() {
00191     unsigned int tmp = (*it).first;
00192 
00193     do {
00194       ++it;
00195     }
00196     while (it!=(*hData).end() &&
00197            StoredType<TYPE>::equal((*it).second,_value) != _equal);
00198 
00199     return tmp;
00200   }
00201   unsigned int nextValue(DataMem& val) {
00202     ((TypedValueContainer<TYPE>&) val).value =
00203       StoredType<TYPE>::get((*it).second);
00204     unsigned int pos = (*it).first;
00205 
00206     do {
00207       ++it;
00208     }
00209     while (it!=(*hData).end() &&
00210            StoredType<TYPE>::equal((*it).second,_value) != _equal);
00211 
00212     return pos;
00213   }
00214 private:
00215   const TYPE _value;
00216   bool _equal;
00217   TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData;
00218   typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
00219 };
00220 
00221 #ifndef DOXYGEN_NOTFOR_DEVEL
00222 
00223 }
00224 
00225 #include "cxx/MutableContainer.cxx"
00226 
00227 #endif // DOXYGEN_NOTFOR_DEVEL
00228 
00229 #endif
00230 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties