michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef GrBinHashKey_DEFINED michael@0: #define GrBinHashKey_DEFINED michael@0: michael@0: #include "GrTypes.h" michael@0: michael@0: /** michael@0: * GrBinHashKey is a hash key class that can take a data chunk of any predetermined michael@0: * length. The hash function used is the One-at-a-Time Hash michael@0: * (http://burtleburtle.net/bob/hash/doobs.html). michael@0: */ michael@0: template michael@0: class GrBinHashKey { michael@0: public: michael@0: enum { kKeySize = KEY_SIZE }; michael@0: michael@0: GrBinHashKey() { michael@0: this->reset(); michael@0: } michael@0: michael@0: void reset() { michael@0: fHash = 0; michael@0: #ifdef SK_DEBUG michael@0: fIsValid = false; michael@0: #endif michael@0: } michael@0: michael@0: void setKeyData(const uint32_t* SK_RESTRICT data) { michael@0: SK_COMPILE_ASSERT(KEY_SIZE % 4 == 0, key_size_mismatch); michael@0: memcpy(&fData, data, KEY_SIZE); michael@0: michael@0: uint32_t hash = 0; michael@0: size_t len = KEY_SIZE; michael@0: while (len >= 4) { michael@0: hash += *data++; michael@0: hash += (hash << 10); michael@0: hash ^= (hash >> 6); michael@0: len -= 4; michael@0: } michael@0: hash += (hash << 3); michael@0: hash ^= (hash >> 11); michael@0: hash += (hash << 15); michael@0: #ifdef SK_DEBUG michael@0: fIsValid = true; michael@0: #endif michael@0: fHash = hash; michael@0: } michael@0: michael@0: bool operator==(const GrBinHashKey& key) const { michael@0: SkASSERT(fIsValid && key.fIsValid); michael@0: if (fHash != key.fHash) { michael@0: return false; michael@0: } michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { michael@0: if (fData[i] != key.fData[i]) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool operator<(const GrBinHashKey& key) const { michael@0: SkASSERT(fIsValid && key.fIsValid); michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { michael@0: if (fData[i] < key.fData[i]) { michael@0: return true; michael@0: } else if (fData[i] > key.fData[i]) { michael@0: return false; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: uint32_t getHash() const { michael@0: SkASSERT(fIsValid); michael@0: return fHash; michael@0: } michael@0: michael@0: const uint8_t* getData() const { michael@0: SkASSERT(fIsValid); michael@0: return reinterpret_cast(fData); michael@0: } michael@0: michael@0: private: michael@0: uint32_t fHash; michael@0: uint32_t fData[KEY_SIZE / sizeof(uint32_t)]; // Buffer for key storage. michael@0: michael@0: #ifdef SK_DEBUG michael@0: public: michael@0: bool fIsValid; michael@0: #endif michael@0: }; michael@0: michael@0: #endif