1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/GrBinHashKey.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef GrBinHashKey_DEFINED 1.14 +#define GrBinHashKey_DEFINED 1.15 + 1.16 +#include "GrTypes.h" 1.17 + 1.18 +/** 1.19 + * GrBinHashKey is a hash key class that can take a data chunk of any predetermined 1.20 + * length. The hash function used is the One-at-a-Time Hash 1.21 + * (http://burtleburtle.net/bob/hash/doobs.html). 1.22 + */ 1.23 +template<size_t KEY_SIZE> 1.24 +class GrBinHashKey { 1.25 +public: 1.26 + enum { kKeySize = KEY_SIZE }; 1.27 + 1.28 + GrBinHashKey() { 1.29 + this->reset(); 1.30 + } 1.31 + 1.32 + void reset() { 1.33 + fHash = 0; 1.34 +#ifdef SK_DEBUG 1.35 + fIsValid = false; 1.36 +#endif 1.37 + } 1.38 + 1.39 + void setKeyData(const uint32_t* SK_RESTRICT data) { 1.40 + SK_COMPILE_ASSERT(KEY_SIZE % 4 == 0, key_size_mismatch); 1.41 + memcpy(&fData, data, KEY_SIZE); 1.42 + 1.43 + uint32_t hash = 0; 1.44 + size_t len = KEY_SIZE; 1.45 + while (len >= 4) { 1.46 + hash += *data++; 1.47 + hash += (hash << 10); 1.48 + hash ^= (hash >> 6); 1.49 + len -= 4; 1.50 + } 1.51 + hash += (hash << 3); 1.52 + hash ^= (hash >> 11); 1.53 + hash += (hash << 15); 1.54 +#ifdef SK_DEBUG 1.55 + fIsValid = true; 1.56 +#endif 1.57 + fHash = hash; 1.58 + } 1.59 + 1.60 + bool operator==(const GrBinHashKey<KEY_SIZE>& key) const { 1.61 + SkASSERT(fIsValid && key.fIsValid); 1.62 + if (fHash != key.fHash) { 1.63 + return false; 1.64 + } 1.65 + for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { 1.66 + if (fData[i] != key.fData[i]) { 1.67 + return false; 1.68 + } 1.69 + } 1.70 + return true; 1.71 + } 1.72 + 1.73 + bool operator<(const GrBinHashKey<KEY_SIZE>& key) const { 1.74 + SkASSERT(fIsValid && key.fIsValid); 1.75 + for (size_t i = 0; i < SK_ARRAY_COUNT(fData); ++i) { 1.76 + if (fData[i] < key.fData[i]) { 1.77 + return true; 1.78 + } else if (fData[i] > key.fData[i]) { 1.79 + return false; 1.80 + } 1.81 + } 1.82 + return false; 1.83 + } 1.84 + 1.85 + uint32_t getHash() const { 1.86 + SkASSERT(fIsValid); 1.87 + return fHash; 1.88 + } 1.89 + 1.90 + const uint8_t* getData() const { 1.91 + SkASSERT(fIsValid); 1.92 + return reinterpret_cast<const uint8_t*>(fData); 1.93 + } 1.94 + 1.95 +private: 1.96 + uint32_t fHash; 1.97 + uint32_t fData[KEY_SIZE / sizeof(uint32_t)]; // Buffer for key storage. 1.98 + 1.99 +#ifdef SK_DEBUG 1.100 +public: 1.101 + bool fIsValid; 1.102 +#endif 1.103 +}; 1.104 + 1.105 +#endif