Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QContiguousCache indexes are always valid.
Forum Updated to NodeBB v4.3 + New Features

QContiguousCache indexes are always valid.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 254 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zain_a
    wrote on last edited by
    #1
     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
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • Z zain_a
       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
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @zain_a What's exactly is your question? Which should there no valid indexes? I don't see anyting related in the docs: "Indexes can become invalid if items are appended after the index position INT_MAX or prepended before the index position 0."

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      C 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @zain_a What's exactly is your question? Which should there no valid indexes? I don't see anyting related in the docs: "Indexes can become invalid if items are appended after the index position INT_MAX or prepended before the index position 0."

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #3

        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.

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          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's qsizetype (which is usually derived from std::size_t, which may or may not match INT_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 the QContiguousCache::areIndexesValid() method and act accordingly. Leave the underlying platform handling to Qt :)

          Cheers.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved