3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef GrBinHashKey_DEFINED
11 #define GrBinHashKey_DEFINED
13 #include "SkChecksum.h"
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).
21 template<size_t KEY_SIZE_IN_BYTES>
22 class GrMurmur3HashKey {
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);
39 fHash = SkChecksum::Murmur3(fData, KEY_SIZE_IN_BYTES);
45 bool operator==(const GrMurmur3HashKey& other) const {
46 if (fHash != other.fHash) {
50 return !memcmp(fData, other.fData, KEY_SIZE_IN_BYTES);
53 uint32_t getHash() const {
58 const uint8_t* getData() const {
60 return reinterpret_cast<const uint8_t*>(fData);
65 uint32_t fData[KEY_SIZE_IN_BYTES / sizeof(uint32_t)]; // Buffer for key storage.
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).
78 template<size_t KEY_SIZE>
81 enum { kKeySize = KEY_SIZE };
94 void setKeyData(const uint32_t* SK_RESTRICT data) {
95 SK_COMPILE_ASSERT(KEY_SIZE % 4 == 0, key_size_mismatch);
96 memcpy(&fData, data, KEY_SIZE);
99 size_t len = KEY_SIZE;
102 hash += (hash << 10);
107 hash ^= (hash >> 11);
108 hash += (hash << 15);
115 bool operator==(const GrBinHashKey<KEY_SIZE>& key) const {
116 SkASSERT(fIsValid && key.fIsValid);
117 if (fHash != key.fHash) {
120 for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) {
121 if (fData[i] != key.fData[i]) {
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]) {
133 } else if (fData[i] > key.fData[i]) {
140 uint32_t getHash() const {
145 const uint8_t* getData() const {
147 return reinterpret_cast<const uint8_t*>(fData);
152 uint32_t fData[KEY_SIZE / sizeof(uint32_t)]; // Buffer for key storage.