1 2/* 3 * Copyright 2014 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. 7 */ 8 9 10#ifndef GrMurmur3HashKey_DEFINED 11#define GrMurmur3HashKey_DEFINED 12 13#include "SkChecksum.h" 14#include "GrTypes.h" 15 16/** 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 */ 21template<size_t KEY_SIZE_IN_BYTES> 22class GrMurmur3HashKey { 23public: 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 63private: 64 uint32_t fHash; 65 uint32_t fData[KEY_SIZE_IN_BYTES / sizeof(uint32_t)]; // Buffer for key storage. 66 67#ifdef SK_DEBUG 68public: 69 bool fIsValid; 70#endif 71}; 72 73#endif 74