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