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 template <typename T, | 14 template <typename T, |
15 typename Key, | 15 typename Key, |
16 const Key& (GetKey)(const T&), | 16 const Key& (GetKey)(const T&), |
17 uint32_t (Hash)(const Key&), | 17 uint32_t (Hash)(const Key&), |
18 bool (Equal)(const T&, const Key&), | 18 bool (Equal)(const T&, const Key&), |
19 int kGrowPercent = 75, // Larger -> more memory efficient, but slow er. | 19 int kGrowPercent = 75, // Larger -> more memory efficient, but slow er. |
20 int kShrinkPercent = 25> | 20 int kShrinkPercent = 25> |
21 class SkTDynamicHash { | 21 class SkTDynamicHash { |
22 static const int kMinCapacity = 4; // Smallest capacity we allow. | 22 static const int kMinCapacity = 4; // Smallest capacity we allow. |
23 public: | 23 public: |
24 SkTDynamicHash(int initialCapacity=64/sizeof(T*)) { | 24 SkTDynamicHash(int initialCapacity=64/sizeof(T*)) { |
25 this->reset(SkNextPow2(initialCapacity > kMinCapacity ? initialCapacity : kMinCapacity)); | 25 this->reset(SkNextPow2(initialCapacity > kMinCapacity ? initialCapacity : kMinCapacity)); |
26 SkASSERT(this->validate()); | |
mtklein
2013/12/03 19:02:02
Let's put this guy back. This will never be expen
| |
27 } | 26 } |
28 | 27 |
29 ~SkTDynamicHash() { | 28 ~SkTDynamicHash() { |
30 sk_free(fArray); | 29 sk_free(fArray); |
31 } | 30 } |
32 | 31 |
33 int count() const { return fCount; } | 32 int count() const { return fCount; } |
34 | 33 |
35 // Return the entry with this key if we have it, otherwise NULL. | 34 // Return the entry with this key if we have it, otherwise NULL. |
36 T* find(const Key& key) const { | 35 T* find(const Key& key) const { |
37 int index = this->firstIndex(key); | 36 int index = this->firstIndex(key); |
38 for (int round = 0; round < fCapacity; round++) { | 37 for (int round = 0; round < fCapacity; round++) { |
39 T* candidate = fArray[index]; | 38 T* candidate = fArray[index]; |
40 if (Empty() == candidate) { | 39 if (Empty() == candidate) { |
41 return NULL; | 40 return NULL; |
42 } | 41 } |
43 if (Deleted() != candidate && Equal(*candidate, key)) { | 42 if (Deleted() != candidate && Equal(*candidate, key)) { |
44 return candidate; | 43 return candidate; |
45 } | 44 } |
46 index = this->nextIndex(index, round); | 45 index = this->nextIndex(index, round); |
47 } | 46 } |
48 SkASSERT(0); // find: should be unreachable | 47 SkASSERT(0); // find: should be unreachable |
49 return NULL; | 48 return NULL; |
50 } | 49 } |
51 | 50 |
52 // Add an entry with this key. We require that no entry with newEntry's key is already present. | 51 // Add an entry with this key. We require that no entry with newEntry's key is already present. |
53 void add(T* newEntry) { | 52 void add(T* newEntry) { |
54 SkASSERT(NULL == this->find(GetKey(*newEntry))); | 53 SkASSERT(NULL == this->find(GetKey(*newEntry))); |
55 this->maybeGrow(); | 54 this->maybeGrow(); |
56 SkASSERT(this->validate()); | |
57 this->innerAdd(newEntry); | 55 this->innerAdd(newEntry); |
58 SkASSERT(this->validate()); | |
59 } | 56 } |
60 | 57 |
61 // Remove the entry with this key. We reqire that an entry with this key is present. | 58 // Remove the entry with this key. We reqire that an entry with this key is present. |
62 void remove(const Key& key) { | 59 void remove(const Key& key) { |
63 SkASSERT(NULL != this->find(key)); | 60 SkASSERT(NULL != this->find(key)); |
64 this->innerRemove(key); | 61 this->innerRemove(key); |
65 SkASSERT(this->validate()); | |
66 this->maybeShrink(); | 62 this->maybeShrink(); |
67 SkASSERT(this->validate()); | |
68 } | 63 } |
69 | 64 |
70 protected: | 65 #ifdef SK_DEBUG |
mtklein
2013/12/03 19:02:02
This is a little nit, but let's just move the #ifd
| |
71 // These methods are used by tests only. | 66 void validate() const { |
72 | 67 #define SKTDYNAMICHASH_CHECK(x) SkASSERT((x)); if (!(x)) return |
mtklein
2013/12/03 19:02:02
If we're guarding by SK_DEBUG, we can just use SkA
| |
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 | 68 |
108 // Is capacity sane? | 69 // Is capacity sane? |
109 SKTDYNAMICHASH_CHECK(SkIsPow2(fCapacity)); | 70 SKTDYNAMICHASH_CHECK(SkIsPow2(fCapacity)); |
110 SKTDYNAMICHASH_CHECK(fCapacity >= kMinCapacity); | 71 SKTDYNAMICHASH_CHECK(fCapacity >= kMinCapacity); |
111 | 72 |
112 // Is fCount correct? | 73 // Is fCount correct? |
113 int count = 0; | 74 int count = 0; |
114 for (int i = 0; i < fCapacity; i++) { | 75 for (int i = 0; i < fCapacity; i++) { |
115 if (Empty() != fArray[i] && Deleted() != fArray[i]) { | 76 if (Empty() != fArray[i] && Deleted() != fArray[i]) { |
116 count++; | 77 count++; |
(...skipping 26 matching lines...) Expand all Loading... | |
143 for (int j = i+1; j < fCapacity; j++) { | 104 for (int j = i+1; j < fCapacity; j++) { |
144 if (Empty() == fArray[j] || Deleted() == fArray[j]) { | 105 if (Empty() == fArray[j] || Deleted() == fArray[j]) { |
145 continue; | 106 continue; |
146 } | 107 } |
147 SKTDYNAMICHASH_CHECK(fArray[i] != fArray[j]); | 108 SKTDYNAMICHASH_CHECK(fArray[i] != fArray[j]); |
148 SKTDYNAMICHASH_CHECK(!Equal(*fArray[i], GetKey(*fArray[j]))); | 109 SKTDYNAMICHASH_CHECK(!Equal(*fArray[i], GetKey(*fArray[j]))); |
149 SKTDYNAMICHASH_CHECK(!Equal(*fArray[j], GetKey(*fArray[i]))); | 110 SKTDYNAMICHASH_CHECK(!Equal(*fArray[j], GetKey(*fArray[i]))); |
150 } | 111 } |
151 } | 112 } |
152 #undef SKTDYNAMICHASH_CHECK | 113 #undef SKTDYNAMICHASH_CHECK |
153 return true; | 114 } |
115 #else | |
116 void validate() const { } | |
117 #endif | |
118 | |
119 protected: | |
120 // These methods are used by tests only. | |
mtklein
2013/12/03 19:02:02
Can we now pull validate() down here too?
| |
121 | |
122 int capacity() const { return fCapacity; } | |
123 | |
124 // How many collisions do we go through before finding where this entry shou ld be inserted? | |
125 int countCollisions(const Key& key) const { | |
126 int index = this->firstIndex(key); | |
127 for (int round = 0; round < fCapacity; round++) { | |
128 const T* candidate = fArray[index]; | |
129 if (Empty() == candidate || Deleted() == candidate || Equal(*candida te, key)) { | |
130 return round; | |
131 } | |
132 index = this->nextIndex(index, round); | |
133 } | |
134 SkASSERT(0); // countCollisions: should be unreachable | |
135 return -1; | |
136 } | |
137 | |
138 private: | |
139 // We have two special values to indicate an empty or deleted entry. | |
140 static T* Empty() { return reinterpret_cast<T*>(0); } // i.e. NULL | |
141 static T* Deleted() { return reinterpret_cast<T*>(1); } // Also an invalid pointer. | |
142 | |
143 static T** AllocArray(int capacity) { | |
144 return (T**)sk_calloc_throw(sizeof(T*) * capacity); // All cells == Emp ty(). | |
145 } | |
146 | |
147 void reset(int capacity) { | |
148 fCount = 0; | |
149 fDeleted = 0; | |
150 fCapacity = capacity; | |
151 fArray = AllocArray(fCapacity); | |
154 } | 152 } |
155 | 153 |
156 void innerAdd(T* newEntry) { | 154 void innerAdd(T* newEntry) { |
157 const Key& key = GetKey(*newEntry); | 155 const Key& key = GetKey(*newEntry); |
158 int index = this->firstIndex(key); | 156 int index = this->firstIndex(key); |
159 for (int round = 0; round < fCapacity; round++) { | 157 for (int round = 0; round < fCapacity; round++) { |
160 const T* candidate = fArray[index]; | 158 const T* candidate = fArray[index]; |
161 if (Empty() == candidate || Deleted() == candidate) { | 159 if (Empty() == candidate || Deleted() == candidate) { |
162 if (Deleted() == candidate) { | 160 if (Deleted() == candidate) { |
163 fDeleted--; | 161 fDeleted--; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 return (index + round + 1) & this->hashMask(); | 228 return (index + round + 1) & this->hashMask(); |
231 } | 229 } |
232 | 230 |
233 int fCount; // Number of non Empty(), non Deleted() entries in fArray. | 231 int fCount; // Number of non Empty(), non Deleted() entries in fArray. |
234 int fDeleted; // Number of Deleted() entries in fArray. | 232 int fDeleted; // Number of Deleted() entries in fArray. |
235 int fCapacity; // Number of entries in fArray. Always a power of 2. | 233 int fCapacity; // Number of entries in fArray. Always a power of 2. |
236 T** fArray; | 234 T** fArray; |
237 }; | 235 }; |
238 | 236 |
239 #endif | 237 #endif |
OLD | NEW |