blob: 803a6802a71fd6eca3a2f098d82d5ab0a658fa94 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
mtklein@google.com4c2af742013-10-21 21:04:06 +000011#ifndef GrTHashTable_DEFINED
12#define GrTHashTable_DEFINED
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon@google.com21cbec42013-01-07 17:23:00 +000014#include "GrTypes.h"
15#include "SkTDArray.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000017// GrTDefaultFindFunctor implements the default find behavior for
18// GrTHashTable (i.e., return the first resource that matches the
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000019// provided key)
20template <typename T> class GrTDefaultFindFunctor {
21public:
22 // always accept the first element examined
sugoi@google.come0e385c2013-03-11 18:50:03 +000023 bool operator()(const T*) const { return true; }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000024};
25
reed@google.comac10a2d2010-12-22 21:39:39 +000026/**
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 */
35template <typename T, typename Key, size_t kHashBits> class GrTHashTable {
36public:
reed@google.com939ca7c2013-09-26 19:56:51 +000037 GrTHashTable() { sk_bzero(fHash, sizeof(fHash)); }
reed@google.comac10a2d2010-12-22 21:39:39 +000038 ~GrTHashTable() {}
39
40 int count() const { return fSorted.count(); }
41 T* find(const Key&) const;
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000042 template <typename FindFuncType> T* find(const Key&, const FindFuncType&) const;
reed@google.comac10a2d2010-12-22 21:39:39 +000043 // 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.org515dcd32013-08-28 14:17:03 +000056#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +000057 void validate() const;
58 bool contains(T*) const;
59#endif
60
61 // testing
bsalomon@google.com21cbec42013-01-07 17:23:00 +000062 const SkTDArray<T*>& getArray() const { return fSorted; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000063 SkTDArray<T*>& getArray() { return fSorted; }
reed@google.comac10a2d2010-12-22 21:39:39 +000064private:
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.com21cbec42013-01-07 17:23:00 +000078 SkTDArray<T*> fSorted;
reed@google.comac10a2d2010-12-22 21:39:39 +000079
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
87template <typename T, typename Key, size_t kHashBits>
88int 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.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(0 == high || Key::LT(*array[high - 1], key));
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 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
122template <typename T, typename Key, size_t kHashBits>
123T* GrTHashTable<T, Key, kHashBits>::find(const Key& key) const {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000124 GrTDefaultFindFunctor<T> find;
125
126 return this->find(key, find);
127}
128
129template <typename T, typename Key, size_t kHashBits>
130template <typename FindFuncType>
131T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, const FindFuncType& findFunc) const {
132
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 int hashIndex = hash2Index(key.getHash());
134 T* elem = fHash[hashIndex];
135
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000136 if (NULL != elem && Key::EQ(*elem, key) && findFunc(elem)) {
137 return elem;
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000138 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000139
140 // bsearch for the key in our sorted array
141 int index = this->searchArray(key);
142 if (index < 0) {
143 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000145
146 const T* const* array = fSorted.begin();
147
148 // above search should have found the first occurrence if there
149 // are multiple.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000150 SkASSERT(0 == index || Key::LT(*array[index - 1], key));
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000151
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.comac10a2d2010-12-22 21:39:39 +0000161}
162
163template <typename T, typename Key, size_t kHashBits>
164bool 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
178template <typename T, typename Key, size_t kHashBits>
179void 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.orgf6de4752013-08-17 00:02:59 +0000187 SkASSERT(index >= 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 // 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.orgf6de4752013-08-17 00:02:59 +0000192 SkASSERT(index < fSorted.count());
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000194 SkASSERT(elem == fSorted[index]);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 fSorted.remove(index);
196}
197
198template <typename T, typename Key, size_t kHashBits>
199T* 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
210template <typename T, typename Key, size_t kHashBits>
211void GrTHashTable<T, Key, kHashBits>::removeAll() {
212 fSorted.reset();
reed@google.com939ca7c2013-09-26 19:56:51 +0000213 sk_bzero(fHash, sizeof(fHash));
reed@google.comac10a2d2010-12-22 21:39:39 +0000214}
215
216template <typename T, typename Key, size_t kHashBits>
217void GrTHashTable<T, Key, kHashBits>::deleteAll() {
218 fSorted.deleteAll();
reed@google.com939ca7c2013-09-26 19:56:51 +0000219 sk_bzero(fHash, sizeof(fHash));
reed@google.comac10a2d2010-12-22 21:39:39 +0000220}
221
222template <typename T, typename Key, size_t kHashBits>
223void GrTHashTable<T, Key, kHashBits>::unrefAll() {
224 fSorted.unrefAll();
reed@google.com939ca7c2013-09-26 19:56:51 +0000225 sk_bzero(fHash, sizeof(fHash));
reed@google.comac10a2d2010-12-22 21:39:39 +0000226}
227
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000228#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000229template <typename T, typename Key, size_t kHashBits>
230void GrTHashTable<T, Key, kHashBits>::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 int count = fSorted.count();
232 for (int i = 1; i < count; i++) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000233 SkASSERT(Key::LT(*fSorted[i - 1], *fSorted[i]) ||
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 Key::EQ(*fSorted[i - 1], *fSorted[i]));
235 }
236}
237
238template <typename T, typename Key, size_t kHashBits>
239bool 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