20 template <
typename TYPE>
21 tlp::MutableContainer<TYPE>::MutableContainer(): vData(new std::deque<typename StoredType<TYPE>::Value>()),
22 hData(NULL), minIndex(UINT_MAX), maxIndex(UINT_MAX), defaultValue(StoredType<TYPE>::defaultValue()), state(VECT), elementInserted(0),
23 ratio(double(sizeof(typename tlp::StoredType<TYPE>::Value)) / (3.0*double(sizeof(void *))+double(sizeof(typename tlp::StoredType<TYPE>::Value)))),
27 template <
typename TYPE>
28 tlp::MutableContainer<TYPE>::~MutableContainer() {
32 if (StoredType<TYPE>::isPointer) {
34 typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it =
37 while (it!= vData->end()) {
38 if ((*it) != defaultValue)
39 StoredType<TYPE>::destroy(*it);
51 if (StoredType<TYPE>::isPointer) {
53 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it = hData->begin();
55 while (it!= hData->end()) {
56 StoredType<TYPE>::destroy((*it).second);
67 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
71 StoredType<TYPE>::destroy(defaultValue);
74 template <
typename TYPE>
75 void tlp::MutableContainer<TYPE>::setAll(
const TYPE &value) {
79 if (StoredType<TYPE>::isPointer) {
81 typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it =
84 while (it!= vData->end()) {
85 if ((*it) != defaultValue)
86 StoredType<TYPE>::destroy(*it);
97 if (StoredType<TYPE>::isPointer) {
99 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it = hData->begin();
101 while (it!= hData->end()) {
102 StoredType<TYPE>::destroy((*it).second);
109 vData =
new std::deque<typename StoredType<TYPE>::Value>();
114 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
118 StoredType<TYPE>::destroy(defaultValue);
119 defaultValue = StoredType<TYPE>::clone(value);
128 template <
typename TYPE>
129 tlp::IteratorValue* tlp::MutableContainer<TYPE>::findAllValues(
const TYPE &value,
132 StoredType<TYPE>::equal(defaultValue, value))
138 return new IteratorVect<TYPE>(value, equal, vData, minIndex);
142 return new IteratorHash<TYPE>(value, equal, hData);
147 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
154 template <
typename TYPE>
155 tlp::Iterator<unsigned int>* tlp::MutableContainer<TYPE>::findAll(
const TYPE &value,
157 return findAllValues(value, equal);
160 template <
typename TYPE>
161 void tlp::MutableContainer<TYPE>::vectset(
const unsigned int i,
162 typename StoredType<TYPE>::Value value) {
163 if (minIndex == UINT_MAX) {
166 (*vData).push_back(value);
182 while ( i > maxIndex ) {
183 (*vData).push_back(defaultValue);
187 while ( i < minIndex ) {
188 (*vData).push_front(defaultValue);
192 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
193 (*vData)[i - minIndex] = value;
195 if (val != defaultValue)
196 StoredType<TYPE>::destroy(val);
202 template <
typename TYPE>
203 void tlp::MutableContainer<TYPE>::set(
const unsigned int i,
const TYPE &value) {
206 !StoredType<TYPE>::equal(defaultValue, value)) {
208 compress (std::min(i,minIndex), std::max(i,maxIndex), elementInserted);
212 if (StoredType<TYPE>::equal(defaultValue, value)) {
217 if (i<=maxIndex && i>=minIndex) {
218 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
220 if (val != defaultValue) {
221 (*vData)[i - minIndex]= defaultValue;
222 StoredType<TYPE>::destroy(val);
230 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
232 if (it!=hData->end()) {
233 StoredType<TYPE>::destroy((*it).second);
243 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
248 typename StoredType<TYPE>::Value newVal =
249 StoredType<TYPE>::clone(value);
259 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
261 if (it!=hData->end())
262 StoredType<TYPE>::destroy((*it).second);
272 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
276 maxIndex = std::max(maxIndex, i);
277 minIndex = std::min(minIndex, i);
281 template <
typename TYPE>
282 void tlp::MutableContainer<TYPE>::add(
const unsigned int i, TYPE val) {
283 if (tlp::StoredType<TYPE>::isPointer ==
false) {
284 if (maxIndex == UINT_MAX) {
285 assert(state == VECT);
288 (*vData).push_back(defaultValue + val);
295 if (i > maxIndex || i < minIndex) {
296 set(i, defaultValue + val);
300 TYPE& oldVal = (*vData)[i - minIndex];
302 if (oldVal == defaultValue) {
303 set(i, defaultValue + val);
313 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
315 if (it!=hData->end()) {
317 if ((it->second + val) == defaultValue) {
318 StoredType<TYPE>::destroy((*it).second);
326 set(i, defaultValue + val);
334 std::cerr << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
339 std::cerr << __PRETTY_FUNCTION__ <<
"not implemented" << std::endl;
342 template <
typename TYPE>
343 typename tlp::StoredType<TYPE>::ReturnedConstValue tlp::MutableContainer<TYPE>::get(
const unsigned int i)
const {
345 if (maxIndex == UINT_MAX)
return StoredType<TYPE>::get(defaultValue);
349 if (i>maxIndex || i<minIndex)
350 return StoredType<TYPE>::get(defaultValue);
352 return StoredType<TYPE>::get((*vData)[i - minIndex]);
355 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
357 if (it!=hData->end())
358 return StoredType<TYPE>::get((*it).second);
360 return StoredType<TYPE>::get(defaultValue);
365 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
366 return StoredType<TYPE>::get(defaultValue);
371 template <
typename TYPE>
372 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::getDefault()
const {
373 return StoredType<TYPE>::get(defaultValue);
376 template <
typename TYPE>
377 bool tlp::MutableContainer<TYPE>::hasNonDefaultValue(
const unsigned int i)
const {
378 if (maxIndex == UINT_MAX)
return false;
382 return (i<=maxIndex && i>=minIndex &&
383 (((*vData)[i - minIndex]) != defaultValue));
386 return ((hData->find(i))!=hData->end());
390 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
395 template <
typename TYPE>
396 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::get(
const unsigned int i,
bool& notDefault)
const {
397 if (maxIndex == UINT_MAX) {
399 return StoredType<TYPE>::get(defaultValue);
404 if (i>maxIndex || i<minIndex) {
406 return StoredType<TYPE>::get(defaultValue);
409 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
410 notDefault = val != defaultValue;
411 return StoredType<TYPE>::get(val);
415 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
417 if (it!=hData->end()) {
419 return StoredType<TYPE>::get((*it).second);
423 return StoredType<TYPE>::get(defaultValue);
430 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
431 return StoredType<TYPE>::get(defaultValue);
435 template <
typename TYPE>
436 void tlp::MutableContainer<TYPE>::vecttohash() {
437 hData=
new TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>(elementInserted);
439 unsigned int newMaxIndex = 0;
440 unsigned int newMinIndex = UINT_MAX;
443 for (
unsigned int i=minIndex; i<=maxIndex; ++i) {
444 if ((*vData)[i - minIndex] != defaultValue) {
445 (*hData)[i] = (*vData)[i - minIndex];
446 newMaxIndex = std::max(newMaxIndex, i);
447 newMinIndex = std::min(newMinIndex, i);
452 maxIndex = newMaxIndex;
453 minIndex = newMinIndex;
459 template <
typename TYPE>
460 void tlp::MutableContainer<TYPE>::hashtovect() {
461 vData =
new std::deque<typename StoredType<TYPE>::Value>();
466 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
468 for (it=hData->begin(); it!=hData->end(); ++it) {
469 if (it->second != defaultValue)
470 vectset(it->first, it->second);
477 template <
typename TYPE>
478 void tlp::MutableContainer<TYPE>::compress(
unsigned int min,
unsigned int max,
unsigned int nbElements) {
479 if (max == UINT_MAX || (max - min) < 10)
return;
481 double limitValue = ratio*(double(max - min + 1.0));
486 if (
double(nbElements) < limitValue) {
494 if (
double(nbElements) > limitValue*1.5) {
502 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
507 template <
typename TYPE>
508 typename tlp::StoredType<TYPE>::ReturnedConstValue tlp::MutableContainer<TYPE>::operator[](
const unsigned int i)
const {