blob: 83462c70c9e172889f03d6fb469078e2af2083db [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
17/**
18 * Key needs
rmistry@google.comd6bab022013-12-02 13:50:38 +000019 * 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
reed@google.comac10a2d2010-12-22 21:39:39 +000023 * uint32_t getHash() const;
24 *
25 * Allows duplicate key entries but on find you may get
26 * any of the duplicate entries returned.
27 */
28template <typename T, typename Key, size_t kHashBits> class GrTHashTable {
29public:
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000030 GrTHashTable() { this->clearHash(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000031 ~GrTHashTable() {}
32
33 int count() const { return fSorted.count(); }
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000034
35 struct Any {
36 // Return the first resource that matches the key.
37 bool operator()(const T*) const { return true; }
38 };
39
40 T* find(const Key& key) const { return this->find(key, Any()); }
41 template <typename Filter> T* find(const Key&, Filter filter) const;
42
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*);
reed@google.comac10a2d2010-12-22 21:39:39 +000046
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000047 void deleteAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000048
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000049#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +000050 void validate() const;
51 bool contains(T*) const;
52#endif
53
54 // testing
bsalomon@google.com21cbec42013-01-07 17:23:00 +000055 const SkTDArray<T*>& getArray() const { return fSorted; }
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000056 SkTDArray<T*>& getArray() { return fSorted; }
reed@google.comac10a2d2010-12-22 21:39:39 +000057private:
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +000058 void clearHash() { sk_bzero(fHash, sizeof(fHash)); }
59
reed@google.comac10a2d2010-12-22 21:39:39 +000060 enum {
61 kHashCount = 1 << kHashBits,
62 kHashMask = kHashCount - 1
63 };
64 static unsigned hash2Index(uint32_t hash) {
65 hash ^= hash >> 16;
66 if (kHashBits <= 8) {
67 hash ^= hash >> 8;
68 }
69 return hash & kHashMask;
70 }
71
72 mutable T* fHash[kHashCount];
bsalomon@google.com21cbec42013-01-07 17:23:00 +000073 SkTDArray<T*> fSorted;
reed@google.comac10a2d2010-12-22 21:39:39 +000074
75 // search fSorted, and return the found index, or ~index of where it
76 // should be inserted
77 int searchArray(const Key&) const;
78};
79
80///////////////////////////////////////////////////////////////////////////////
81
82template <typename T, typename Key, size_t kHashBits>
83int GrTHashTable<T, Key, kHashBits>::searchArray(const Key& key) const {
84 int count = fSorted.count();
85 if (0 == count) {
86 // we should insert it at 0
87 return ~0;
88 }
89
90 const T* const* array = fSorted.begin();
91 int high = count - 1;
92 int low = 0;
93 while (high > low) {
94 int index = (low + high) >> 1;
rmistry@google.comd6bab022013-12-02 13:50:38 +000095 if (Key::LessThan(*array[index], key)) {
reed@google.comac10a2d2010-12-22 21:39:39 +000096 low = index + 1;
97 } else {
98 high = index;
99 }
100 }
101
102 // check if we found it
rmistry@google.comd6bab022013-12-02 13:50:38 +0000103 if (Key::Equals(*array[high], key)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 // above search should have found the first occurrence if there
105 // are multiple.
rmistry@google.comd6bab022013-12-02 13:50:38 +0000106 SkASSERT(0 == high || Key::LessThan(*array[high - 1], key));
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 return high;
108 }
109
110 // now return the ~ of where we should insert it
rmistry@google.comd6bab022013-12-02 13:50:38 +0000111 if (Key::LessThan(*array[high], key)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 high += 1;
113 }
114 return ~high;
115}
116
117template <typename T, typename Key, size_t kHashBits>
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000118template <typename Filter>
119T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, Filter filter) const {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 int hashIndex = hash2Index(key.getHash());
122 T* elem = fHash[hashIndex];
123
rmistry@google.comd6bab022013-12-02 13:50:38 +0000124 if (NULL != elem && Key::Equals(*elem, key) && filter(elem)) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000125 return elem;
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000126 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000127
128 // bsearch for the key in our sorted array
129 int index = this->searchArray(key);
130 if (index < 0) {
131 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000133
134 const T* const* array = fSorted.begin();
135
136 // above search should have found the first occurrence if there
137 // are multiple.
rmistry@google.comd6bab022013-12-02 13:50:38 +0000138 SkASSERT(0 == index || Key::LessThan(*array[index - 1], key));
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000139
rmistry@google.comd6bab022013-12-02 13:50:38 +0000140 for ( ; index < count() && Key::Equals(*array[index], key); ++index) {
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000141 if (filter(fSorted[index])) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000142 // update the hash
143 fHash[hashIndex] = fSorted[index];
144 return fSorted[index];
145 }
146 }
147
148 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000149}
150
151template <typename T, typename Key, size_t kHashBits>
152bool GrTHashTable<T, Key, kHashBits>::insert(const Key& key, T* elem) {
153 int index = this->searchArray(key);
154 bool first = index < 0;
155 if (first) {
156 // turn it into the actual index
157 index = ~index;
158 }
159 // add it to our array
160 *fSorted.insert(index) = elem;
161 // update our hash table (overwrites any dupe's position in the hash)
162 fHash[hash2Index(key.getHash())] = elem;
163 return first;
164}
165
166template <typename T, typename Key, size_t kHashBits>
167void GrTHashTable<T, Key, kHashBits>::remove(const Key& key, const T* elem) {
168 int index = hash2Index(key.getHash());
169 if (fHash[index] == elem) {
170 fHash[index] = NULL;
171 }
172
173 // remove from our sorted array
174 index = this->searchArray(key);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000175 SkASSERT(index >= 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 // if there are multiple matches searchArray will give us the first match
177 // march forward until we find elem.
178 while (elem != fSorted[index]) {
179 ++index;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000180 SkASSERT(index < fSorted.count());
reed@google.comac10a2d2010-12-22 21:39:39 +0000181 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000182 SkASSERT(elem == fSorted[index]);
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 fSorted.remove(index);
184}
185
186template <typename T, typename Key, size_t kHashBits>
reed@google.comac10a2d2010-12-22 21:39:39 +0000187void GrTHashTable<T, Key, kHashBits>::deleteAll() {
188 fSorted.deleteAll();
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000189 this->clearHash();
reed@google.comac10a2d2010-12-22 21:39:39 +0000190}
191
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000192#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000193template <typename T, typename Key, size_t kHashBits>
194void GrTHashTable<T, Key, kHashBits>::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 int count = fSorted.count();
196 for (int i = 1; i < count; i++) {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000197 SkASSERT(Key::LessThan(*fSorted[i - 1], *fSorted[i]) ||
198 Key::Equals(*fSorted[i - 1], *fSorted[i]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 }
200}
201
202template <typename T, typename Key, size_t kHashBits>
203bool GrTHashTable<T, Key, kHashBits>::contains(T* elem) const {
204 int index = fSorted.find(elem);
205 return index >= 0;
206}
207
208#endif
209
210#endif