2011-03-31 21:26:24 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2011-03-31 21:26:24 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2011-03-31 21:26:24 +00:00
|
|
|
#ifndef GrBinHashKey_DEFINED
|
|
|
|
#define GrBinHashKey_DEFINED
|
|
|
|
|
|
|
|
#include "GrTypes.h"
|
|
|
|
|
|
|
|
/**
|
2012-07-16 17:36:28 +00:00
|
|
|
* Hash function class that can take a data chunk of any predetermined length. The hash function
|
|
|
|
* used is the One-at-a-Time Hash (http://burtleburtle.net/bob/hash/doobs.html).
|
|
|
|
*
|
2012-12-20 15:13:01 +00:00
|
|
|
* Keys are computed from ENTRY objects. ENTRY must be fully ordered by a member:
|
|
|
|
* int compare(const GrTBinHashKey<ENTRY, ..>& k);
|
|
|
|
* which returns negative if the ENTRY < k, 0 if it equals k, and positive if k < the ENTRY.
|
|
|
|
* Additionally, ENTRY must be flattenable into the key using setKeyData.
|
2012-07-16 17:36:28 +00:00
|
|
|
*
|
|
|
|
* This class satisfies the requirements to be a key for a GrTHashTable.
|
2011-03-31 21:26:24 +00:00
|
|
|
*/
|
2012-12-20 15:13:01 +00:00
|
|
|
template<typename ENTRY, size_t KEY_SIZE>
|
2012-07-16 17:36:28 +00:00
|
|
|
class GrTBinHashKey {
|
2011-03-31 21:26:24 +00:00
|
|
|
public:
|
2012-12-20 15:13:01 +00:00
|
|
|
enum { kKeySize = KEY_SIZE };
|
|
|
|
|
2012-07-16 17:36:28 +00:00
|
|
|
GrTBinHashKey() {
|
|
|
|
this->reset();
|
|
|
|
}
|
2011-03-31 21:26:24 +00:00
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
GrTBinHashKey(const GrTBinHashKey<ENTRY, KEY_SIZE>& other) {
|
2011-08-18 18:15:16 +00:00
|
|
|
*this = other;
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
2012-07-16 17:36:28 +00:00
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
GrTBinHashKey<ENTRY, KEY_SIZE>& operator=(const GrTBinHashKey<ENTRY, KEY_SIZE>& other) {
|
2011-08-18 18:15:16 +00:00
|
|
|
memcpy(this, &other, sizeof(*this));
|
|
|
|
return *this;
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 17:36:28 +00:00
|
|
|
~GrTBinHashKey() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
fHash = 0;
|
|
|
|
#if GR_DEBUG
|
|
|
|
fIsValid = false;
|
|
|
|
#endif
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 22:35:18 +00:00
|
|
|
void setKeyData(const uint32_t* SK_RESTRICT data) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(GrIsALIGN4(KEY_SIZE));
|
2012-12-20 15:13:01 +00:00
|
|
|
memcpy(&fData, data, KEY_SIZE);
|
2011-08-18 18:15:16 +00:00
|
|
|
|
2011-12-12 22:35:18 +00:00
|
|
|
uint32_t hash = 0;
|
2012-12-20 15:13:01 +00:00
|
|
|
size_t len = KEY_SIZE;
|
2011-08-18 18:15:16 +00:00
|
|
|
while (len >= 4) {
|
2011-12-12 22:35:18 +00:00
|
|
|
hash += *data++;
|
|
|
|
hash += (fHash << 10);
|
|
|
|
hash ^= (hash >> 6);
|
2011-08-18 18:15:16 +00:00
|
|
|
len -= 4;
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
2011-12-12 22:35:18 +00:00
|
|
|
hash += (fHash << 3);
|
|
|
|
hash ^= (fHash >> 11);
|
|
|
|
hash += (fHash << 15);
|
2011-08-18 18:15:16 +00:00
|
|
|
#if GR_DEBUG
|
|
|
|
fIsValid = true;
|
|
|
|
#endif
|
2011-12-12 22:35:18 +00:00
|
|
|
fHash = hash;
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
int compare(const GrTBinHashKey<ENTRY, KEY_SIZE>& key) const {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(fIsValid && key.fIsValid);
|
2012-12-20 15:13:01 +00:00
|
|
|
return memcmp(fData, key.fData, KEY_SIZE);
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
static bool EQ(const ENTRY& entry, const GrTBinHashKey<ENTRY, KEY_SIZE>& key) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(key.fIsValid);
|
2011-03-31 21:26:24 +00:00
|
|
|
return 0 == entry.compare(key);
|
|
|
|
}
|
2011-06-01 19:27:31 +00:00
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
static bool LT(const ENTRY& entry, const GrTBinHashKey<ENTRY, KEY_SIZE>& key) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(key.fIsValid);
|
2011-06-01 19:27:31 +00:00
|
|
|
return entry.compare(key) < 0;
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getHash() const {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(fIsValid);
|
2011-08-18 18:15:16 +00:00
|
|
|
return fHash;
|
2011-03-31 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 15:13:01 +00:00
|
|
|
const uint8_t* getData() const {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(fIsValid);
|
2012-12-20 15:13:01 +00:00
|
|
|
return fData;
|
|
|
|
}
|
|
|
|
|
2011-03-31 21:26:24 +00:00
|
|
|
private:
|
2011-08-18 18:15:16 +00:00
|
|
|
uint32_t fHash;
|
2012-12-20 15:13:01 +00:00
|
|
|
uint8_t fData[KEY_SIZE]; // Buffer for key storage
|
2011-03-31 21:26:24 +00:00
|
|
|
|
|
|
|
#if GR_DEBUG
|
|
|
|
public:
|
|
|
|
bool fIsValid;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|