* A helper class to implement hashing for aggregate types. * * @code * class MyType { * ... * uint hash() const * { * KDevHash hash; * return hash << m_1 << m_2 << ...; * } * }; * @endcode */
| 24 | * @endcode |
| 25 | */ |
| 26 | class KDevHash |
| 27 | { |
| 28 | public: |
| 29 | enum { |
| 30 | DEFAULT_SEED = 2166136261u |
| 31 | }; |
| 32 | |
| 33 | explicit KDevHash(uint hash = DEFAULT_SEED) |
| 34 | : m_hash(hash) |
| 35 | {} |
| 36 | |
| 37 | KDevHash(const KDevHash&) = delete; |
| 38 | KDevHash& operator=(const KDevHash&) = delete; |
| 39 | |
| 40 | inline operator uint() const |
| 41 | { |
| 42 | return m_hash; |
| 43 | } |
| 44 | |
| 45 | template <typename T> |
| 46 | inline KDevHash& operator<<(T value) |
| 47 | { |
| 48 | m_hash = hash_combine(m_hash, qHash(value)); |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | static inline uint hash_combine(uint seed, uint hash) |
| 53 | { |
| 54 | // this is copied from boost::hash_combine |
| 55 | return seed ^ (hash + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | uint m_hash; |
| 60 | }; |
| 61 | |
| 62 | #endif //KDEVPLATFORM_KDEVHASH_H |