Results 1 to 8 of 8

Thread: QVector copying data with remove()

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QVector copying data with remove()

    Hello!

    I'm developing a graph in Qwt in which the new data appears in the right side while the old data is cut out in the left. Using a QVector<double> to storage the data, that would mean to use a QVector::append() together with a QVector::remove(0,1), right?

    The problem is that I supposed that in doing remove(0,1), QVector copies all data after the first (now removed) item to a previous position (so now the data that was in position 1 is copied to position 0, position 2 is copied to position 1 and so on), what means a huge processing.

    Is that true? And if it so, how may I get around this problem?

    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QVector copying data with remove()

    If you aren't changing the size of the QVector (in other words, just removing one entry at the beginning and adding a new one at the end) you can do this:

    Use the QVector::data() method to return a double * pointer to the start of the internal array. Use memcpy() to copy N - 1 items starting at the N = 1 position to the N = 0 position, then assign the Nth entry to your new value (using QVector::operator[]() if you want).

    Since you aren't changing either the size of the array or where it lives in memory but are just changing the value of the entries, this should be safe (and fast).

  3. The following user says thank you to d_stranz for this useful post:

    Momergil (10th June 2014)

  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector copying data with remove()

    Quote Originally Posted by d_stranz View Post
    Use memcpy() to copy N - 1 items starting at the N = 1 position to the N = 0 position, then assign the Nth entry to your new value (using QVector::operator[]() if you want).

    this should be safe (and fast).
    Thanks d_stranz for the reply.

    Well, how exactly the use of memcpy() makes it faster?
    May the Lord be with you. Always.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QVector copying data with remove()

    Well, how exactly the use of memcpy() makes it faster?
    It may not make it faster. It depends on how QVector is implemented. QVector might use memcpy() internally to copy the contents of the vector when you remove() something from it. However, by manipulating the double * pointer directly, you can be sure that nothing else will happen to cause a reallocation of the memory. That would be the slow part.

    There is no way to avoid copying the contents of the QVector when you remove something from the beginning or middle. The only way to avoid copying is to use some other data collection, like QList<>.

    I haven't used Qwt for a long time, so I do not know if Qwt 6 supports the use of anything except QVector<> or other contiguous data collections. It might be possible to write an adapter class that uses QList<> internally, but looks like QVector<> to Qwt. I don't know. If there is a Qwt data interface that uses iterators, you could use any collection class that has forward iterator semantics.

  6. The following user says thank you to d_stranz for this useful post:

    Momergil (11th June 2014)

  7. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector copying data with remove()

    Quote Originally Posted by d_stranz View Post
    It might be possible to write an adapter class that uses QList<> internally, but looks like QVector<> to Qwt.
    Now THAT would be awesome! Have any ideas on how could that be done?
    May the Lord be with you. Always.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector copying data with remove()

    Quote Originally Posted by d_stranz View Post
    The only way to avoid copying is to use some other data collection, like QList<>.
    Not really. QList is implemented in a similar way as QVector with the only important difference that QVector always grows "at the far end" while QList can grow in both directions (meaning that if you need to insert an element in the first half of the data structure, the list will grow at the near end, if you insert in the second half, it will grow at the far end). You probably meant QLinkedList but that's very rarely used as random access to a linked list is not possible so it is very slow for such cases.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    Momergil (13th June 2014)

  10. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector copying data with remove()

    If I understand your problem correctly, the data structure you need is a queue. Unless QVector is forced upon you by some interface, you should pick QQueue instead.

  11. #8
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector copying data with remove()

    Quote Originally Posted by yeye_olive View Post
    Unless QVector is forced upon you by some interface
    Yep! I'm using Qwt which uses a QVector<QPointF> internally to storage the plotting data :T So I can't avoid using QVector for the plotting, although it maybe faster to copy a entire new data to a QVector instead of using append + remove(0,1); but I wouldn't know that.
    May the Lord be with you. Always.

Similar Threads

  1. Copying data within model/widgetmapper
    By scott_hollen in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2011, 18:27
  2. Memory aligned QVector().data()
    By sto in forum Qt Programming
    Replies: 10
    Last Post: 19th November 2010, 09:30
  3. memory leak when copying Qimage data
    By klobauster_toomc in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 08:33
  4. [[SOLVED]]Can't get data from QVector
    By asieriko in forum Newbie
    Replies: 1
    Last Post: 13th December 2009, 19:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.