Results 1 to 7 of 7

Thread: A Qt collection that is ordered and can be accessed via hash key ... ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Qt collection that is ordered and can be accessed via hash key ... ?

    Quote Originally Posted by doberkofler View Post
    Unfortunately I need to order of insertion and this would not seem to work with QMap.
    You can't have both at once without duplicating information. Hash-based access requires items to be ordered (somehow) according to the hash, otherwise using the hash has no benefit. Long story short - you need two structures, as already suggested.

    But I would do it the other way round - keep the "main" data in the list and have a hash for mapping item id to position in the sequence. It somehow seems more appropriate to me than storing data in the hash and then the pointers to those items in the list.

    Qt Code:
    1. template <class T> class HashedList : public QVector<T> {
    2. public:
    3. //...
    4. void addItem(const T &item) {
    5. int h = qHash(item);
    6. int pos = count();
    7. m_hash.insert(h, pos);
    8. append(item);
    9. }
    10. int hashedIndexOf(const T& item) const {
    11. int h = qHash(item);
    12. return m_hash.value(h, -1);
    13. }
    14. // etc.
    15. private:
    16. QHash<int, int> m_hash;
    17. };
    To copy to clipboard, switch view to plain text mode 

    Note that this REQUIRES hashes to be unique. If you can't guarantee that, substitute the integer with the item "name" (QString). Alternatively make your class implicitly or better yet explicitly shared and have two separate structures with shallow copies of the items. With implicit sharing this gets tricky, explicit sharing is best. An alternative is to use pointers as JohannesMunk suggested.
    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.


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

    doberkofler (22nd March 2010)

Similar Threads

  1. Replies: 1
    Last Post: 10th February 2009, 09:42
  2. QHash
    By coderbob in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2008, 14:29
  3. QHash ordered by value
    By Lele in forum Qt Programming
    Replies: 7
    Last Post: 11th February 2008, 09:30
  4. QHash with 2 keys?
    By MrGarbage in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2007, 01:09
  5. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17

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
  •  
Qt is a trademark of The Qt Company.