blob: 028bb912bbd49d520b6026292c78eb31f4f4cd42 [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
epoger@google.com17b78942011-08-26 14:40:38 +000039 ~GrBinHashKey() {
junov@google.comf93e7172011-03-31 21:26:24 +000040 }
41
bsalomon@google.com6b5fdc12011-12-12 22:35:18 +000042 void setKeyData(const uint32_t* SK_RESTRICT data) {
junov@google.comf7c00f62011-08-18 18:15:16 +000043 GrAssert(GrIsALIGN4(KeySize));
44 memcpy(&fData, data, KeySize);
45
bsalomon@google.com6b5fdc12011-12-12 22:35:18 +000046 uint32_t hash = 0;
junov@google.comf7c00f62011-08-18 18:15:16 +000047 size_t len = KeySize;
48 while (len >= 4) {
bsalomon@google.com6b5fdc12011-12-12 22:35:18 +000049 hash += *data++;
50 hash += (fHash << 10);
51 hash ^= (hash >> 6);
junov@google.comf7c00f62011-08-18 18:15:16 +000052 len -= 4;
junov@google.comf93e7172011-03-31 21:26:24 +000053 }
bsalomon@google.com6b5fdc12011-12-12 22:35:18 +000054 hash += (fHash << 3);
55 hash ^= (fHash >> 11);
56 hash += (fHash << 15);
junov@google.comf7c00f62011-08-18 18:15:16 +000057#if GR_DEBUG
58 fIsValid = true;
59#endif
bsalomon@google.com6b5fdc12011-12-12 22:35:18 +000060 fHash = hash;
junov@google.comf93e7172011-03-31 21:26:24 +000061 }
62
junov@google.comf7c00f62011-08-18 18:15:16 +000063 int compare(const GrBinHashKey<Entry, KeySize>& key) const {
64 GrAssert(fIsValid && key.fIsValid);
65 return memcmp(fData, key.fData, KeySize);
junov@google.comf93e7172011-03-31 21:26:24 +000066 }
67
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000068 static bool
junov@google.comf7c00f62011-08-18 18:15:16 +000069 EQ(const Entry& entry, const GrBinHashKey<Entry, KeySize>& key) {
junov@google.comf93e7172011-03-31 21:26:24 +000070 GrAssert(key.fIsValid);
71 return 0 == entry.compare(key);
72 }
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000073
74 static bool
junov@google.comf7c00f62011-08-18 18:15:16 +000075 LT(const Entry& entry, const GrBinHashKey<Entry, KeySize>& key) {
junov@google.comf93e7172011-03-31 21:26:24 +000076 GrAssert(key.fIsValid);
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000077 return entry.compare(key) < 0;
junov@google.comf93e7172011-03-31 21:26:24 +000078 }
79
80 uint32_t getHash() const {
81 GrAssert(fIsValid);
junov@google.comf7c00f62011-08-18 18:15:16 +000082 return fHash;
junov@google.comf93e7172011-03-31 21:26:24 +000083 }
84
85private:
junov@google.comf7c00f62011-08-18 18:15:16 +000086 uint32_t fHash;
87 uint8_t fData[KeySize]; //Buffer for key storage
junov@google.comf93e7172011-03-31 21:26:24 +000088
89#if GR_DEBUG
90public:
91 bool fIsValid;
92#endif
93};
94
95#endif