3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #ifndef GrTHashTable_DEFINED
12 #define GrTHashTable_DEFINED
15 #include "SkTDArray.h"
19 * static bool Equals(const Entry&, const Key&);
20 * static bool LessThan(const Entry&, const Key&);
21 * static bool Equals(const Entry&, const Entry&); for SK_DEBUG if GrTHashTable::validate() is called
22 * static bool LessThan(const Entry&, const Entry&); for SK_DEBUG if GrTHashTable::validate() is called
23 * uint32_t getHash() const;
25 * Allows duplicate key entries but on find you may get
26 * any of the duplicate entries returned.
28 template <typename T, typename Key, size_t kHashBits> class GrTHashTable {
30 GrTHashTable() { this->clearHash(); }
33 int count() const { return fSorted.count(); }
36 // Return the first resource that matches the key.
37 bool operator()(const T*) const { return true; }
40 T* find(const Key& key) const { return this->find(key, Any()); }
41 template <typename Filter> T* find(const Key&, Filter filter) const;
43 // return true if key was unique when inserted.
44 bool insert(const Key&, T*);
45 void remove(const Key&, const T*);
50 void validate() const;
51 bool contains(T*) const;
55 const SkTDArray<T*>& getArray() const { return fSorted; }
56 SkTDArray<T*>& getArray() { return fSorted; }
58 void clearHash() { sk_bzero(fHash, sizeof(fHash)); }
61 kHashCount = 1 << kHashBits,
62 kHashMask = kHashCount - 1
64 static unsigned hash2Index(intptr_t hash) {
66 if (sizeof(hash) == 8) {
74 return hash & kHashMask;
77 mutable T* fHash[kHashCount];
78 SkTDArray<T*> fSorted;
80 // search fSorted, and return the found index, or ~index of where it
82 int searchArray(const Key&) const;
85 ///////////////////////////////////////////////////////////////////////////////
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();
91 // we should insert it at 0
95 const T* const* array = fSorted.begin();
99 int index = (low + high) >> 1;
100 if (Key::LessThan(*array[index], key)) {
107 // check if we found it
108 if (Key::Equals(*array[high], key)) {
109 // above search should have found the first occurrence if there
111 SkASSERT(0 == high || Key::LessThan(*array[high - 1], key));
115 // now return the ~ of where we should insert it
116 if (Key::LessThan(*array[high], key)) {
122 template <typename T, typename Key, size_t kHashBits>
123 template <typename Filter>
124 T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, Filter filter) const {
126 int hashIndex = hash2Index(key.getHash());
127 T* elem = fHash[hashIndex];
129 if (NULL != elem && Key::Equals(*elem, key) && filter(elem)) {
133 // bsearch for the key in our sorted array
134 int index = this->searchArray(key);
139 const T* const* array = fSorted.begin();
141 // above search should have found the first occurrence if there
143 SkASSERT(0 == index || Key::LessThan(*array[index - 1], key));
145 for ( ; index < count() && Key::Equals(*array[index], key); ++index) {
146 if (filter(fSorted[index])) {
148 fHash[hashIndex] = fSorted[index];
149 return fSorted[index];
156 template <typename T, typename Key, size_t kHashBits>
157 bool GrTHashTable<T, Key, kHashBits>::insert(const Key& key, T* elem) {
158 int index = this->searchArray(key);
159 bool first = index < 0;
161 // turn it into the actual index
164 // add it to our array
165 *fSorted.insert(index) = elem;
166 // update our hash table (overwrites any dupe's position in the hash)
167 fHash[hash2Index(key.getHash())] = elem;
171 template <typename T, typename Key, size_t kHashBits>
172 void GrTHashTable<T, Key, kHashBits>::remove(const Key& key, const T* elem) {
173 int index = hash2Index(key.getHash());
174 if (fHash[index] == elem) {
178 // remove from our sorted array
179 index = this->searchArray(key);
180 SkASSERT(index >= 0);
181 // if there are multiple matches searchArray will give us the first match
182 // march forward until we find elem.
183 while (elem != fSorted[index]) {
185 SkASSERT(index < fSorted.count());
187 SkASSERT(elem == fSorted[index]);
188 fSorted.remove(index);
191 template <typename T, typename Key, size_t kHashBits>
192 void GrTHashTable<T, Key, kHashBits>::deleteAll() {
198 template <typename T, typename Key, size_t kHashBits>
199 void GrTHashTable<T, Key, kHashBits>::validate() const {
200 int count = fSorted.count();
201 for (int i = 1; i < count; i++) {
202 SkASSERT(Key::LessThan(*fSorted[i - 1], *fSorted[i]) ||
203 Key::Equals(*fSorted[i - 1], *fSorted[i]));
207 template <typename T, typename Key, size_t kHashBits>
208 bool GrTHashTable<T, Key, kHashBits>::contains(T* elem) const {
209 int index = fSorted.find(elem);