20 template <
typename TYPE>
21 tlp::MutableContainer<TYPE>::MutableContainer()
22 : vData(new std::deque<typename StoredType<TYPE>::Value>()), hData(nullptr), minIndex(UINT_MAX),
23 maxIndex(UINT_MAX), defaultValue(StoredType<TYPE>::defaultValue()), state(VECT),
25 ratio(double(sizeof(typename
tlp::StoredType<TYPE>::Value)) /
26 (3.0 * double(sizeof(void *)) + double(sizeof(typename
tlp::StoredType<TYPE>::Value)))),
29 template <
typename TYPE>
30 tlp::MutableContainer<TYPE>::~MutableContainer() {
34 if (StoredType<TYPE>::isPointer) {
36 typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it = vData->begin();
38 while (it != vData->end()) {
39 if ((*it) != defaultValue)
40 StoredType<TYPE>::destroy(*it);
52 if (StoredType<TYPE>::isPointer) {
54 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it =
57 while (it != hData->end()) {
58 StoredType<TYPE>::destroy(it->second);
69 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
73 StoredType<TYPE>::destroy(defaultValue);
76 template <
typename TYPE>
77 void tlp::MutableContainer<TYPE>::setDefault(
typename StoredType<TYPE>::ReturnedConstValue value) {
78 StoredType<TYPE>::destroy(defaultValue);
79 defaultValue = StoredType<TYPE>::clone(value);
82 template <
typename TYPE>
83 void tlp::MutableContainer<TYPE>::setAll(
typename StoredType<TYPE>::ReturnedConstValue value) {
87 if (StoredType<TYPE>::isPointer) {
89 typename std::deque<typename StoredType<TYPE>::Value>::const_iterator it = vData->begin();
91 while (it != vData->end()) {
92 if ((*it) != defaultValue)
93 StoredType<TYPE>::destroy(*it);
104 if (StoredType<TYPE>::isPointer) {
106 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it =
109 while (it != hData->end()) {
110 StoredType<TYPE>::destroy(it->second);
117 vData =
new std::deque<typename StoredType<TYPE>::Value>();
122 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
126 StoredType<TYPE>::destroy(defaultValue);
127 defaultValue = StoredType<TYPE>::clone(value);
136 template <
typename TYPE>
138 tlp::MutableContainer<TYPE>::findAllValues(
typename StoredType<TYPE>::ReturnedConstValue value,
140 if (equal && StoredType<TYPE>::equal(defaultValue, value))
146 return new IteratorVect<TYPE>(value, equal, vData, minIndex);
150 return new IteratorHash<TYPE>(value, equal, hData);
155 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
162 template <
typename TYPE>
164 tlp::MutableContainer<TYPE>::findAll(
typename StoredType<TYPE>::ReturnedConstValue value,
166 return findAllValues(value, equal);
169 template <
typename TYPE>
170 void tlp::MutableContainer<TYPE>::vectset(
const unsigned int i,
171 typename StoredType<TYPE>::Value value) {
172 if (minIndex == UINT_MAX) {
175 (*vData).push_back(value);
190 while (i > maxIndex) {
191 (*vData).push_back(defaultValue);
195 while (i < minIndex) {
196 (*vData).push_front(defaultValue);
200 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
201 (*vData)[i - minIndex] = value;
203 if (val != defaultValue)
204 StoredType<TYPE>::destroy(val);
210 template <
typename TYPE>
211 void tlp::MutableContainer<TYPE>::set(
const unsigned int i,
212 typename StoredType<TYPE>::ReturnedConstValue value,
213 bool forceDefaultValueRemoval) {
215 if (!compressing && !StoredType<TYPE>::equal(defaultValue, value)) {
217 compress(std::min(i, minIndex), std::max(i, maxIndex), elementInserted);
221 if (StoredType<TYPE>::equal(defaultValue, value)) {
226 if (i <= maxIndex && i >= minIndex) {
227 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
229 if (val != defaultValue) {
230 (*vData)[i - minIndex] = defaultValue;
231 StoredType<TYPE>::destroy(val);
233 }
else if (forceDefaultValueRemoval)
240 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::iterator it =
243 if (it != hData->end()) {
244 StoredType<TYPE>::destroy(it->second);
254 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
258 typename StoredType<TYPE>::Value newVal = StoredType<TYPE>::clone(value);
268 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::iterator it =
271 if (it != hData->end()) {
272 StoredType<TYPE>::destroy(it->second);
276 (*hData)[i] = newVal;
283 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
287 maxIndex = std::max(maxIndex, i);
288 minIndex = std::min(minIndex, i);
292 template <
typename TYPE>
293 void tlp::MutableContainer<TYPE>::add(
const unsigned int i, TYPE val) {
294 if (tlp::StoredType<TYPE>::isPointer ==
false) {
295 if (maxIndex == UINT_MAX) {
296 assert(state == VECT);
299 (*vData).push_back(defaultValue + val);
306 if (i > maxIndex || i < minIndex) {
307 set(i, defaultValue + val);
311 TYPE &oldVal = (*vData)[i - minIndex];
313 if (oldVal == defaultValue) {
314 set(i, defaultValue + val);
324 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::iterator it =
327 if (it != hData->end()) {
329 if ((it->second + val) == defaultValue) {
330 StoredType<TYPE>::destroy(it->second);
336 set(i, defaultValue + val);
344 std::cerr << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
349 std::cerr << __PRETTY_FUNCTION__ <<
"not implemented" << std::endl;
352 template <
typename TYPE>
353 typename tlp::StoredType<TYPE>::ReturnedConstValue
354 tlp::MutableContainer<TYPE>::get(
const unsigned int i)
const {
356 if (!elementInserted)
357 return StoredType<TYPE>::get(defaultValue);
362 if (i > maxIndex || i < minIndex)
363 return StoredType<TYPE>::get(defaultValue);
365 return StoredType<TYPE>::get((*vData)[i - minIndex]);
368 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::iterator it =
371 if (it != hData->end())
372 return StoredType<TYPE>::get(it->second);
374 return StoredType<TYPE>::get(defaultValue);
379 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
380 return StoredType<TYPE>::get(defaultValue);
385 template <
typename TYPE>
386 void tlp::MutableContainer<TYPE>::invertBooleanValue(
const unsigned int i) {
387 if (std::is_same<
typename StoredType<TYPE>::Value,
bool>::value) {
390 if (i > maxIndex || i < minIndex)
391 vectset(i, !defaultValue);
393 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
395 if (val != defaultValue)
399 (*vData)[i - minIndex] = !val;
405 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::iterator it =
408 if (it != hData->end()) {
412 (*hData)[i] = !defaultValue;
420 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
426 std::cerr << __PRETTY_FUNCTION__ <<
"not implemented" << std::endl;
429 template <
typename TYPE>
430 typename tlp::StoredType<TYPE>::ReturnedValue tlp::MutableContainer<TYPE>::getDefault()
const {
431 return StoredType<TYPE>::get(defaultValue);
434 template <
typename TYPE>
435 bool tlp::MutableContainer<TYPE>::hasNonDefaultValue(
const unsigned int i)
const {
436 if (!elementInserted)
441 return (i <= maxIndex && i >= minIndex && (((*vData)[i - minIndex]) != defaultValue));
444 return ((hData->find(i)) != hData->end());
448 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
453 template <
typename TYPE>
454 typename tlp::StoredType<TYPE>::ReturnedValue
455 tlp::MutableContainer<TYPE>::get(
const unsigned int i,
bool ¬Default)
const {
456 if (!elementInserted) {
458 return StoredType<TYPE>::get(defaultValue);
464 if (i > maxIndex || i < minIndex) {
466 return StoredType<TYPE>::get(defaultValue);
468 typename StoredType<TYPE>::Value val = (*vData)[i - minIndex];
469 notDefault = val != defaultValue;
470 return StoredType<TYPE>::get(val);
474 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::iterator it =
477 if (it != hData->end()) {
479 return StoredType<TYPE>::get(it->second);
482 return StoredType<TYPE>::get(defaultValue);
489 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
490 return StoredType<TYPE>::get(defaultValue);
494 template <
typename TYPE>
495 unsigned int tlp::MutableContainer<TYPE>::numberOfNonDefaultValues()
const {
496 return elementInserted;
499 template <
typename TYPE>
500 void tlp::MutableContainer<TYPE>::vecttohash() {
501 hData =
new tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>(elementInserted);
503 unsigned int newMaxIndex = 0;
504 unsigned int newMinIndex = UINT_MAX;
507 for (
unsigned int i = minIndex; i <= maxIndex; ++i) {
508 if ((*vData)[i - minIndex] != defaultValue) {
509 (*hData)[i] = (*vData)[i - minIndex];
510 newMaxIndex = std::max(newMaxIndex, i);
511 newMinIndex = std::min(newMinIndex, i);
516 maxIndex = newMaxIndex;
517 minIndex = newMinIndex;
523 template <
typename TYPE>
524 void tlp::MutableContainer<TYPE>::hashtovect() {
525 vData =
new std::deque<typename StoredType<TYPE>::Value>();
530 typename tlp_hash_map<unsigned int, typename StoredType<TYPE>::Value>::const_iterator it;
532 for (it = hData->begin(); it != hData->end(); ++it) {
533 if (it->second != defaultValue)
534 vectset(it->first, it->second);
541 template <
typename TYPE>
542 void tlp::MutableContainer<TYPE>::compress(
unsigned int min,
unsigned int max,
543 unsigned int nbElements) {
544 if (max == UINT_MAX || (max - min) < 10)
547 double limitValue = ratio * (double(max - min + 1.0));
552 if (
double(nbElements) < limitValue) {
560 if (
double(nbElements) > limitValue * 1.5) {
568 tlp::error() << __PRETTY_FUNCTION__ <<
"unexpected state value (serious bug)" << std::endl;
573 template <
typename TYPE>
574 typename tlp::StoredType<TYPE>::ReturnedConstValue
575 tlp::MutableContainer<TYPE>::operator[](
const unsigned int i)
const {