Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
MutableContainer.h
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 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef _TLPMUTABLECONTAINER_
22 #define _TLPMUTABLECONTAINER_
23 
24 #include <deque>
25 #include <iostream>
26 #include <string>
27 #include <cassert>
28 #include <tulip/tulipconf.h>
29 #include <tulip/tuliphash.h>
30 #include <tulip/StoredType.h>
31 #include <tulip/DataSet.h>
32 #include <tulip/Iterator.h>
33 
34 namespace tlp {
35 
36 #ifndef DOXYGEN_NOTFOR_DEVEL
37 //===================================================================
38 // we first define an interface
39 // to make easier the iteration on values
40 // stored in a MutableContainer for the GraphUpdatesRecorder
41 class IteratorValue: public Iterator<unsigned int> {
42 public:
43  IteratorValue() {}
44  virtual ~IteratorValue() {}
45  virtual unsigned int nextValue(DataMem&) = 0;
46 };
47 #endif // DOXYGEN_NOTFOR_DEVEL
48 //===================================================================
49 template <typename TYPE>
50 class MutableContainer {
51  friend class MutableContainerTest;
52  friend class GraphUpdatesRecorder;
53 public:
54  MutableContainer();
55  ~MutableContainer();
56  /**
57  * set the default value
58  */
59  void setAll(const TYPE &value);
60  /**
61  * set the value associated to i
62  */
63  void set(const unsigned int i, const TYPE &value);
64  /**
65  * add val to the value associated to i
66  */
67  void add(const unsigned int i, TYPE val);
68  /**
69  * get the value associated to i
70  */
71  typename StoredType<TYPE>::ReturnedConstValue get(const unsigned int i) const;
72  /**
73  * get the value associated to i and indicates if it is not the default one
74  */
75  typename StoredType<TYPE>::ReturnedValue get(const unsigned int i, bool& isNotDefault) const;
76  /**
77  * get the default value
78  */
79  typename StoredType<TYPE>::ReturnedValue getDefault() const;
80  /**
81  * return if the value associated to i is not the default one
82  */
83  bool hasNonDefaultValue(const unsigned int i) const;
84  /**
85  * return a pointer on an iterator for all the elements whose associated value
86  * is equal to a given value or different from the default value.
87  * A null pointer is returned in case of an iteration on all the elements
88  * whose value is equal to the default value.
89  */
90  Iterator<unsigned int>* findAll(const TYPE &value, bool equal = true) const;
91 private:
92  MutableContainer(const MutableContainer<TYPE> &) {}
93  void operator=(const MutableContainer<TYPE> &) {}
94  typename StoredType<TYPE>::ReturnedConstValue operator[](const unsigned int i) const;
95  void vecttohash();
96  void hashtovect();
97  void compress(unsigned int min, unsigned int max, unsigned int nbElements);
98  inline void vectset(const unsigned int i, typename StoredType<TYPE>::Value value);
99  IteratorValue* findAllValues(const TYPE &value, bool equal = true) const;
100 private:
101  std::deque<typename StoredType<TYPE>::Value> *vData;
102  TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData;
103  unsigned int minIndex,maxIndex;
104  typename StoredType<TYPE>::Value defaultValue;
105  enum State { VECT=0, HASH=1 };
106  State state;
107  unsigned int elementInserted;
108  double ratio;
109  bool compressing;
110 };
111 
112 //===================================================================
113 // we implement 2 templates with IteratorValue as parent class
114 // for the two kinds of storage used in a MutableContainer
115 // one for vector storage
116 template <typename TYPE>
117 class IteratorVect : public tlp::IteratorValue {
118 public:
119  IteratorVect(const TYPE &value, bool equal, std::deque<typename StoredType<TYPE>::Value> *vData, unsigned int minIndex):
120  _value(value),
121  _equal(equal),
122  _pos(minIndex),
123  vData(vData),
124  it(vData->begin()) {
125  while (it!=(*vData).end() &&
126  StoredType<TYPE>::equal((*it), _value) != _equal) {
127  ++it;
128  ++_pos;
129  }
130  }
131  bool hasNext() {
132  return (_pos<UINT_MAX && it!=(*vData).end());
133  }
134  unsigned int next() {
135  unsigned int tmp = _pos;
136 
137  do {
138  ++it;
139  ++_pos;
140  }
141  while (it!=(*vData).end() &&
142  StoredType<TYPE>::equal((*it), _value) != _equal);
143 
144  return tmp;
145  }
146  unsigned int nextValue(DataMem& val) {
147  ((TypedValueContainer<TYPE>&) val).value = StoredType<TYPE>::get(*it);
148  unsigned int pos = _pos;
149 
150  do {
151  ++it;
152  ++_pos;
153  }
154  while (it!=(*vData).end() &&
155  StoredType<TYPE>::equal((*it), _value) != _equal);
156 
157  return pos;
158  }
159 private:
160  const TYPE _value;
161  bool _equal;
162  unsigned int _pos;
163  std::deque<typename StoredType<TYPE>::Value> *vData;
164  typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it;
165 };
166 
167 // one for hash storage
168 template <typename TYPE>
169 class IteratorHash : public IteratorValue {
170 public:
171  IteratorHash(const TYPE &value, bool equal, TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData):
172  _value(value),
173  _equal(equal),
174  hData(hData) {
175  it=(*hData).begin();
176 
177  while (it!=(*hData).end() &&
178  StoredType<TYPE>::equal((*it).second,_value) != _equal)
179  ++it;
180  }
181  bool hasNext() {
182  return (it!=(*hData).end());
183  }
184  unsigned int next() {
185  unsigned int tmp = (*it).first;
186 
187  do {
188  ++it;
189  }
190  while (it!=(*hData).end() &&
191  StoredType<TYPE>::equal((*it).second,_value) != _equal);
192 
193  return tmp;
194  }
195  unsigned int nextValue(DataMem& val) {
196  ((TypedValueContainer<TYPE>&) val).value =
197  StoredType<TYPE>::get((*it).second);
198  unsigned int pos = (*it).first;
199 
200  do {
201  ++it;
202  }
203  while (it!=(*hData).end() &&
204  StoredType<TYPE>::equal((*it).second,_value) != _equal);
205 
206  return pos;
207  }
208 private:
209  const TYPE _value;
210  bool _equal;
211  TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData;
212  typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
213 };
214 
215 #ifndef DOXYGEN_NOTFOR_DEVEL
216 
217 }
218 
219 #include "cxx/MutableContainer.cxx"
220 
221 #endif // DOXYGEN_NOTFOR_DEVEL
222 
223 #endif
224 ///@endcond