blob: 936eab0c14bb6712110edf079459af3b0822feab [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#ifndef GrBinHashKey_DEFINED
11#define GrBinHashKey_DEFINED
12
13#include "GrTypes.h"
14
15/**
junov@google.comf7c00f62011-08-18 18:15:16 +000016 * 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.com0d3f1fb2011-06-01 19:27:31 +000018 * (http://burtleburtle.net/bob/hash/doobs.html).
junov@google.comf93e7172011-03-31 21:26:24 +000019 */
junov@google.comf7c00f62011-08-18 18:15:16 +000020template<typename Entry, size_t KeySize>
21class GrBinHashKey {
junov@google.comf93e7172011-03-31 21:26:24 +000022public:
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000023 GrBinHashKey()
junov@google.comf7c00f62011-08-18 18:15:16 +000024 : fHash(0)
junov@google.comf93e7172011-03-31 21:26:24 +000025#if GR_DEBUG
junov@google.comf7c00f62011-08-18 18:15:16 +000026 , fIsValid(false)
junov@google.comf93e7172011-03-31 21:26:24 +000027#endif
28 {}
29
junov@google.comf7c00f62011-08-18 18:15:16 +000030 GrBinHashKey(const GrBinHashKey<Entry, KeySize>& other) {
31 *this = other;
junov@google.comf93e7172011-03-31 21:26:24 +000032 }
junov@google.comf7c00f62011-08-18 18:15:16 +000033 GrBinHashKey<Entry, KeySize>& operator=(const GrBinHashKey<Entry,
34 KeySize>& other) {
35 memcpy(this, &other, sizeof(*this));
36 return *this;
junov@google.comf93e7172011-03-31 21:26:24 +000037 }
38
39 virtual ~GrBinHashKey() {
junov@google.comf93e7172011-03-31 21:26:24 +000040 }
41
junov@google.comf7c00f62011-08-18 18:15:16 +000042 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.comf93e7172011-03-31 21:26:24 +000053 }
junov@google.comf7c00f62011-08-18 18:15:16 +000054 fHash += (fHash << 3);
55 fHash ^= (fHash >> 11);
56 fHash += (fHash << 15);
57#if GR_DEBUG
58 fIsValid = true;
59#endif
junov@google.comf93e7172011-03-31 21:26:24 +000060 }
61
junov@google.comf7c00f62011-08-18 18:15:16 +000062 int compare(const GrBinHashKey<Entry, KeySize>& key) const {
63 GrAssert(fIsValid && key.fIsValid);
64 return memcmp(fData, key.fData, KeySize);
junov@google.comf93e7172011-03-31 21:26:24 +000065 }
66
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000067 static bool
junov@google.comf7c00f62011-08-18 18:15:16 +000068 EQ(const Entry& entry, const GrBinHashKey<Entry, KeySize>& key) {
junov@google.comf93e7172011-03-31 21:26:24 +000069 GrAssert(key.fIsValid);
70 return 0 == entry.compare(key);
71 }
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000072
73 static bool
junov@google.comf7c00f62011-08-18 18:15:16 +000074 LT(const Entry& entry, const GrBinHashKey<Entry, KeySize>& key) {
junov@google.comf93e7172011-03-31 21:26:24 +000075 GrAssert(key.fIsValid);
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000076 return entry.compare(key) < 0;
junov@google.comf93e7172011-03-31 21:26:24 +000077 }
78
79 uint32_t getHash() const {
80 GrAssert(fIsValid);
junov@google.comf7c00f62011-08-18 18:15:16 +000081 return fHash;
junov@google.comf93e7172011-03-31 21:26:24 +000082 }
83
84private:
junov@google.comf7c00f62011-08-18 18:15:16 +000085 uint32_t fHash;
86 uint8_t fData[KeySize]; //Buffer for key storage
junov@google.comf93e7172011-03-31 21:26:24 +000087
88#if GR_DEBUG
89public:
90 bool fIsValid;
91#endif
92};
93
94#endif