Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces 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 <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 private:
94  MutableContainer(const MutableContainer<TYPE> &) {}
95  void operator=(const MutableContainer<TYPE> &) {}
96  typename StoredType<TYPE>::ReturnedConstValue operator[](const unsigned int i) const;
97  void vecttohash();
98  void hashtovect();
99  void compress(unsigned int min, unsigned int max, unsigned int nbElements);
100  inline void vectset(const unsigned int i, typename StoredType<TYPE>::Value value);
101  IteratorValue* findAllValues(const TYPE &value, bool equal = true) const;
102 private:
103  std::deque<typename StoredType<TYPE>::Value> *vData;
104  TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData;
105  unsigned int minIndex,maxIndex;
106  typename StoredType<TYPE>::Value defaultValue;
107  enum State { VECT=0, HASH=1 };
108  State state;
109  unsigned int elementInserted;
110  double ratio;
111  bool compressing;
112 };
113 
114 //===================================================================
115 // we implement 2 templates with IteratorValue as parent class
116 // for the two kinds of storage used in a MutableContainer
117 // one for vector storage
118 template <typename TYPE>
119 class IteratorVect : public tlp::IteratorValue {
120 public:
121  IteratorVect(const TYPE &value, bool equal, std::deque<typename StoredType<TYPE>::Value> *vData, unsigned int minIndex):
122  _value(value),
123  _equal(equal),
124  _pos(minIndex),
125  vData(vData),
126  it(vData->begin()) {
127  while (it!=(*vData).end() &&
128  StoredType<TYPE>::equal((*it), _value) != _equal) {
129  ++it;
130  ++_pos;
131  }
132  }
133  bool hasNext() {
134  return (_pos<UINT_MAX && it!=(*vData).end());
135  }
136  unsigned int next() {
137  unsigned int tmp = _pos;
138 
139  do {
140  ++it;
141  ++_pos;
142  }
143  while (it!=(*vData).end() &&
144  StoredType<TYPE>::equal((*it), _value) != _equal);
145 
146  return tmp;
147  }
148  unsigned int nextValue(DataMem& val) {
149  ((TypedValueContainer<TYPE>&) val).value = StoredType<TYPE>::get(*it);
150  unsigned int pos = _pos;
151 
152  do {
153  ++it;
154  ++_pos;
155  }
156  while (it!=(*vData).end() &&
157  StoredType<TYPE>::equal((*it), _value) != _equal);
158 
159  return pos;
160  }
161 private:
162  const TYPE _value;
163  bool _equal;
164  unsigned int _pos;
165  std::deque<typename StoredType<TYPE>::Value> *vData;
166  typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it;
167 };
168 
169 // one for hash storage
170 template <typename TYPE>
171 class IteratorHash : public IteratorValue {
172 public:
173  IteratorHash(const TYPE &value, bool equal, TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData):
174  _value(value),
175  _equal(equal),
176  hData(hData) {
177  it=(*hData).begin();
178 
179  while (it!=(*hData).end() &&
180  StoredType<TYPE>::equal((*it).second,_value) != _equal)
181  ++it;
182  }
183  bool hasNext() {
184  return (it!=(*hData).end());
185  }
186  unsigned int next() {
187  unsigned int tmp = (*it).first;
188 
189  do {
190  ++it;
191  }
192  while (it!=(*hData).end() &&
193  StoredType<TYPE>::equal((*it).second,_value) != _equal);
194 
195  return tmp;
196  }
197  unsigned int nextValue(DataMem& val) {
198  ((TypedValueContainer<TYPE>&) val).value =
199  StoredType<TYPE>::get((*it).second);
200  unsigned int pos = (*it).first;
201 
202  do {
203  ++it;
204  }
205  while (it!=(*hData).end() &&
206  StoredType<TYPE>::equal((*it).second,_value) != _equal);
207 
208  return pos;
209  }
210 private:
211  const TYPE _value;
212  bool _equal;
213  TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value> *hData;
214  typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
215 };
216 
217 #ifndef DOXYGEN_NOTFOR_DEVEL
218 
219 }
220 
221 #include "cxx/MutableContainer.cxx"
222 
223 #endif // DOXYGEN_NOTFOR_DEVEL
224 
225 #endif
226 ///@endcond