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 | /** |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 16 | * Hash function class that can take a data chunk of any predetermined |
| 17 | * length. The hash function used is the One-at-a-Time Hash |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 18 | * (http://burtleburtle.net/bob/hash/doobs.html). |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 19 | */ |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 20 | template<typename Entry, size_t KeySize> |
| 21 | class GrBinHashKey { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 22 | public: |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 23 | GrBinHashKey() |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 24 | : fHash(0) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | #if GR_DEBUG |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 26 | , fIsValid(false) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 27 | #endif |
| 28 | {} |
| 29 | |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 30 | GrBinHashKey(const GrBinHashKey<Entry, KeySize>& other) { |
| 31 | *this = other; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 32 | } |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 33 | GrBinHashKey<Entry, KeySize>& operator=(const GrBinHashKey<Entry, |
| 34 | KeySize>& other) { |
| 35 | memcpy(this, &other, sizeof(*this)); |
| 36 | return *this; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | virtual ~GrBinHashKey() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 42 | void setKeyData(const uint32_t *data) { |
| 43 | GrAssert(GrIsALIGN4(KeySize)); |
| 44 | memcpy(&fData, data, KeySize); |
| 45 | |
| 46 | fHash = 0; |
| 47 | size_t len = KeySize; |
| 48 | while (len >= 4) { |
| 49 | fHash += *data++; |
| 50 | fHash += (fHash << 10); |
| 51 | fHash ^= (fHash >> 6); |
| 52 | len -= 4; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 53 | } |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 54 | fHash += (fHash << 3); |
| 55 | fHash ^= (fHash >> 11); |
| 56 | fHash += (fHash << 15); |
| 57 | #if GR_DEBUG |
| 58 | fIsValid = true; |
| 59 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 62 | int compare(const GrBinHashKey<Entry, KeySize>& key) const { |
| 63 | GrAssert(fIsValid && key.fIsValid); |
| 64 | return memcmp(fData, key.fData, KeySize); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 67 | static bool |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 68 | EQ(const Entry& entry, const GrBinHashKey<Entry, KeySize>& key) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 69 | GrAssert(key.fIsValid); |
| 70 | return 0 == entry.compare(key); |
| 71 | } |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 72 | |
| 73 | static bool |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 74 | LT(const Entry& entry, const GrBinHashKey<Entry, KeySize>& key) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 75 | GrAssert(key.fIsValid); |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 76 | return entry.compare(key) < 0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | uint32_t getHash() const { |
| 80 | GrAssert(fIsValid); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 81 | return fHash; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private: |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame^] | 85 | uint32_t fHash; |
| 86 | uint8_t fData[KeySize]; //Buffer for key storage |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 87 | |
| 88 | #if GR_DEBUG |
| 89 | public: |
| 90 | bool fIsValid; |
| 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | #endif |