OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTDynamicHash_DEFINED | 8 #ifndef SkTDynamicHash_DEFINED |
9 #define SkTDynamicHash_DEFINED | 9 #define SkTDynamicHash_DEFINED |
10 | 10 |
11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
12 #include "SkMath.h" | 12 #include "SkMath.h" |
13 | 13 |
14 /** SkTDynamicHash is a set of pointers of T. The elements can be looked up from the set by | |
15 * key Key. Duplicate keys are allowed. | |
16 */ | |
14 template <typename T, | 17 template <typename T, |
15 typename Key, | 18 typename Key, |
16 const Key& (GetKey)(const T&), | 19 const Key& (GetKey)(const T&), |
17 uint32_t (Hash)(const Key&), | 20 uint32_t (Hash)(const Key&), |
18 bool (Equal)(const T&, const Key&), | 21 bool (Equal)(const T&, const Key&), |
19 int kGrowPercent = 75, // Larger -> more memory efficient, but slow er. | 22 int kGrowPercent = 75, // Larger -> more memory efficient, but slow er. |
20 int kShrinkPercent = 25> | 23 int kShrinkPercent = 25> |
21 class SkTDynamicHash { | 24 class SkTDynamicHash { |
22 static const int kMinCapacity = 4; // Smallest capacity we allow. | 25 static const int kMinCapacity = 4; // Smallest capacity we allow. |
23 public: | 26 public: |
24 SkTDynamicHash(int initialCapacity=64/sizeof(T*)) { | 27 SkTDynamicHash(int initialCapacity=64/sizeof(T*)) { |
25 this->reset(SkNextPow2(initialCapacity > kMinCapacity ? initialCapacity : kMinCapacity)); | 28 this->reset(SkNextPow2(initialCapacity > kMinCapacity ? initialCapacity : kMinCapacity)); |
26 SkASSERT(this->validate()); | |
mtklein
2013/11/27 14:21:54
Please put validate back to how it was. SkASSERT
Kimmo Kinnunen
2013/11/28 06:40:08
The commit msg actually said that when it is *run*
| |
27 } | 29 } |
28 | 30 |
29 ~SkTDynamicHash() { | 31 ~SkTDynamicHash() { |
30 sk_free(fArray); | 32 sk_free(fArray); |
31 } | 33 } |
32 | 34 |
33 int count() const { return fCount; } | 35 int count() const { return fCount; } |
34 | 36 |
35 // Return the entry with this key if we have it, otherwise NULL. | 37 struct Any { |
38 // Return the first entry that matches the key. | |
39 bool operator()(const T*) const { return true; } | |
mtklein
2013/11/27 14:21:54
I'm not a fan of this API. I know it's replicatin
Kimmo Kinnunen
2013/11/28 06:40:08
Yeah.. Though, in this stage of this usage, I don'
| |
40 }; | |
41 | |
42 // Return an entry with passed key if the collection has it, otherwise NULL. | |
36 T* find(const Key& key) const { | 43 T* find(const Key& key) const { |
44 return this->find(key, Any()); | |
45 } | |
46 | |
47 // Return an entry with the passed key if the collection has it and if filte r returns true for | |
48 // the entry. Otherwise return NULL. | |
49 template <typename Filter> | |
50 T* find(const Key& key, Filter filter) const { | |
37 int index = this->firstIndex(key); | 51 int index = this->firstIndex(key); |
38 for (int round = 0; round < fCapacity; round++) { | 52 for (int round = 0; round < fCapacity; round++) { |
39 T* candidate = fArray[index]; | 53 T* candidate = fArray[index]; |
40 if (Empty() == candidate) { | 54 if (Empty() == candidate) { |
41 return NULL; | 55 return NULL; |
42 } | 56 } |
43 if (Deleted() != candidate && Equal(*candidate, key)) { | 57 if (Deleted() != candidate && Equal(*candidate, key) && filter(fArra y[index])) { |
44 return candidate; | 58 return candidate; |
45 } | 59 } |
46 index = this->nextIndex(index, round); | 60 index = this->nextIndex(index, round); |
47 } | 61 } |
48 SkASSERT(0); // find: should be unreachable | |
49 return NULL; | 62 return NULL; |
50 } | 63 } |
51 | 64 |
52 // Add an entry with this key. We require that no entry with newEntry's key is already present. | 65 // Add an entry with this key. |
53 void add(T* newEntry) { | 66 void add(T* newEntry) { |
54 SkASSERT(NULL == this->find(GetKey(*newEntry))); | |
55 this->maybeGrow(); | 67 this->maybeGrow(); |
56 SkASSERT(this->validate()); | |
57 this->innerAdd(newEntry); | 68 this->innerAdd(newEntry); |
58 SkASSERT(this->validate()); | |
59 } | 69 } |
60 | 70 |
61 // Remove the entry with this key. We reqire that an entry with this key is present. | 71 // Remove the entry with this key. We require that the entry has been added before. |
62 void remove(const Key& key) { | 72 void remove(const T* entry) { |
mtklein
2013/11/27 14:21:54
Generally, these should be const T&. It's neither
Kimmo Kinnunen
2013/11/28 06:40:08
A) This is set that stores pointers
B) Pointers ar
bsalomon
2013/12/02 14:18:16
If the function (or something it calls) will retai
reed1
2013/12/02 16:20:40
agreed -- passing a ptr is clearer (to me) that th
| |
63 SkASSERT(NULL != this->find(key)); | 73 SkASSERT(NULL != this->find(GetKey(*entry))); |
64 this->innerRemove(key); | 74 this->innerRemove(entry); |
65 SkASSERT(this->validate()); | |
66 this->maybeShrink(); | 75 this->maybeShrink(); |
67 SkASSERT(this->validate()); | |
68 } | 76 } |
69 | 77 |
70 protected: | 78 #ifdef SK_DEBUG |
71 // These methods are used by tests only. | 79 void validate() const { |
72 | 80 #define SKTDYNAMICHASH_CHECK(x) SkASSERT((x)); if (!(x)) return; |
73 int capacity() const { return fCapacity; } | |
74 | |
75 // How many collisions do we go through before finding where this entry shou ld be inserted? | |
76 int countCollisions(const Key& key) const { | |
77 int index = this->firstIndex(key); | |
78 for (int round = 0; round < fCapacity; round++) { | |
79 const T* candidate = fArray[index]; | |
80 if (Empty() == candidate || Deleted() == candidate || Equal(*candida te, key)) { | |
81 return round; | |
82 } | |
83 index = this->nextIndex(index, round); | |
84 } | |
85 SkASSERT(0); // countCollisions: should be unreachable | |
86 return -1; | |
87 } | |
88 | |
89 private: | |
90 // We have two special values to indicate an empty or deleted entry. | |
91 static T* Empty() { return reinterpret_cast<T*>(0); } // i.e. NULL | |
92 static T* Deleted() { return reinterpret_cast<T*>(1); } // Also an invalid pointer. | |
93 | |
94 static T** AllocArray(int capacity) { | |
95 return (T**)sk_calloc_throw(sizeof(T*) * capacity); // All cells == Emp ty(). | |
96 } | |
97 | |
98 void reset(int capacity) { | |
99 fCount = 0; | |
100 fDeleted = 0; | |
101 fCapacity = capacity; | |
102 fArray = AllocArray(fCapacity); | |
103 } | |
104 | |
105 bool validate() const { | |
106 #define SKTDYNAMICHASH_CHECK(x) SkASSERT((x)); if (!(x)) return false | |
107 | 81 |
108 // Is capacity sane? | 82 // Is capacity sane? |
109 SKTDYNAMICHASH_CHECK(SkIsPow2(fCapacity)); | 83 SKTDYNAMICHASH_CHECK(SkIsPow2(fCapacity)); |
110 SKTDYNAMICHASH_CHECK(fCapacity >= kMinCapacity); | 84 SKTDYNAMICHASH_CHECK(fCapacity >= kMinCapacity); |
111 | 85 |
112 // Is fCount correct? | 86 // Is fCount correct? |
113 int count = 0; | 87 int count = 0; |
114 for (int i = 0; i < fCapacity; i++) { | 88 for (int i = 0; i < fCapacity; i++) { |
115 if (Empty() != fArray[i] && Deleted() != fArray[i]) { | 89 if (Empty() != fArray[i] && Deleted() != fArray[i]) { |
116 count++; | 90 count++; |
(...skipping 21 matching lines...) Expand all Loading... | |
138 // Are all entries unique? | 112 // Are all entries unique? |
139 for (int i = 0; i < fCapacity; i++) { | 113 for (int i = 0; i < fCapacity; i++) { |
140 if (Empty() == fArray[i] || Deleted() == fArray[i]) { | 114 if (Empty() == fArray[i] || Deleted() == fArray[i]) { |
141 continue; | 115 continue; |
142 } | 116 } |
143 for (int j = i+1; j < fCapacity; j++) { | 117 for (int j = i+1; j < fCapacity; j++) { |
144 if (Empty() == fArray[j] || Deleted() == fArray[j]) { | 118 if (Empty() == fArray[j] || Deleted() == fArray[j]) { |
145 continue; | 119 continue; |
146 } | 120 } |
147 SKTDYNAMICHASH_CHECK(fArray[i] != fArray[j]); | 121 SKTDYNAMICHASH_CHECK(fArray[i] != fArray[j]); |
148 SKTDYNAMICHASH_CHECK(!Equal(*fArray[i], GetKey(*fArray[j]))); | |
149 SKTDYNAMICHASH_CHECK(!Equal(*fArray[j], GetKey(*fArray[i]))); | |
150 } | 122 } |
151 } | 123 } |
152 #undef SKTDYNAMICHASH_CHECK | 124 #undef SKTDYNAMICHASH_CHECK |
153 return true; | 125 } |
126 #else | |
127 void validate() const { } | |
128 #endif | |
129 | |
130 protected: | |
131 // These methods are used by tests only. | |
132 | |
133 int capacity() const { return fCapacity; } | |
134 | |
135 // How many collisions do we go through before finding where this entry shou ld be inseretd? | |
136 int countCollisions(const Key& key) const { | |
137 int index = this->firstIndex(key); | |
138 for (int round = 0; round < fCapacity; round++) { | |
139 const T* candidate = fArray[index]; | |
140 if (Empty() == candidate || Deleted() == candidate || Equal(*candida te, key)) { | |
141 return round; | |
142 } | |
143 index = this->nextIndex(index, round); | |
144 } | |
145 SkASSERT(0); // countCollisions: should be unreachable | |
146 return -1; | |
147 } | |
148 | |
149 private: | |
150 // We have two special values to indicate an empty or deleted entry. | |
151 static T* Empty() { return reinterpret_cast<T*>(0); } // i.e. NULL | |
152 static T* Deleted() { return reinterpret_cast<T*>(1); } // Also an invalid pointer. | |
153 | |
154 static T** AllocArray(int capacity) { | |
155 return (T**)sk_calloc_throw(sizeof(T*) * capacity); // All cells == Emp ty(). | |
156 } | |
157 | |
158 void reset(int capacity) { | |
159 fCount = 0; | |
160 fDeleted = 0; | |
161 fCapacity = capacity; | |
162 fArray = AllocArray(fCapacity); | |
154 } | 163 } |
155 | 164 |
156 void innerAdd(T* newEntry) { | 165 void innerAdd(T* newEntry) { |
157 const Key& key = GetKey(*newEntry); | 166 const Key& key = GetKey(*newEntry); |
158 int index = this->firstIndex(key); | 167 int index = this->firstIndex(key); |
159 for (int round = 0; round < fCapacity; round++) { | 168 for (int round = 0; round < fCapacity; round++) { |
160 const T* candidate = fArray[index]; | 169 const T* candidate = fArray[index]; |
161 if (Empty() == candidate || Deleted() == candidate) { | 170 if (Empty() == candidate || Deleted() == candidate) { |
162 if (Deleted() == candidate) { | 171 if (Deleted() == candidate) { |
163 fDeleted--; | 172 fDeleted--; |
164 } | 173 } |
165 fCount++; | 174 fCount++; |
166 fArray[index] = newEntry; | 175 fArray[index] = newEntry; |
167 return; | 176 return; |
168 } | 177 } |
169 index = this->nextIndex(index, round); | 178 index = this->nextIndex(index, round); |
170 } | 179 } |
171 SkASSERT(0); // add: should be unreachable | 180 SkASSERT(0); // add: should be unreachable |
172 } | 181 } |
173 | 182 |
174 void innerRemove(const Key& key) { | 183 void innerRemove(const T* entry) { |
184 const Key& key = GetKey(*entry); | |
175 const int firstIndex = this->firstIndex(key); | 185 const int firstIndex = this->firstIndex(key); |
176 int index = firstIndex; | 186 int index = firstIndex; |
177 for (int round = 0; round < fCapacity; round++) { | 187 for (int round = 0; round < fCapacity; round++) { |
178 const T* candidate = fArray[index]; | 188 const T* candidate = fArray[index]; |
179 if (Deleted() != candidate && Equal(*candidate, key)) { | 189 if (Deleted() != candidate && Equal(*candidate, key) && fArray[index ] == entry) { |
180 fDeleted++; | 190 fDeleted++; |
181 fCount--; | 191 fCount--; |
182 fArray[index] = Deleted(); | 192 fArray[index] = Deleted(); |
183 return; | 193 return; |
184 } | 194 } |
185 index = this->nextIndex(index, round); | 195 index = this->nextIndex(index, round); |
186 } | 196 } |
187 SkASSERT(0); // innerRemove: should be unreachable | 197 SkASSERT(0); // innerRemove: should be unreachable |
188 } | 198 } |
189 | 199 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 return (index + round + 1) & this->hashMask(); | 240 return (index + round + 1) & this->hashMask(); |
231 } | 241 } |
232 | 242 |
233 int fCount; // Number of non Empty(), non Deleted() entries in fArray. | 243 int fCount; // Number of non Empty(), non Deleted() entries in fArray. |
234 int fDeleted; // Number of Deleted() entries in fArray. | 244 int fDeleted; // Number of Deleted() entries in fArray. |
235 int fCapacity; // Number of entries in fArray. Always a power of 2. | 245 int fCapacity; // Number of entries in fArray. Always a power of 2. |
236 T** fArray; | 246 T** fArray; |
237 }; | 247 }; |
238 | 248 |
239 #endif | 249 #endif |
OLD | NEW |