Results 1 to 8 of 8

Thread: Reversing QString and QByteArray

  1. #1
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Reversing QString and QByteArray

    hi
    What is the best way to get reverse of QString and QByteArray (fast and safe) with out using loops (it becomes very slow with huge data)
    thanks
    Br
    Sis

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Reversing QString and QByteArray

    If you don't want to maintain loops, then may be you can maintain a copy of reverse. I mean a parallel copy. Each time you do an append on the string, do the similar reversed operation of the other copy. Tricky
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    alrawab (12th January 2013)

  4. #3
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reversing QString and QByteArray

    yes it a good idea but i'm dealing with dna sequences .
    i'm using :
    for( QByteArray::const_iterator i = myqbyte.constEnd(); i !=myqbyte.constBegin(); )
    but this procedure become so slow with large sequences

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Reversing QString and QByteArray

    DNA

    I were to do so, I would write a customized container for DNA strand/sequence, (This will need knowledge of DNA structure and ways to digitize it)

    There are other methods, but are applciation specific, like reverse only the section of the sequence? (instead of complete sequence).

    Think again do you really want to reverse the sequence? can you not re-write your operation to operate on the sequence in reverse (I bet that should be acceptable)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reversing QString and QByteArray

    this is my function which do the reverse task (some time i need to get the reverse of the full length )
    //--------------------------------------
    // Returns the reverse strand of a DNA or RNA sequence.
    QByteArray QDnaSequence::Reverse(QByteArray& seq)
    {
    QByteArray reverse;
    for( QByteArray::const_iterator i = seq.constEnd(); i !=seq.constBegin(); )
    {
    --i;
    reverse += *i;
    }
    return reverse;
    }

  7. #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: Reversing QString and QByteArray

    There is std::reverse(), you know...

    Qt Code:
    1. #include <algorithm>
    2.  
    3. QByteArray reverse = seq;
    4. std::reverse(reverse.constBegin(), reverse.constEnd());
    To copy to clipboard, switch view to plain text mode 

    You can also use reverse_copy:

    Qt Code:
    1. QByteArray reverse;
    2. std::reverse_copy(seq.constBegin(), seq.constEnd(), reverse.begin());
    To copy to clipboard, switch view to plain text mode 
    Complexity is O(n).

    There is also the trivial approach:

    Qt Code:
    1. QByteArray reverse(const QByteArray &ba) {
    2. QByteArray reverse;
    3. reverse.reserve(ba.size());
    4. for(int i=ba.size(); i>=0; --i) reverse.append(ba.at(i));
    5. return reverse;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I can also imagine parallelizing the algorithm by dividing the array into chunks and reversing each chunk in a separate thread.


    Added after 21 minutes:


    Here is a complete implementation:

    Qt Code:
    1. #include <QByteArray>
    2.  
    3. #include <QtConcurrentMap>
    4.  
    5. struct Chunk {
    6. char *ba;
    7. int size;
    8. int from;
    9. int cnt;
    10. };
    11.  
    12. void swapChunk(const Chunk &ch) {
    13. const int size = ch.size;
    14. char *baRef = ch.ba;
    15. for(int i=0;i<ch.cnt;++i) {
    16. int i1 = ch.from+i;
    17. int i2 = size-1-ch.from-i;
    18. qSwap(baRef[i1], baRef[i2]);
    19. }
    20. }
    21.  
    22. QByteArray reverseInParallel(QByteArray ba) {
    23. char *dat = ba.data();
    24. int threads = QThread::idealThreadCount();
    25. int bytesPerThread = ba.size()/2/threads; // if size not even, the middle doesn't change anyway
    26. // prepare work
    27. QList<Chunk> chunks;
    28. for(int i=0;i<threads;++i) {
    29. Chunk ch;
    30. ch.ba = dat;
    31. ch.size = ba.size();
    32. ch.from = bytesPerThread*i;
    33. ch.cnt = bytesPerThread;
    34. chunks << ch;
    35. }
    36. // execute
    37. QtConcurrent::blockingMap(chunks, swapChunk);
    38. return ba;
    39. }
    40.  
    41. int main() {
    42. for(int i=0;i<10;++i) {
    43. ba += "0123456789";
    44. }
    45. qDebug() << reverseInParallel(ba);
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th January 2013 at 21:01.
    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.


  8. The following 2 users say thank you to wysota for this useful post:

    alrawab (12th January 2013), boudie (12th January 2013)

  9. #7
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reversing QString and QByteArray

    There is std::reverse()
    i know my question concerning is there pure Qt procedure to do that with out usin std
    thanks a lot

  10. #8
    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: Reversing QString and QByteArray

    Using what the C++ language offers doesn't make you "less Qt". That's often a newbie mistake to search for "Qt solutions" if they already have C++ solutions. You can't program Qt without C++.
    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.


Similar Threads

  1. Get QByteArray from QString
    By Coder5546 in forum Newbie
    Replies: 1
    Last Post: 21st December 2012, 18:03
  2. QByteArray to Qstring
    By bmps in forum Qt Programming
    Replies: 6
    Last Post: 16th November 2011, 20:02
  3. QByteArray to QString
    By hvitual in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2009, 10:43
  4. QByteArray to QString
    By babu198649 in forum Newbie
    Replies: 7
    Last Post: 6th December 2007, 13:08
  5. How to get QByteArray from QString in Qt3
    By joseph in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2007, 09:23

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.