OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 | 10 |
11 #ifndef GrTHashTable_DEFINED | 11 #ifndef GrTHashTable_DEFINED |
12 #define GrTHashTable_DEFINED | 12 #define GrTHashTable_DEFINED |
13 | 13 |
14 #include "GrTypes.h" | 14 #include "GrTypes.h" |
15 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
16 | 16 |
17 /** | 17 /** |
18 * Key needs | 18 * Key needs |
19 * static bool EQ(const Entry&, const HashKey&); | 19 * static bool Equals(const Entry&, const Key&); |
20 * static bool LT(const Entry&, const HashKey&); | 20 * static bool LessThan(const Entry&, const Key&); |
| 21 * static bool Equals(const Entry&, const Entry&); for SK_DEBUG if GrTHashT
able::validate() is called |
| 22 * static bool LessThan(const Entry&, const Entry&); for SK_DEBUG if GrTHas
hTable::validate() is called |
21 * uint32_t getHash() const; | 23 * uint32_t getHash() const; |
22 * | 24 * |
23 * Allows duplicate key entries but on find you may get | 25 * Allows duplicate key entries but on find you may get |
24 * any of the duplicate entries returned. | 26 * any of the duplicate entries returned. |
25 */ | 27 */ |
26 template <typename T, typename Key, size_t kHashBits> class GrTHashTable { | 28 template <typename T, typename Key, size_t kHashBits> class GrTHashTable { |
27 public: | 29 public: |
28 GrTHashTable() { this->clearHash(); } | 30 GrTHashTable() { this->clearHash(); } |
29 ~GrTHashTable() {} | 31 ~GrTHashTable() {} |
30 | 32 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 if (0 == count) { | 85 if (0 == count) { |
84 // we should insert it at 0 | 86 // we should insert it at 0 |
85 return ~0; | 87 return ~0; |
86 } | 88 } |
87 | 89 |
88 const T* const* array = fSorted.begin(); | 90 const T* const* array = fSorted.begin(); |
89 int high = count - 1; | 91 int high = count - 1; |
90 int low = 0; | 92 int low = 0; |
91 while (high > low) { | 93 while (high > low) { |
92 int index = (low + high) >> 1; | 94 int index = (low + high) >> 1; |
93 if (Key::LT(*array[index], key)) { | 95 if (Key::LessThan(*array[index], key)) { |
94 low = index + 1; | 96 low = index + 1; |
95 } else { | 97 } else { |
96 high = index; | 98 high = index; |
97 } | 99 } |
98 } | 100 } |
99 | 101 |
100 // check if we found it | 102 // check if we found it |
101 if (Key::EQ(*array[high], key)) { | 103 if (Key::Equals(*array[high], key)) { |
102 // above search should have found the first occurrence if there | 104 // above search should have found the first occurrence if there |
103 // are multiple. | 105 // are multiple. |
104 SkASSERT(0 == high || Key::LT(*array[high - 1], key)); | 106 SkASSERT(0 == high || Key::LessThan(*array[high - 1], key)); |
105 return high; | 107 return high; |
106 } | 108 } |
107 | 109 |
108 // now return the ~ of where we should insert it | 110 // now return the ~ of where we should insert it |
109 if (Key::LT(*array[high], key)) { | 111 if (Key::LessThan(*array[high], key)) { |
110 high += 1; | 112 high += 1; |
111 } | 113 } |
112 return ~high; | 114 return ~high; |
113 } | 115 } |
114 | 116 |
115 template <typename T, typename Key, size_t kHashBits> | 117 template <typename T, typename Key, size_t kHashBits> |
116 template <typename Filter> | 118 template <typename Filter> |
117 T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, Filter filter) const { | 119 T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, Filter filter) const { |
118 | 120 |
119 int hashIndex = hash2Index(key.getHash()); | 121 int hashIndex = hash2Index(key.getHash()); |
120 T* elem = fHash[hashIndex]; | 122 T* elem = fHash[hashIndex]; |
121 | 123 |
122 if (NULL != elem && Key::EQ(*elem, key) && filter(elem)) { | 124 if (NULL != elem && Key::Equals(*elem, key) && filter(elem)) { |
123 return elem; | 125 return elem; |
124 } | 126 } |
125 | 127 |
126 // bsearch for the key in our sorted array | 128 // bsearch for the key in our sorted array |
127 int index = this->searchArray(key); | 129 int index = this->searchArray(key); |
128 if (index < 0) { | 130 if (index < 0) { |
129 return NULL; | 131 return NULL; |
130 } | 132 } |
131 | 133 |
132 const T* const* array = fSorted.begin(); | 134 const T* const* array = fSorted.begin(); |
133 | 135 |
134 // above search should have found the first occurrence if there | 136 // above search should have found the first occurrence if there |
135 // are multiple. | 137 // are multiple. |
136 SkASSERT(0 == index || Key::LT(*array[index - 1], key)); | 138 SkASSERT(0 == index || Key::LessThan(*array[index - 1], key)); |
137 | 139 |
138 for ( ; index < count() && Key::EQ(*array[index], key); ++index) { | 140 for ( ; index < count() && Key::Equals(*array[index], key); ++index) { |
139 if (filter(fSorted[index])) { | 141 if (filter(fSorted[index])) { |
140 // update the hash | 142 // update the hash |
141 fHash[hashIndex] = fSorted[index]; | 143 fHash[hashIndex] = fSorted[index]; |
142 return fSorted[index]; | 144 return fSorted[index]; |
143 } | 145 } |
144 } | 146 } |
145 | 147 |
146 return NULL; | 148 return NULL; |
147 } | 149 } |
148 | 150 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 void GrTHashTable<T, Key, kHashBits>::deleteAll() { | 187 void GrTHashTable<T, Key, kHashBits>::deleteAll() { |
186 fSorted.deleteAll(); | 188 fSorted.deleteAll(); |
187 this->clearHash(); | 189 this->clearHash(); |
188 } | 190 } |
189 | 191 |
190 #ifdef SK_DEBUG | 192 #ifdef SK_DEBUG |
191 template <typename T, typename Key, size_t kHashBits> | 193 template <typename T, typename Key, size_t kHashBits> |
192 void GrTHashTable<T, Key, kHashBits>::validate() const { | 194 void GrTHashTable<T, Key, kHashBits>::validate() const { |
193 int count = fSorted.count(); | 195 int count = fSorted.count(); |
194 for (int i = 1; i < count; i++) { | 196 for (int i = 1; i < count; i++) { |
195 SkASSERT(Key::LT(*fSorted[i - 1], *fSorted[i]) || | 197 SkASSERT(Key::LessThan(*fSorted[i - 1], *fSorted[i]) || |
196 Key::EQ(*fSorted[i - 1], *fSorted[i])); | 198 Key::Equals(*fSorted[i - 1], *fSorted[i])); |
197 } | 199 } |
198 } | 200 } |
199 | 201 |
200 template <typename T, typename Key, size_t kHashBits> | 202 template <typename T, typename Key, size_t kHashBits> |
201 bool GrTHashTable<T, Key, kHashBits>::contains(T* elem) const { | 203 bool GrTHashTable<T, Key, kHashBits>::contains(T* elem) const { |
202 int index = fSorted.find(elem); | 204 int index = fSorted.find(elem); |
203 return index >= 0; | 205 return index >= 0; |
204 } | 206 } |
205 | 207 |
206 #endif | 208 #endif |
207 | 209 |
208 #endif | 210 #endif |
OLD | NEW |