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 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 13 | #include "SkChecksum.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 14 | #include "GrTypes.h" |
| 15 | |
| 16 | /** |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 17 | * GrMurmur3HashKey is a hash key class that can take a data chunk of any predetermined |
| 18 | * length. It uses the Murmur3 hash function. It is intended to be used with |
| 19 | * SkTDynamicHash (where GrBinHashKey is for GrTHashTable). |
| 20 | */ |
| 21 | template<size_t KEY_SIZE_IN_BYTES> |
| 22 | class GrMurmur3HashKey { |
| 23 | public: |
| 24 | GrMurmur3HashKey() { |
| 25 | this->reset(); |
| 26 | } |
| 27 | |
| 28 | void reset() { |
| 29 | fHash = 0; |
| 30 | #ifdef SK_DEBUG |
| 31 | fIsValid = false; |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | void setKeyData(const uint32_t* data) { |
| 36 | SK_COMPILE_ASSERT(KEY_SIZE_IN_BYTES % 4 == 0, key_size_mismatch); |
| 37 | memcpy(fData, data, KEY_SIZE_IN_BYTES); |
| 38 | |
| 39 | fHash = SkChecksum::Murmur3(fData, KEY_SIZE_IN_BYTES); |
| 40 | #ifdef SK_DEBUG |
| 41 | fIsValid = true; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | bool operator==(const GrMurmur3HashKey& other) const { |
| 46 | if (fHash != other.fHash) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | return !memcmp(fData, other.fData, KEY_SIZE_IN_BYTES); |
| 51 | } |
| 52 | |
| 53 | uint32_t getHash() const { |
| 54 | SkASSERT(fIsValid); |
| 55 | return fHash; |
| 56 | } |
| 57 | |
| 58 | const uint8_t* getData() const { |
| 59 | SkASSERT(fIsValid); |
| 60 | return reinterpret_cast<const uint8_t*>(fData); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | uint32_t fHash; |
| 65 | uint32_t fData[KEY_SIZE_IN_BYTES / sizeof(uint32_t)]; // Buffer for key storage. |
| 66 | |
| 67 | #ifdef SK_DEBUG |
| 68 | public: |
| 69 | bool fIsValid; |
| 70 | #endif |
| 71 | }; |
| 72 | |
| 73 | /** |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 74 | * GrBinHashKey is a hash key class that can take a data chunk of any predetermined |
| 75 | * length. The hash function used is the One-at-a-Time Hash |
| 76 | * (http://burtleburtle.net/bob/hash/doobs.html). |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 77 | */ |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 78 | template<size_t KEY_SIZE> |
| 79 | class GrBinHashKey { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 80 | public: |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 81 | enum { kKeySize = KEY_SIZE }; |
| 82 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 83 | GrBinHashKey() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 84 | this->reset(); |
| 85 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 87 | void reset() { |
| 88 | fHash = 0; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 89 | #ifdef SK_DEBUG |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 90 | fIsValid = false; |
| 91 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 94 | void setKeyData(const uint32_t* SK_RESTRICT data) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 95 | SK_COMPILE_ASSERT(KEY_SIZE % 4 == 0, key_size_mismatch); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 96 | memcpy(&fData, data, KEY_SIZE); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 98 | uint32_t hash = 0; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 99 | size_t len = KEY_SIZE; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 100 | while (len >= 4) { |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 101 | hash += *data++; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 102 | hash += (hash << 10); |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 103 | hash ^= (hash >> 6); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 104 | len -= 4; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 105 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 106 | hash += (hash << 3); |
| 107 | hash ^= (hash >> 11); |
| 108 | hash += (hash << 15); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 109 | #ifdef SK_DEBUG |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 110 | fIsValid = true; |
| 111 | #endif |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 112 | fHash = hash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 113 | } |
| 114 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 115 | bool operator==(const GrBinHashKey<KEY_SIZE>& key) const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 116 | SkASSERT(fIsValid && key.fIsValid); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 117 | if (fHash != key.fHash) { |
| 118 | return false; |
| 119 | } |
| 120 | for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { |
| 121 | if (fData[i] != key.fData[i]) { |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | return true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 126 | } |
| 127 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 128 | bool operator<(const GrBinHashKey<KEY_SIZE>& key) const { |
| 129 | SkASSERT(fIsValid && key.fIsValid); |
| 130 | for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { |
| 131 | if (fData[i] < key.fData[i]) { |
| 132 | return true; |
| 133 | } else if (fData[i] > key.fData[i]) { |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | return false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | uint32_t getHash() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 141 | SkASSERT(fIsValid); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 142 | return fHash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 145 | const uint8_t* getData() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 146 | SkASSERT(fIsValid); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 147 | return reinterpret_cast<const uint8_t*>(fData); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 148 | } |
| 149 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 150 | private: |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 151 | uint32_t fHash; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 152 | uint32_t fData[KEY_SIZE / sizeof(uint32_t)]; // Buffer for key storage. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 153 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 154 | #ifdef SK_DEBUG |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 155 | public: |
| 156 | bool fIsValid; |
| 157 | #endif |
| 158 | }; |
| 159 | |
| 160 | #endif |