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);
350 if (i>maxIndex || i<minIndex)
351 return StoredType<TYPE>::get(defaultValue);
353 return StoredType<TYPE>::get((*vData)[i - minIndex]);
356 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
358 if (it!=hData->end())
359 return StoredType<TYPE>::get((*it).second);
361 return StoredType<TYPE>::get(defaultValue);
366 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
367 return StoredType<TYPE>::get(defaultValue);
372 template <
typename TYPE>
373 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::getDefault()
const {
374 return StoredType<TYPE>::get(defaultValue);
377 template <
typename TYPE>
378 bool tlp::MutableContainer<TYPE>::hasNonDefaultValue(
const unsigned int i)
const {
379 if (maxIndex == UINT_MAX)
return false;
383 return (i<=maxIndex && i>=minIndex &&
384 (((*vData)[i - minIndex]) != defaultValue));
387 return ((hData->find(i))!=hData->end());
391 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
396 template <
typename TYPE>
397 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::get(
const unsigned int i,
bool& notDefault)
const {
398 if (maxIndex == UINT_MAX) {
400 return StoredType<TYPE>::get(defaultValue);
406 if (i>maxIndex || i<minIndex) {
408 return StoredType<TYPE>::get(defaultValue);
411 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
412 notDefault = val != defaultValue;
413 return StoredType<TYPE>::get(val);
417 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::iterator it = hData->find(i);
419 if (it!=hData->end()) {
421 return StoredType<TYPE>::get((*it).second);
425 return StoredType<TYPE>::get(defaultValue);
432 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
433 return StoredType<TYPE>::get(defaultValue);
437 template <
typename TYPE>
438 unsigned int tlp::MutableContainer<TYPE>::numberOfNonDefaultValues()
const {
439 return elementInserted;
442 template <
typename TYPE>
443 void tlp::MutableContainer<TYPE>::vecttohash() {
444 hData=
new TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>(elementInserted);
446 unsigned int newMaxIndex = 0;
447 unsigned int newMinIndex = UINT_MAX;
450 for (
unsigned int i=minIndex; i<=maxIndex; ++i) {
451 if ((*vData)[i - minIndex] != defaultValue) {
452 (*hData)[i] = (*vData)[i - minIndex];
453 newMaxIndex = std::max(newMaxIndex, i);
454 newMinIndex = std::min(newMinIndex, i);
459 maxIndex = newMaxIndex;
460 minIndex = newMinIndex;
466 template <
typename TYPE>
467 void tlp::MutableContainer<TYPE>::hashtovect() {
468 vData =
new std::deque<typename StoredType<TYPE>::Value>();
473 typename TLP_HASH_MAP<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
475 for (it=hData->begin(); it!=hData->end(); ++it) {
476 if (it->second != defaultValue)
477 vectset(it->first, it->second);
484 template <
typename TYPE>
485 void tlp::MutableContainer<TYPE>::compress(
unsigned int min,
unsigned int max,
unsigned int nbElements) {
486 if (max == UINT_MAX || (max - min) < 10)
return;
488 double limitValue = ratio*(double(max - min + 1.0));
493 if (
double(nbElements) < limitValue) {
501 if (
double(nbElements) > limitValue*1.5) {
509 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
514 template <
typename TYPE>
515 typename tlp::StoredType<TYPE>::ReturnedConstValue tlp::MutableContainer<TYPE>::operator[](
const unsigned int i)
const {