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 | /** |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 16 | * GrBinHashKey is a hash key class that can take a data chunk of any predetermined |
| 17 | * length. The hash function used is the One-at-a-Time Hash |
| 18 | * (http://burtleburtle.net/bob/hash/doobs.html). |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 19 | */ |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 20 | template<size_t KEY_SIZE> |
| 21 | class GrBinHashKey { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 22 | public: |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 23 | enum { kKeySize = KEY_SIZE }; |
| 24 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 25 | GrBinHashKey() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 26 | this->reset(); |
| 27 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 28 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 29 | void reset() { |
| 30 | fHash = 0; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 31 | #ifdef SK_DEBUG |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 32 | fIsValid = false; |
| 33 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 36 | void setKeyData(const uint32_t* SK_RESTRICT data) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 37 | SK_COMPILE_ASSERT(KEY_SIZE % 4 == 0, key_size_mismatch); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 38 | memcpy(&fData, data, KEY_SIZE); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 40 | uint32_t hash = 0; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 41 | size_t len = KEY_SIZE; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 42 | while (len >= 4) { |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 43 | hash += *data++; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 44 | hash += (hash << 10); |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 45 | hash ^= (hash >> 6); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 46 | len -= 4; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 47 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 48 | hash += (hash << 3); |
| 49 | hash ^= (hash >> 11); |
| 50 | hash += (hash << 15); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 51 | #ifdef SK_DEBUG |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 52 | fIsValid = true; |
| 53 | #endif |
bsalomon@google.com | 6b5fdc1 | 2011-12-12 22:35:18 +0000 | [diff] [blame] | 54 | fHash = hash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 57 | bool operator==(const GrBinHashKey<KEY_SIZE>& key) const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 58 | SkASSERT(fIsValid && key.fIsValid); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 59 | if (fHash != key.fHash) { |
| 60 | return false; |
| 61 | } |
| 62 | for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { |
| 63 | if (fData[i] != key.fData[i]) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | return true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 70 | bool operator<(const GrBinHashKey<KEY_SIZE>& key) const { |
| 71 | SkASSERT(fIsValid && key.fIsValid); |
| 72 | for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { |
| 73 | if (fData[i] < key.fData[i]) { |
| 74 | return true; |
| 75 | } else if (fData[i] > key.fData[i]) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | return false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | uint32_t getHash() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 83 | SkASSERT(fIsValid); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 84 | return fHash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 87 | const uint8_t* getData() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 88 | SkASSERT(fIsValid); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 89 | return reinterpret_cast<const uint8_t*>(fData); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 90 | } |
| 91 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 92 | private: |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 93 | uint32_t fHash; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 94 | 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] | 95 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 96 | #ifdef SK_DEBUG |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 97 | public: |
| 98 | bool fIsValid; |
| 99 | #endif |
| 100 | }; |
| 101 | |
| 102 | #endif |