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. [SOLVED] QQueue - Easy way to access tail element?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QQueue - Easy way to access tail element?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.9k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    I was wondering if there is a function similar to "head()" (that returns the queue head item), but I need the same thing for the "tail".

    What I want to do : Average all value inside a Queue, each element of the Queue also representing an average. I need to access the tail element to update it's value with new value that comes in (real-time averaging)

    I thought of using array for this, but I would need to move the element all the time, and found QQueue would do this for me (not sure about performance vs array though)

    Any advice appreciated!

    Here is the code I have:

    @ // Get current second
    int currentSecondPower = (int) timeElapsed_sec;
    // If the second changed, add new value to top of queue
    if (currentSecondPower != lastSecondPower) {
    nbPointsPower = 0;
    queuePower.enqueue(value);
    // check the queue size, remove element if needed
    if (queuePower.size() > settings->averagingPower) {
    queuePower.dequeue();
    }
    }
    // If the second is the same, recalculate average and replace value of the tail
    else {
    double firstEle = queuePower.last() *((double)nbPointsPower/(nbPointsPower+1)); ///how to access tail of queue?
    double secondEle = ((double)value)/(nbPointsPower+1);
    // replace last element of the queue
    queuePower.replace(queuePower.size()-1, firstEle + secondEle);
    nbPointsPower++;
    }

        lastSecondPower = currentSecondPower;@
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      A QQueue is just a QList with some bells and whistles.
      So you can always use QList::last() (provided your queue is not empty)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        Oh I see, so basically I could use .first() or .last() and it would be like .head() and tail() ?

        I guess I could just use a QList and use removeLast() and insert() instead of EnQueue and DeQueue, not sure what is better with a QQueue now..

        To replace the last Element of the Queue, i'm using this code, is there a better syntax? :
        @ queuePower.replace(queuePower.size()-1, value);@
        Thanks


        Free Indoor Cycling Software - https://maximumtrainer.com

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

          Hi,

          Yes it will.

          You could but your code would be less readable. QQueue's name already provides a hint on what is going to happen with that container.

          Better syntax ? What do you have in mind ?

          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
          0
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

            Thanks SGaist I will keep QQueue as you said it implies I want to use it as a Queue.
            I thought maybe a cleared way to replace the last element queue.ReplaceLast() but I guess the previous code will do, thanks for your help !


            Free Indoor Cycling Software - https://maximumtrainer.com

            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