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. Collection and Fixed Number of Items

Collection and Fixed Number of Items

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.6k Views 3 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by webzoid
    #1

    I'm interested to see how others have tackled this scenario.

    What would be a cute or efficient or fast way of limiting the number of elements in a QList or other collection?

    Considering the following crude example:

    QList<double> list;
    
    void addItem(double VALUE) {
        if (list.count() == MAX_ITEMS)
            list.pop_back();
    
        list.insert(0, VALUE);
    }
    

    This could achieve what I want, but seems very inefficient.

    You may ask why I'm inserting elements at the start of the list rather than appending to the end, well, in my application the entries in a list are all timestamped and I prefer to have the most recent entry at the start. I don't know whether there is a performance impact based on doing things this way.

    Is there a more credible alternative in existence within the Qt world?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean something like QQueue ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        This could achieve what I want, but seems very inefficient.

        Hi! It is ineffcient. What you're asking for is called a "ring buffer" or "circular buffer". Boost has one, see Boost.Circular Buffer.

        1 Reply Last reply
        3
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          If you know the maximum size and you can afford to pre-allocate the entire space (to avoid dynamic allocation on insertions) a much better container than a linked list is a ring buffer.
          It is basically a fixed size array with two pointers (start and end). Insertions/deletions are basically pointer increment/decrement, so it's cheap. Indexed access is just (start + index) so cheap also.
          There's no ring buffer in Qt but there are many implementations out there (e.g. circular_buffer in boost). If you don't want to pull a dependency you can easily implement it on top of any continuous container like QVector or std::vector, but for better results I suggest something with a constant size like std::array.

          1 Reply Last reply
          2
          • webzoidW Offline
            webzoidW Offline
            webzoid
            wrote on last edited by
            #5

            @SGaist isn't QQueue just built on top of a QList with just enqueue and dequeue functions? Fundamentally, nothing is different behind the scenes?

            @Wieland correct, a ring buffer would suit but wasn't sure whether Qt had such classes available.

            @Chris-Kawa I can certainly afford to pre-allocate a buffer. What I'm storing in these lists won't break the bank in terms of memory but I do need the end result to be fast - these buffers may get updated rather quickly (for instance, during a log file replay). I'll look at wrapping something up into a circular buffer.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I was wondering whether you wanted to use a FIFO or as my fellows rightly pointed to a ring buffer.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • webzoidW Offline
                webzoidW Offline
                webzoid
                wrote on last edited by
                #7

                @SGaist you were indeed correct.

                I've found this implementation of QCircularBuffer but this seems to have been discontinued since 5.5 (for some reason)

                http://doc.qt.io/archives/qt-5.5/qt3d-qcircularbuffer.html

                I guess I could just re-implement this in my application

                1 Reply Last reply
                0

                • Login

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