Results 1 to 7 of 7

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

  1. #1
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

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

    I would need to keep track of a sorted (the order does matter) list of entries that should nevertheless be hashed using a QString for quick look up.
    If i do understand the Qt generic collection classes correctly this cannot be done or at least not in an easy way.
    - QHash provides the keyed access but does not keep track of the order of insertion
    - QList on the other hand has it's order but does not support hashed access
    What would be the best way to solve this in Qt?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    What about QMap? Ordered by key..

    and in case of multiple values for the same key.. by insertion-order:

    "Returns a list containing all the values associated with key key, from the most recently inserted to the least recently inserted one."

    Johannes

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

    doberkofler (21st March 2010)

  4. #3
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

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

    Unfortunately I need to order of insertion and this would not seem to work with QMap.

  5. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Just keep a separate QList pointing to the same items then. That's how I did that in the past.

    You can of course write your own ListHash class that provides/handles both.

    There is not a single class for that, because the internal structure of a hashmap doesn't allow to keep the order of insertion plainly visible. Items are put to a certain slot (determined by the hash function) of the hashmap regardless of when they are added. That's what makes the lookup fast. You can't efficiently provide a sequential access in that same data structure. You just use a second one..

    Johannes
    Last edited by JohannesMunk; 21st March 2010 at 22:03.

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

    doberkofler (22nd March 2010)

  7. #5
    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: 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.


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

    doberkofler (22nd March 2010)

  9. #6
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

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

    Thank you for the reply! Using two lists and sharing the items seems to be a reasonable approach but I guess this would only be possible when using pointers (dynamically allovated items) to share them between two columns or is there also a way to share items in a collection with valkue semantics?

  10. #7
    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: A Qt collection that is ordered and can be accessed via hash key ... ?

    "Implicit" and "explicit" sharing are the magic words here.
    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. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.