epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #ifndef GrBinHashKey_DEFINED |
| 11 | #define GrBinHashKey_DEFINED |
| 12 | |
| 13 | #include "GrTypes.h" |
| 14 | |
| 15 | /** |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 16 | * Hash function class that can take a data chunk of any predetermined length. The hash function |
| 17 | * used is the One-at-a-Time Hash (http://burtleburtle.net/bob/hash/doobs.html). |
| 18 | * |
| 19 | * Keys are computed from Entry objects. Entry must be fully ordered by a member: |
| 20 | * int compare(const GrTBinHashKey<Entry, ..>& k); |
| 21 | * which returns negative if the Entry < k, 0 if it equals k, and positive if k < the Entry. |
| 22 | * Additionally, Entry must be flattenable into the key using setKeyData. |
| 23 | * |
| 24 | * This class satisfies the requirements to be a key for a GrTHashTable. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | */ |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 26 | template<typename Entry, size_t KeySize> |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 27 | class GrTBinHashKey { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 28 | public: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 29 | GrTBinHashKey() { |
| 30 | this->reset(); |
| 31 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 33 | GrTBinHashKey(const GrTBinHashKey<Entry, KeySize>& other) { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 34 | *this = other; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 35 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 36 | |
| 37 | GrTBinHashKey<Entry, KeySize>& operator=(const GrTBinHashKey<Entry, KeySize>& other) { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 38 | memcpy(this, &other, sizeof(*this)); |
| 39 | return *this; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 42 | ~GrTBinHashKey() { |
| 43 | } |
| 44 | |
| 45 | void reset() { |
| 46 | fHash = 0; |
| 47 | #if GR_DEBUG |
| 48 | fIsValid = false; |
| 49 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 52 | void setKeyData(const uint32_t* SK_RESTRICT data) { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 53 | GrAssert(GrIsALIGN4(KeySize)); |
| 54 | memcpy(&fData, data, KeySize); |
| 55 | |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 56 | uint32_t hash = 0; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 57 | size_t len = KeySize; |
| 58 | while (len >= 4) { |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 59 | hash += *data++; |
| 60 | hash += (fHash << 10); |
| 61 | hash ^= (hash >> 6); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 62 | len -= 4; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 63 | } |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 64 | hash += (fHash << 3); |
| 65 | hash ^= (fHash >> 11); |
| 66 | hash += (fHash << 15); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 67 | #if GR_DEBUG |
| 68 | fIsValid = true; |
| 69 | #endif |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 70 | fHash = hash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 73 | int compare(const GrTBinHashKey<Entry, KeySize>& key) const { |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 74 | GrAssert(fIsValid && key.fIsValid); |
| 75 | return memcmp(fData, key.fData, KeySize); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 78 | static bool EQ(const Entry& entry, const GrTBinHashKey<Entry, KeySize>& key) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 79 | GrAssert(key.fIsValid); |
| 80 | return 0 == entry.compare(key); |
| 81 | } |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 82 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 83 | static bool LT(const Entry& entry, const GrTBinHashKey<Entry, KeySize>& key) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 84 | GrAssert(key.fIsValid); |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 85 | return entry.compare(key) < 0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | uint32_t getHash() const { |
| 89 | GrAssert(fIsValid); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 90 | return fHash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 94 | uint32_t fHash; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame^] | 95 | uint8_t fData[KeySize]; // Buffer for key storage |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 96 | |
| 97 | #if GR_DEBUG |
| 98 | public: |
| 99 | bool fIsValid; |
| 100 | #endif |
| 101 | }; |
| 102 | |
| 103 | #endif |