Results 1 to 10 of 10

Thread: QHash add comparison function, how?

  1. #1
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QHash add comparison function, how?

    Hi,

    In std::map (stl) you can define the key and value types, but also the comparison function for the keys.
    I want to do the same for a QHash, but I reading QtAssistant and googling and the only things I can define are key and value type.

    Can I set the comparison function for a QHash? If yes, how?

    Thanks.

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHash add comparison function, how?

    You not only can, but you have to define the function:
    Qt Code:
    1. quint32 qHash(const Key &key);
    To copy to clipboard, switch view to plain text mode 
    unless you're working with some standard type, for which the function already has been provided.

  3. #3
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash add comparison function, how?

    I'm using a QPair as a key, and I want to use another comparison function and not the given one.
    I tried to derived QPair an use it as a key:
    Qt Code:
    1. template <class T1, class T2>
    2. struct stEvPairKey : QPair<T1, T2>
    3. {
    4. stEvPairKey() : QPair<T1, T2>() {}
    5. stEvPairKey(const T1 &t1, const T2 &t2) : QPair<T1, T2>(t1, t2) {}
    6. };
    7. template <class T1, class T2>
    8. bool operator==(const stEvPairKey<T1, T2> &p1, const stEvPairKey<T1, T2> &p2)
    9. {
    10. return (p1.first == p2.first) && (p1.second & p2.second);
    11. }
    To copy to clipboard, switch view to plain text mode 

    and use this struct as a key:
    Qt Code:
    1. QHash<stEvPairKey<int, unsigned long>, CMyClass*> m_hash;
    To copy to clipboard, switch view to plain text mode 

    But never goes into my "operator==".
    The function you give:
    Qt Code:
    1. quint32 qHash(const Key &key);
    To copy to clipboard, switch view to plain text mode 
    is to compute the hash value for the key, but not to compare 2 keys.

    What is wrong?

    Thank you.

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

    Default Re: QHash add comparison function, how?

    Doesn't QPair have the operator already defined the way you defined it?
    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.


  5. #5
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHash add comparison function, how?

    As you will see in the following program, both the equality operator and the hash function get called.
    Qt Code:
    1. #include <QHash>
    2. #include <QDebug>
    3.  
    4. template <class T1, class T2>
    5. struct stEvPairKey : QPair<T1, T2>
    6. {
    7. stEvPairKey() : QPair<T1, T2>() {}
    8. stEvPairKey(const T1 &t1, const T2 &t2) : QPair<T1, T2>(t1, t2) {}
    9. };
    10. template <class T1, class T2>
    11. bool operator==(const stEvPairKey<T1, T2> &p1, const stEvPairKey<T1, T2> &p2)
    12. {
    13. qDebug()<<"comparison";
    14. return (p1.first == p2.first) && (p1.second & p2.second);
    15. }
    16. template <class T1, class T2>
    17. quint32 qHash(const stEvPairKey<T1,T2>&key)
    18. {
    19. qDebug()<<"hash";
    20. return qHash((QPair<T1,T2>)key);
    21. }
    22. void main()
    23. {
    24.  
    25. QHash<stEvPairKey<int, unsigned long>, bool> m_hash;
    26. m_hash.insert(stEvPairKey<int, unsigned long>(1, 2), true);
    27. if(m_hash.contains(stEvPairKey<int, unsigned long>(1, 2)))
    28. qDebug()<<"true";
    29. }
    To copy to clipboard, switch view to plain text mode 
    Although, as Wysota pointed out, both functions are unnecessary because QPair provides those functions.

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

    holst (30th March 2010)

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

    Default Re: QHash add comparison function, how?

    By the way... qHash() is for comparing two keys.
    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:

    holst (30th March 2010)

  9. #7
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash add comparison function, how?

    ok, at last I understand what is happening.
    The second part of the key T2 are some flags. For example this code
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include <QMultiHash>
    4. #include <QDebug>
    5. #include <QString>
    6.  
    7. template <class T1, class T2>
    8. struct stEvPairKey : QPair<T1, T2>
    9. {
    10. stEvPairKey() : QPair<T1, T2>() {}
    11. stEvPairKey(const T1 &t1, const T2 &t2) : QPair<T1, T2>(t1, t2) {}
    12. };
    13. template <class T1, class T2>
    14. bool operator==(const stEvPairKey<T1, T2> &p1, const stEvPairKey<T1, T2> &p2)
    15. {
    16. qDebug()<<"comparison";
    17. return (p1.first == p2.first) && (p1.second & p2.second);
    18. }
    19. template <class T1, class T2>
    20. quint32 qHash(const stEvPairKey<T1,T2>&key)
    21. {
    22. qDebug()<<"hash: " << qHash((QPair<T1,T2>)key);
    23. return qHash((QPair<T1,T2>)key);
    24. }
    25.  
    26. enum EFlags
    27. {
    28. NONE_FLAGS = 0,
    29.  
    30. FLAG1 = 1,
    31. FLAG2 = 2,
    32. FLAG3 = 4,
    33. };
    34.  
    35. int main(int argc, char *argv[])
    36. {
    37. QCoreApplication a(argc, argv);
    38.  
    39. QHash<stEvPairKey<int, unsigned long>, bool> m_hash;
    40. m_hash.insert(stEvPairKey<int, unsigned long>(1, FLAG1 | FLAG3), true);
    41. if(m_hash.contains(stEvPairKey<int, unsigned long>(1, FLAG1)))
    42. qDebug()<<"FLAG1: true";
    43. if(m_hash.contains(stEvPairKey<int, unsigned long>(1, FLAG1 | FLAG3)))
    44. qDebug()<<"FLAG1 | FLAG3: true";
    45.  
    46. return a.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 

    The output is:
    hash: 65541
    hash: 65541
    hash: 65537
    hash: 65541
    comparison
    FLAG1 | FLAG3: true
    My idea was to implement the operator== for stEvPairKey so the 1st "if()" be also true. But as is shown in the output the hash value is different for "FLAG1" and for "FLAG1 | FLAG3".
    I was confused, the flags can't be part of the hash key.

    Thank you very much

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

    Default Re: QHash add comparison function, how?

    Maybe they should be a part of value and not the key then?
    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.


  11. #9
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash add comparison function, how?

    Yes, that's right.
    I made the change and works. I was confused, I was only thinking in the comparison function, when "contains()" is called, and no the "qHash()".

    Thank you again.

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

    Default Re: QHash add comparison function, how?

    QHash::contains() will call qHash() for the key.
    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: 3
    Last Post: 12th March 2012, 01:45
  2. Replies: 1
    Last Post: 10th February 2009, 09:42
  3. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17
  4. String comparison
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2006, 12:31
  5. case insensitive comparison.
    By munna in forum Newbie
    Replies: 1
    Last Post: 7th May 2006, 16:27

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.