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