QContiguousCache indexes are always valid.
-
QContiguousCache<QString> cache; cache.setCapacity(3); cache.insert(INT_MAX, "1"); // cache contains one value and has valid indexes, INT_MAX to INT_MAX cache.append("2"); // cache contains two values but does not have valid indexes. cache.append("2"); // cache contains three values but does not have valid indexes. qDebug()<< cache.areIndexesValid(); // prints true
-
-
The code fragment and comment is (adapted) from the docs but the actual behaviour is different from the statements in the comments.
QContiguousCache<QString> cache; cache.setCapacity(3); qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache; cache.insert(INT_MAX, "A"); // cache contains one value and has valid indexes, INT_MAX to INT_MAX qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache; cache.append("B"); // cache contains two values but does not have valid indexes. qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache; cache.append("C"); // cache contains three values but does not have valid indexes. qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache; cache.append("D"); // a fourth value in a three-value cache causes assertion qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache; cache.prepend("Z"); qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache; cache.normalizeIndexes(); qDebug() << cache.areIndexesValid() << cache.firstIndex() << cache.lastIndex() << cache;
true 0 -1 QContiguousCache() true 2147483647 2147483647 QContiguousCache("A") true 2147483647 2147483648 QContiguousCache("A", "B") true 2147483647 2147483649 QContiguousCache("A", "B", "C") true 2147483648 2147483650 QContiguousCache("B", "C", "D") true 2147483647 2147483649 QContiguousCache("Z", "B", "C") true 1 3 QContiguousCache("B", "C", "D")
The behaviour in regard to inserting into a full cache is as documented i.e., it dropped the "furthest" entry.
I suspect (without checking the code) that inserting beyond either end of a QContiguousCache with a capacity of INT_MAX would fail (assert).
Edit: To Paul Colby's point below. This is platform dependent. For me: Ubuntu Linux 24.04, GCC 11, Qt 6, 64-bit executable.
-
Hi @zain_a,
The example you've copied from the Qt docs is demonstrating how/why to use QContiguousCache::normalizeIndexes(), if needed, but it's not saying definitively that it is always needed. It's platform dependant.
QContiguousCache::areIndexesValid() says:
Indexes can become invalid if items are appended after the index position INT_MAX or prepended before the index position 0.
Note, that it can become invalid, not that it necessarily will. In this case, it depends on the difference (if any) between the old C
INT_MAX
and Qt'sqsizetype
(which is usually derived fromstd::size_t
, which may or may not matchINT_MAX
').Try this, for example:
qDebug() << INT_MAX; qDebug() << std::numeric_limits<qsizetype>::max();
For me, on a 64-bit Linux system with gcc, I get:
2147483647 9223372036854775807
But on platforms where those two are the same, then your example will probably print
false
as you expected. All that to say, just use theQContiguousCache::areIndexesValid()
method and act accordingly. Leave the underlying platform handling to Qt :)Cheers.