QMultiMap insertion



  • My application reads 5,000 files each containing 100 to 10,000 records into 5,000 QMultiMap by QDateTime. Many of the QDateTime keys are equal because the original data only reports to the second. Speed is important because there are 5,000,000 records in 5,000 QMultiMap.

    The problem for me is that the duplicate keys are sorted backwards. The records are inserted in this order;

    Key Data
    2015-11-28 0003 1
    2015-11-28 0003 2
    2015-11-28 0003 3
    2015-11-28 0003 4

    and a foreach loop returns

    2015-11-28 0003 4
    2015-11-28 0003 3
    2015-11-28 0003 2
    2015-11-28 0003 1

    The end users can't tolerate the output reversed.

    Am I stuck converting to a std::multimap? I understand that usually preserves the insertion order of duplicate keys, but is not guaranteed.

    The data used to be stored in QList and sorted with qStableSort, but I converted to QMap because qSort and qStableSort are deprecated.

    I suppose that if I switch to the STL for this I should store the records as std::vector and use std::sort because a std::sort std::vector is much fast than std::sortstd::list

    Ideas welcome.

    Thanks - Ken


  • Lifetime Qt Champion

    Hi,

    Why not use stable_sort ? The Qt Algorithms have been deprecated because you can use all the STL ones directly.



  • @SGaist

    Thanks for the quick reply.

    Until a few days ago the data was held in a QList <MyClass>. The data is supposed to arrive already sorted by date-time, but there are exceptions. The the QList::sort() method was just too slow so I tried converting to a QMultiMap to insert the data by QDateTime. This involved a tolerable amount of change to the code.

    Changing back, could I change QMap <QDateTime, MyClass> to QList <MyClass>
    and use std::stable_sort(dataList.toStd.begin(), dataList.toStd.end(), MyClass::less)?

    The MyClass < method is already available in place, so that's not an issue. It wasn't obvious to me that QList::toStd() would produce a reference to the underlying list letting me sort in place with std::stable_sort.

    Anyway, I have changed the data storage to a std:vector and use std::stable_sort because sorting on a std::vector is so much faster than on a std::list.

    The old qStableSort was very fast on a QList, so I'm sure the QT designers were taking advantage of the way a QList is stored. I guess I jumped the gun on removing deprecated features.

    • Ken

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.