epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
mtklein@google.com | 4c2af74 | 2013-10-21 21:04:06 +0000 | [diff] [blame] | 11 | #ifndef GrTHashTable_DEFINED |
| 12 | #define GrTHashTable_DEFINED |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 21cbec4 | 2013-01-07 17:23:00 +0000 | [diff] [blame] | 14 | #include "GrTypes.h" |
| 15 | #include "SkTDArray.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 17 | // GrTDefaultFindFunctor implements the default find behavior for |
| 18 | // GrTHashTable (i.e., return the first resource that matches the |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 19 | // provided key) |
| 20 | template <typename T> class GrTDefaultFindFunctor { |
| 21 | public: |
| 22 | // always accept the first element examined |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 23 | bool operator()(const T*) const { return true; } |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Key needs |
| 28 | * static bool EQ(const Entry&, const HashKey&); |
| 29 | * static bool LT(const Entry&, const HashKey&); |
| 30 | * uint32_t getHash() const; |
| 31 | * |
| 32 | * Allows duplicate key entries but on find you may get |
| 33 | * any of the duplicate entries returned. |
| 34 | */ |
| 35 | template <typename T, typename Key, size_t kHashBits> class GrTHashTable { |
| 36 | public: |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 37 | GrTHashTable() { sk_bzero(fHash, sizeof(fHash)); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | ~GrTHashTable() {} |
| 39 | |
| 40 | int count() const { return fSorted.count(); } |
| 41 | T* find(const Key&) const; |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 42 | template <typename FindFuncType> T* find(const Key&, const FindFuncType&) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | // return true if key was unique when inserted. |
| 44 | bool insert(const Key&, T*); |
| 45 | void remove(const Key&, const T*); |
| 46 | T* removeAt(int index, uint32_t hash); |
| 47 | void removeAll(); |
| 48 | void deleteAll(); |
| 49 | void unrefAll(); |
| 50 | |
| 51 | /** |
| 52 | * Return the index for the element, using a linear search. |
| 53 | */ |
| 54 | int slowFindIndex(T* elem) const { return fSorted.find(elem); } |
| 55 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 56 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | void validate() const; |
| 58 | bool contains(T*) const; |
| 59 | #endif |
| 60 | |
| 61 | // testing |
bsalomon@google.com | 21cbec4 | 2013-01-07 17:23:00 +0000 | [diff] [blame] | 62 | const SkTDArray<T*>& getArray() const { return fSorted; } |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 63 | SkTDArray<T*>& getArray() { return fSorted; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | private: |
| 65 | enum { |
| 66 | kHashCount = 1 << kHashBits, |
| 67 | kHashMask = kHashCount - 1 |
| 68 | }; |
| 69 | static unsigned hash2Index(uint32_t hash) { |
| 70 | hash ^= hash >> 16; |
| 71 | if (kHashBits <= 8) { |
| 72 | hash ^= hash >> 8; |
| 73 | } |
| 74 | return hash & kHashMask; |
| 75 | } |
| 76 | |
| 77 | mutable T* fHash[kHashCount]; |
bsalomon@google.com | 21cbec4 | 2013-01-07 17:23:00 +0000 | [diff] [blame] | 78 | SkTDArray<T*> fSorted; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | |
| 80 | // search fSorted, and return the found index, or ~index of where it |
| 81 | // should be inserted |
| 82 | int searchArray(const Key&) const; |
| 83 | }; |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | |
| 87 | template <typename T, typename Key, size_t kHashBits> |
| 88 | int GrTHashTable<T, Key, kHashBits>::searchArray(const Key& key) const { |
| 89 | int count = fSorted.count(); |
| 90 | if (0 == count) { |
| 91 | // we should insert it at 0 |
| 92 | return ~0; |
| 93 | } |
| 94 | |
| 95 | const T* const* array = fSorted.begin(); |
| 96 | int high = count - 1; |
| 97 | int low = 0; |
| 98 | while (high > low) { |
| 99 | int index = (low + high) >> 1; |
| 100 | if (Key::LT(*array[index], key)) { |
| 101 | low = index + 1; |
| 102 | } else { |
| 103 | high = index; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // check if we found it |
| 108 | if (Key::EQ(*array[high], key)) { |
| 109 | // above search should have found the first occurrence if there |
| 110 | // are multiple. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 111 | SkASSERT(0 == high || Key::LT(*array[high - 1], key)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | return high; |
| 113 | } |
| 114 | |
| 115 | // now return the ~ of where we should insert it |
| 116 | if (Key::LT(*array[high], key)) { |
| 117 | high += 1; |
| 118 | } |
| 119 | return ~high; |
| 120 | } |
| 121 | |
| 122 | template <typename T, typename Key, size_t kHashBits> |
| 123 | T* GrTHashTable<T, Key, kHashBits>::find(const Key& key) const { |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 124 | GrTDefaultFindFunctor<T> find; |
| 125 | |
| 126 | return this->find(key, find); |
| 127 | } |
| 128 | |
| 129 | template <typename T, typename Key, size_t kHashBits> |
| 130 | template <typename FindFuncType> |
| 131 | T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, const FindFuncType& findFunc) const { |
| 132 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | int hashIndex = hash2Index(key.getHash()); |
| 134 | T* elem = fHash[hashIndex]; |
| 135 | |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 136 | if (NULL != elem && Key::EQ(*elem, key) && findFunc(elem)) { |
| 137 | return elem; |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 138 | } |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 139 | |
| 140 | // bsearch for the key in our sorted array |
| 141 | int index = this->searchArray(key); |
| 142 | if (index < 0) { |
| 143 | return NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | } |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 145 | |
| 146 | const T* const* array = fSorted.begin(); |
| 147 | |
| 148 | // above search should have found the first occurrence if there |
| 149 | // are multiple. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 150 | SkASSERT(0 == index || Key::LT(*array[index - 1], key)); |
robertphillips@google.com | 3b57ded | 2012-09-18 17:16:33 +0000 | [diff] [blame] | 151 | |
| 152 | for ( ; index < count() && Key::EQ(*array[index], key); ++index) { |
| 153 | if (findFunc(fSorted[index])) { |
| 154 | // update the hash |
| 155 | fHash[hashIndex] = fSorted[index]; |
| 156 | return fSorted[index]; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | template <typename T, typename Key, size_t kHashBits> |
| 164 | bool GrTHashTable<T, Key, kHashBits>::insert(const Key& key, T* elem) { |
| 165 | int index = this->searchArray(key); |
| 166 | bool first = index < 0; |
| 167 | if (first) { |
| 168 | // turn it into the actual index |
| 169 | index = ~index; |
| 170 | } |
| 171 | // add it to our array |
| 172 | *fSorted.insert(index) = elem; |
| 173 | // update our hash table (overwrites any dupe's position in the hash) |
| 174 | fHash[hash2Index(key.getHash())] = elem; |
| 175 | return first; |
| 176 | } |
| 177 | |
| 178 | template <typename T, typename Key, size_t kHashBits> |
| 179 | void GrTHashTable<T, Key, kHashBits>::remove(const Key& key, const T* elem) { |
| 180 | int index = hash2Index(key.getHash()); |
| 181 | if (fHash[index] == elem) { |
| 182 | fHash[index] = NULL; |
| 183 | } |
| 184 | |
| 185 | // remove from our sorted array |
| 186 | index = this->searchArray(key); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 187 | SkASSERT(index >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | // if there are multiple matches searchArray will give us the first match |
| 189 | // march forward until we find elem. |
| 190 | while (elem != fSorted[index]) { |
| 191 | ++index; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 192 | SkASSERT(index < fSorted.count()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 194 | SkASSERT(elem == fSorted[index]); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 195 | fSorted.remove(index); |
| 196 | } |
| 197 | |
| 198 | template <typename T, typename Key, size_t kHashBits> |
| 199 | T* GrTHashTable<T, Key, kHashBits>::removeAt(int elemIndex, uint32_t hash) { |
| 200 | int hashIndex = hash2Index(hash); |
| 201 | if (fHash[hashIndex] == fSorted[elemIndex]) { |
| 202 | fHash[hashIndex] = NULL; |
| 203 | } |
| 204 | // remove from our sorted array |
| 205 | T* elem = fSorted[elemIndex]; |
| 206 | fSorted.remove(elemIndex); |
| 207 | return elem; |
| 208 | } |
| 209 | |
| 210 | template <typename T, typename Key, size_t kHashBits> |
| 211 | void GrTHashTable<T, Key, kHashBits>::removeAll() { |
| 212 | fSorted.reset(); |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 213 | sk_bzero(fHash, sizeof(fHash)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | template <typename T, typename Key, size_t kHashBits> |
| 217 | void GrTHashTable<T, Key, kHashBits>::deleteAll() { |
| 218 | fSorted.deleteAll(); |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 219 | sk_bzero(fHash, sizeof(fHash)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | template <typename T, typename Key, size_t kHashBits> |
| 223 | void GrTHashTable<T, Key, kHashBits>::unrefAll() { |
| 224 | fSorted.unrefAll(); |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 225 | sk_bzero(fHash, sizeof(fHash)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | } |
| 227 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 228 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | template <typename T, typename Key, size_t kHashBits> |
| 230 | void GrTHashTable<T, Key, kHashBits>::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | int count = fSorted.count(); |
| 232 | for (int i = 1; i < count; i++) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 233 | SkASSERT(Key::LT(*fSorted[i - 1], *fSorted[i]) || |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 234 | Key::EQ(*fSorted[i - 1], *fSorted[i])); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | template <typename T, typename Key, size_t kHashBits> |
| 239 | bool GrTHashTable<T, Key, kHashBits>::contains(T* elem) const { |
| 240 | int index = fSorted.find(elem); |
| 241 | return index >= 0; |
| 242 | } |
| 243 | |
| 244 | #endif |
| 245 | |
| 246 | #endif |