epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * 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.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #ifndef GrBinHashKey_DEFINED |
| 11 | #define GrBinHashKey_DEFINED |
| 12 | |
| 13 | #include "GrTypes.h" |
| 14 | |
| 15 | /** |
| 16 | * Abstract base class that presents the building interface of GrBinHashKey. |
| 17 | * This base class allows builder methods to not know the exact template |
| 18 | * parameters of GrBinHashKey |
| 19 | */ |
| 20 | class GrBinHashKeyBuilder { |
| 21 | public: |
| 22 | GrBinHashKeyBuilder() {} |
| 23 | virtual ~GrBinHashKeyBuilder() {} |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 24 | virtual void keyData(const uint32_t *dataToAdd, size_t len) = 0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * Hash function class than can take a data stream of indeterminate length. |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 29 | * It also has the ability to recieve data in several chunks (steamed). The |
| 30 | * hash function used is the One-at-a-Time Hash |
| 31 | * (http://burtleburtle.net/bob/hash/doobs.html). |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 32 | * |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 33 | * Keys are built in two passes the first pass builds the key until the |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 34 | * allocated storage for the key runs out, raw data accumulation stops, but |
| 35 | * the calculation of the 32-bit hash value and total key length continue. |
| 36 | * The second pass is only necessary if storage ran-out during the first pass. |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 37 | * If that is the case, the heap storage portion of the key will be |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 38 | * re-allocated so that the entire key can be stored in the second pass. |
| 39 | * |
| 40 | * Code for building a key: |
| 41 | * |
| 42 | * GrBinHashKey<MyEntryStruct, MyStackSize> MyKey; |
| 43 | * while( MyKey->doPass() ) |
| 44 | * { |
| 45 | * MyObject->buildKey(&MyKey); //invoke a builder method |
| 46 | * } |
| 47 | * |
| 48 | * All the builder method needs to do is make calls to the keyData method to |
| 49 | * append binary data to the key. |
| 50 | */ |
| 51 | template<typename Entry, size_t StackSize> |
| 52 | class GrBinHashKey : public GrBinHashKeyBuilder { |
| 53 | public: |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 54 | GrBinHashKey() |
| 55 | : fA(0) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | , fLength(0) |
| 57 | , fHeapData(NULL) |
| 58 | , fPhysicalSize(StackSize) |
| 59 | , fUseHeap(false) |
| 60 | , fPass(0) |
| 61 | #if GR_DEBUG |
| 62 | , fIsValid(true) |
| 63 | #endif |
| 64 | {} |
| 65 | |
| 66 | private: |
| 67 | // Illegal: must choose explicitly between copyAndTakeOwnership |
| 68 | // and deepCopyFrom. |
| 69 | // Not inheriting GrNoncopyable, because it causes very obscure compiler |
| 70 | // errors with template classes, which are hard to trace back to the use |
| 71 | // of assignment. |
| 72 | GrBinHashKey(const GrBinHashKey<Entry, StackSize>&) {} |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 73 | GrBinHashKey<Entry, StackSize>& operator=(const GrBinHashKey<Entry, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 74 | StackSize>&) { |
| 75 | return this; |
| 76 | } |
| 77 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 78 | public: |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 79 | void copyAndTakeOwnership(GrBinHashKey<Entry, StackSize>& key) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 80 | GrAssert(key.fIsValid); |
junov@google.com | 5d6e108 | 2011-05-25 20:26:11 +0000 | [diff] [blame] | 81 | copyFields(key); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 82 | if (fUseHeap) { |
| 83 | key.fHeapData = NULL; // ownership transfer |
| 84 | } |
| 85 | // Consistency Checking |
| 86 | // To avoid the overhead of copying or ref-counting the dynamically |
| 87 | // allocated portion of the key, we use ownership transfer |
| 88 | // Key usability is only tracked in debug builds. |
| 89 | GR_DEBUGCODE(key.fIsValid = false;) |
| 90 | } |
| 91 | |
| 92 | void deepCopyFrom(const GrBinHashKey<Entry, StackSize>& key) { |
| 93 | GrAssert(key.fIsValid); |
junov@google.com | 5d6e108 | 2011-05-25 20:26:11 +0000 | [diff] [blame] | 94 | copyFields(key); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 95 | if (fUseHeap) { |
| 96 | fHeapData = reinterpret_cast<uint8_t*>( |
| 97 | GrMalloc(sizeof(uint8_t) * fPhysicalSize)); |
| 98 | memcpy(fHeapData, key.fHeapData, fLength); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | virtual ~GrBinHashKey() { |
| 103 | if (fUseHeap) { |
| 104 | GrFree(fHeapData); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | bool doPass() { |
| 109 | GrAssert(fIsValid); |
| 110 | if (0 == fPass) { |
| 111 | fPass++; |
| 112 | return true; |
| 113 | } |
| 114 | if (1 == fPass) { |
| 115 | bool passNeeded = false; |
| 116 | if (fLength > fPhysicalSize) { |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 117 | // If the first pass ran out of space the we need to |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 118 | // re-allocate and perform a second pass |
| 119 | GrFree(fHeapData); |
| 120 | fHeapData = reinterpret_cast<uint8_t*>( |
| 121 | GrMalloc(sizeof(uint8_t) * fLength)); |
| 122 | fPhysicalSize = fLength; |
| 123 | fUseHeap = true; |
| 124 | passNeeded = true; |
| 125 | fLength = 0; |
| 126 | } |
| 127 | fPass++; |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 128 | return passNeeded; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 133 | void keyData(const uint32_t *dataToAdd, size_t len) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 134 | GrAssert(fIsValid); |
| 135 | GrAssert(fPass); |
tomhudson@google.com | 78e7d2c | 2011-06-01 20:43:05 +0000 | [diff] [blame] | 136 | GrAssert(GrIsALIGN4(len)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 137 | if (fUseHeap) { |
| 138 | GrAssert(fHeapData); |
| 139 | GrAssert(fLength + len <= fPhysicalSize); |
| 140 | memcpy(&fHeapData[fLength], dataToAdd, len ); |
| 141 | } else { |
| 142 | if (fLength + len <= StackSize) { |
| 143 | memcpy(&fStackData[fLength], dataToAdd, len); |
| 144 | } else { |
| 145 | GrAssert(1 == fPass); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fLength += len; |
| 150 | |
| 151 | if (1 == fPass) { |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 152 | uint32_t a = fA; |
| 153 | while (len >= 4) { |
| 154 | a += *dataToAdd++; |
| 155 | a += (a << 10); |
| 156 | a ^= (a >> 6); |
| 157 | len -= 4; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 158 | } |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 159 | a += (a << 3); |
| 160 | a ^= (a >> 11); |
| 161 | a += (a << 15); |
| 162 | |
| 163 | fA = a; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | int compare(const GrBinHashKey<Entry, StackSize>& key) const { |
| 168 | GrAssert(fIsValid); |
| 169 | if (fLength == key.fLength) { |
| 170 | GrAssert(fUseHeap == key.fUseHeap); |
| 171 | if(fUseHeap) { |
| 172 | return memcmp(fHeapData, key.fHeapData, fLength); |
| 173 | } else { |
| 174 | return memcmp(fStackData, key.fStackData, fLength); |
| 175 | } |
| 176 | } |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 177 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 178 | return (fLength - key.fLength); |
| 179 | } |
| 180 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 181 | static bool |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 182 | EQ(const Entry& entry, const GrBinHashKey<Entry, StackSize>& key) { |
| 183 | GrAssert(key.fIsValid); |
| 184 | return 0 == entry.compare(key); |
| 185 | } |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 186 | |
| 187 | static bool |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 188 | LT(const Entry& entry, const GrBinHashKey<Entry, StackSize>& key) { |
| 189 | GrAssert(key.fIsValid); |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 190 | return entry.compare(key) < 0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | uint32_t getHash() const { |
| 194 | GrAssert(fIsValid); |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 195 | return fA; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private: |
junov@google.com | 5d6e108 | 2011-05-25 20:26:11 +0000 | [diff] [blame] | 199 | void copyFields(const GrBinHashKey<Entry, StackSize>& src) { |
| 200 | if (fUseHeap) { |
| 201 | GrFree(fHeapData); |
| 202 | } |
| 203 | // We do a field-by-field copy because this is a non-POD |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 204 | // class, and therefore memcpy would be bad |
junov@google.com | 5d6e108 | 2011-05-25 20:26:11 +0000 | [diff] [blame] | 205 | fA = src.fA; |
junov@google.com | 5d6e108 | 2011-05-25 20:26:11 +0000 | [diff] [blame] | 206 | fLength = src.fLength; |
| 207 | memcpy(fStackData, src.fStackData, StackSize); |
| 208 | fHeapData = src.fHeapData; |
| 209 | fPhysicalSize = src.fPhysicalSize; |
| 210 | fUseHeap = src.fUseHeap; |
| 211 | fPass = src.fPass; |
| 212 | } |
| 213 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 214 | uint32_t fA; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 215 | |
| 216 | // For accumulating the variable length binary key |
| 217 | size_t fLength; // length of data accumulated so far |
| 218 | uint8_t fStackData[StackSize]; //Buffer for key storage |
| 219 | uint8_t* fHeapData; //Dynamically allocated extended key storage |
| 220 | size_t fPhysicalSize; //Total size available for key storage |
| 221 | bool fUseHeap; //Using a dynamically allocated key storage |
| 222 | int fPass; //Key generation pass counter |
| 223 | |
| 224 | #if GR_DEBUG |
| 225 | public: |
| 226 | bool fIsValid; |
| 227 | #endif |
| 228 | }; |
| 229 | |
| 230 | #endif |