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>::setDefault(
typename StoredType<TYPE>::ReturnedConstValue value) {
76 StoredType<TYPE>::destroy(defaultValue);
77 defaultValue = StoredType<TYPE>::clone(value);
80 template <
typename TYPE>
81 void tlp::MutableContainer<TYPE>::setAll(
typename StoredType<TYPE>::ReturnedConstValue value) {
85 if (StoredType<TYPE>::isPointer) {
87 typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it =
90 while (it!= vData->end()) {
91 if ((*it) != defaultValue)
92 StoredType<TYPE>::destroy(*it);
103 if (StoredType<TYPE>::isPointer) {
105 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it = hData->begin();
107 while (it!= hData->end()) {
108 StoredType<TYPE>::destroy((*it).second);
115 vData =
new std::deque<typename StoredType<TYPE>::Value>();
120 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
124 StoredType<TYPE>::destroy(defaultValue);
125 defaultValue = StoredType<TYPE>::clone(value);
134 template <
typename TYPE>
135 tlp::IteratorValue* tlp::MutableContainer<TYPE>::findAllValues(
typename StoredType<TYPE>::ReturnedConstValue value,
138 StoredType<TYPE>::equal(defaultValue, value))
144 return new IteratorVect<TYPE>(value, equal, vData, minIndex);
148 return new IteratorHash<TYPE>(value, equal, hData);
153 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
160 template <
typename TYPE>
161 tlp::Iterator<unsigned int>* tlp::MutableContainer<TYPE>::findAll(
typename StoredType<TYPE>::ReturnedConstValue value,
163 return findAllValues(value, equal);
166 template <
typename TYPE>
167 void tlp::MutableContainer<TYPE>::vectset(
const unsigned int i,
168 typename StoredType<TYPE>::Value value) {
169 if (minIndex == UINT_MAX) {
172 (*vData).push_back(value);
188 while ( i > maxIndex ) {
189 (*vData).push_back(defaultValue);
193 while ( i < minIndex ) {
194 (*vData).push_front(defaultValue);
198 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
199 (*vData)[i - minIndex] = value;
201 if (val != defaultValue)
202 StoredType<TYPE>::destroy(val);
208 template <
typename TYPE>
209 void tlp::MutableContainer<TYPE>::set(
const unsigned int i,
typename StoredType<TYPE>::ReturnedConstValue value) {
212 !StoredType<TYPE>::equal(defaultValue, value)) {
214 compress (std::min(i,minIndex), std::max(i,maxIndex), elementInserted);
218 if (StoredType<TYPE>::equal(defaultValue, value)) {
223 if (i<=maxIndex && i>=minIndex) {
224 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
226 if (val != defaultValue) {
227 (*vData)[i - minIndex]= defaultValue;
228 StoredType<TYPE>::destroy(val);
236 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
238 if (it!=hData->end()) {
239 StoredType<TYPE>::destroy((*it).second);
249 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
254 typename StoredType<TYPE>::Value newVal =
255 StoredType<TYPE>::clone(value);
265 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
267 if (it!=hData->end())
268 StoredType<TYPE>::destroy((*it).second);
278 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
282 maxIndex = std::max(maxIndex, i);
283 minIndex = std::min(minIndex, i);
287 template <
typename TYPE>
288 void tlp::MutableContainer<TYPE>::add(
const unsigned int i, TYPE val) {
289 if (tlp::StoredType<TYPE>::isPointer ==
false) {
290 if (maxIndex == UINT_MAX) {
291 assert(state == VECT);
294 (*vData).push_back(defaultValue + val);
301 if (i > maxIndex || i < minIndex) {
302 set(i, defaultValue + val);
306 TYPE& oldVal = (*vData)[i - minIndex];
308 if (oldVal == defaultValue) {
309 set(i, defaultValue + val);
319 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
321 if (it!=hData->end()) {
323 if ((it->second + val) == defaultValue) {
324 StoredType<TYPE>::destroy((*it).second);
332 set(i, defaultValue + val);
340 std::cerr << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
345 std::cerr << __PRETTY_FUNCTION__ <<
"not implemented" << std::endl;
348 template <
typename TYPE>
349 typename tlp::StoredType<TYPE>::ReturnedConstValue tlp::MutableContainer<TYPE>::get(
const unsigned int i)
const {
351 if (maxIndex == UINT_MAX)
return StoredType<TYPE>::get(defaultValue);
356 if (i>maxIndex || i<minIndex)
357 return StoredType<TYPE>::get(defaultValue);
359 return StoredType<TYPE>::get((*vData)[i - minIndex]);
362 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
364 if (it!=hData->end())
365 return StoredType<TYPE>::get((*it).second);
367 return StoredType<TYPE>::get(defaultValue);
372 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
373 return StoredType<TYPE>::get(defaultValue);
378 template <
typename TYPE>
379 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::getDefault()
const {
380 return StoredType<TYPE>::get(defaultValue);
383 template <
typename TYPE>
384 bool tlp::MutableContainer<TYPE>::hasNonDefaultValue(
const unsigned int i)
const {
385 if (maxIndex == UINT_MAX)
return false;
389 return (i<=maxIndex && i>=minIndex &&
390 (((*vData)[i - minIndex]) != defaultValue));
393 return ((hData->find(i))!=hData->end());
397 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
402 template <
typename TYPE>
403 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::get(
const unsigned int i,
bool& notDefault)
const {
404 if (maxIndex == UINT_MAX) {
406 return StoredType<TYPE>::get(defaultValue);
412 if (i>maxIndex || i<minIndex) {
414 return StoredType<TYPE>::get(defaultValue);
417 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
418 notDefault = val != defaultValue;
419 return StoredType<TYPE>::get(val);
423 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
425 if (it!=hData->end()) {
427 return StoredType<TYPE>::get((*it).second);
431 return StoredType<TYPE>::get(defaultValue);
438 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
439 return StoredType<TYPE>::get(defaultValue);
443 template <
typename TYPE>
444 unsigned int tlp::MutableContainer<TYPE>::numberOfNonDefaultValues()
const {
445 return elementInserted;
448 template <
typename TYPE>
449 void tlp::MutableContainer<TYPE>::vecttohash() {
450 hData=
new TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>(elementInserted);
452 unsigned int newMaxIndex = 0;
453 unsigned int newMinIndex = UINT_MAX;
456 for (
unsigned int i=minIndex; i<=maxIndex; ++i) {
457 if ((*vData)[i - minIndex] != defaultValue) {
458 (*hData)[i] = (*vData)[i - minIndex];
459 newMaxIndex = std::max(newMaxIndex, i);
460 newMinIndex = std::min(newMinIndex, i);
465 maxIndex = newMaxIndex;
466 minIndex = newMinIndex;
472 template <
typename TYPE>
473 void tlp::MutableContainer<TYPE>::hashtovect() {
474 vData =
new std::deque<typename StoredType<TYPE>::Value>();
479 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
481 for (it=hData->begin(); it!=hData->end(); ++it) {
482 if (it->second != defaultValue)
483 vectset(it->first, it->second);
490 template <
typename TYPE>
491 void tlp::MutableContainer<TYPE>::compress(
unsigned int min,
unsigned int max,
unsigned int nbElements) {
492 if (max == UINT_MAX || (max - min) < 10)
return;
494 double limitValue = ratio*(double(max - min + 1.0));
499 if (
double(nbElements) < limitValue) {
507 if (
double(nbElements) > limitValue*1.5) {
515 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
520 template <
typename TYPE>
521 typename tlp::StoredType<TYPE>::ReturnedConstValue tlp::MutableContainer<TYPE>::operator[](
const unsigned int i)
const {