halcanary | 038cb5e | 2015-03-25 10:22:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 8 | #ifndef SkTHash_DEFINED |
| 9 | #define SkTHash_DEFINED |
| 10 | |
halcanary | 038cb5e | 2015-03-25 10:22:53 -0700 | [diff] [blame] | 11 | #include "SkChecksum.h" |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | #include "SkTemplates.h" |
| 14 | |
| 15 | // Before trying to use SkTHashTable, look below to see if SkTHashMap or SkTHashSet works for you. |
| 16 | // They're easier to use, usually perform the same, and have fewer sharp edges. |
| 17 | |
| 18 | // T and K are treated as ordinary copyable C++ types. |
| 19 | // Traits must have: |
| 20 | // - static K GetKey(T) |
| 21 | // - static uint32_t Hash(K) |
| 22 | // If the key is large and stored inside T, you may want to make K a const&. |
| 23 | // Similarly, if T is large you might want it to be a pointer. |
| 24 | template <typename T, typename K, typename Traits = T> |
| 25 | class SkTHashTable : SkNoncopyable { |
| 26 | public: |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 27 | SkTHashTable() : fCount(0), fCapacity(0) {} |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 28 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 29 | // Clear the table. |
| 30 | void reset() { |
| 31 | this->~SkTHashTable(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 32 | new (this) SkTHashTable; |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 33 | } |
| 34 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 35 | // How many entries are in the table? |
| 36 | int count() const { return fCount; } |
| 37 | |
mtklein | 469a3fe | 2015-08-07 09:33:37 -0700 | [diff] [blame] | 38 | // Approximately how many bytes of memory do we use beyond sizeof(*this)? |
| 39 | size_t approxBytesUsed() const { return fCapacity * sizeof(Slot); } |
| 40 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 41 | // !!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!!!!! |
| 42 | // set(), find() and foreach() all allow mutable access to table entries. |
| 43 | // If you change an entry so that it no longer has the same key, all hell |
| 44 | // will break loose. Do not do that! |
| 45 | // |
| 46 | // Please prefer to use SkTHashMap or SkTHashSet, which do not have this danger. |
| 47 | |
| 48 | // The pointers returned by set() and find() are valid only until the next call to set(). |
| 49 | // The pointers you receive in foreach() are only valid for its duration. |
| 50 | |
| 51 | // Copy val into the hash table, returning a pointer to the copy now in the table. |
| 52 | // If there already is an entry in the table with the same key, we overwrite it. |
Mike Klein | 6b00a07 | 2016-12-15 15:13:13 -0500 | [diff] [blame] | 53 | T* set(T val) { |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 54 | if (4 * fCount >= 3 * fCapacity) { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 55 | this->resize(fCapacity > 0 ? fCapacity * 2 : 4); |
| 56 | } |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 57 | return this->uncheckedSet(std::move(val)); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 60 | // If there is an entry in the table with this key, return a pointer to it. If not, null. |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 61 | T* find(const K& key) const { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 62 | uint32_t hash = Hash(key); |
| 63 | int index = hash & (fCapacity-1); |
| 64 | for (int n = 0; n < fCapacity; n++) { |
| 65 | Slot& s = fSlots[index]; |
| 66 | if (s.empty()) { |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 67 | return nullptr; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 68 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 69 | if (hash == s.hash && key == Traits::GetKey(s.val)) { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 70 | return &s.val; |
| 71 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 72 | index = this->next(index); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 73 | } |
| 74 | SkASSERT(fCapacity == 0); |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 75 | return nullptr; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 76 | } |
| 77 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 78 | // Remove the value with this key from the hash table. |
| 79 | void remove(const K& key) { |
| 80 | SkASSERT(this->find(key)); |
| 81 | |
| 82 | uint32_t hash = Hash(key); |
| 83 | int index = hash & (fCapacity-1); |
| 84 | for (int n = 0; n < fCapacity; n++) { |
| 85 | Slot& s = fSlots[index]; |
| 86 | SkASSERT(!s.empty()); |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 87 | if (hash == s.hash && key == Traits::GetKey(s.val)) { |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 88 | fCount--; |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 89 | break; |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 90 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 91 | index = this->next(index); |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 92 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 93 | |
| 94 | // Rearrange elements to restore the invariants for linear probing. |
| 95 | for (;;) { |
| 96 | Slot& emptySlot = fSlots[index]; |
| 97 | int emptyIndex = index; |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 98 | int originalIndex; |
| 99 | // Look for an element that can be moved into the empty slot. |
| 100 | // If the empty slot is in between where an element landed, and its native slot, then |
| 101 | // move it to the empty slot. Don't move it if its native slot is in between where |
| 102 | // the element landed and the empty slot. |
| 103 | // [native] <= [empty] < [candidate] == GOOD, can move candidate to empty slot |
| 104 | // [empty] < [native] < [candidate] == BAD, need to leave candidate where it is |
| 105 | do { |
| 106 | index = this->next(index); |
| 107 | Slot& s = fSlots[index]; |
Florin Malita | 053730d | 2017-03-10 11:51:07 -0500 | [diff] [blame] | 108 | if (s.empty()) { |
| 109 | // We're done shuffling elements around. Clear the last empty slot. |
| 110 | emptySlot = Slot(); |
| 111 | return; |
| 112 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 113 | originalIndex = s.hash & (fCapacity - 1); |
| 114 | } while ((index <= originalIndex && originalIndex < emptyIndex) |
| 115 | || (originalIndex < emptyIndex && emptyIndex < index) |
| 116 | || (emptyIndex < index && index <= originalIndex)); |
| 117 | // Move the element to the empty slot. |
| 118 | Slot& moveFrom = fSlots[index]; |
| 119 | emptySlot = std::move(moveFrom); |
| 120 | } |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 123 | // Call fn on every entry in the table. You may mutate the entries, but be very careful. |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 124 | template <typename Fn> // f(T*) |
| 125 | void foreach(Fn&& fn) { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 126 | for (int i = 0; i < fCapacity; i++) { |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 127 | if (!fSlots[i].empty()) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 128 | fn(&fSlots[i].val); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Call fn on every entry in the table. You may not mutate anything. |
| 134 | template <typename Fn> // f(T) or f(const T&) |
| 135 | void foreach(Fn&& fn) const { |
| 136 | for (int i = 0; i < fCapacity; i++) { |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 137 | if (!fSlots[i].empty()) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 138 | fn(fSlots[i].val); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private: |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 144 | T* uncheckedSet(T&& val) { |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 145 | const K& key = Traits::GetKey(val); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 146 | uint32_t hash = Hash(key); |
| 147 | int index = hash & (fCapacity-1); |
| 148 | for (int n = 0; n < fCapacity; n++) { |
| 149 | Slot& s = fSlots[index]; |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 150 | if (s.empty()) { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 151 | // New entry. |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 152 | s.val = std::move(val); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 153 | s.hash = hash; |
| 154 | fCount++; |
| 155 | return &s.val; |
| 156 | } |
| 157 | if (hash == s.hash && key == Traits::GetKey(s.val)) { |
| 158 | // Overwrite previous entry. |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 159 | // Note: this triggers extra copies when adding the same value repeatedly. |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 160 | s.val = std::move(val); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 161 | return &s.val; |
| 162 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 163 | |
| 164 | index = this->next(index); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 165 | } |
| 166 | SkASSERT(false); |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 167 | return nullptr; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void resize(int capacity) { |
| 171 | int oldCapacity = fCapacity; |
| 172 | SkDEBUGCODE(int oldCount = fCount); |
| 173 | |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 174 | fCount = 0; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 175 | fCapacity = capacity; |
| 176 | SkAutoTArray<Slot> oldSlots(capacity); |
| 177 | oldSlots.swap(fSlots); |
| 178 | |
| 179 | for (int i = 0; i < oldCapacity; i++) { |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 180 | Slot& s = oldSlots[i]; |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 181 | if (!s.empty()) { |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 182 | this->uncheckedSet(std::move(s.val)); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | SkASSERT(fCount == oldCount); |
| 186 | } |
| 187 | |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 188 | int next(int index) const { |
| 189 | index--; |
| 190 | if (index < 0) { index += fCapacity; } |
| 191 | return index; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 192 | } |
| 193 | |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 194 | static uint32_t Hash(const K& key) { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 195 | uint32_t hash = Traits::Hash(key); |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 196 | return hash ? hash : 1; // We reserve hash 0 to mark empty. |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | struct Slot { |
| 200 | Slot() : hash(0) {} |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 201 | Slot(T&& v, uint32_t h) : val(std::move(v)), hash(h) {} |
| 202 | Slot(Slot&& o) { *this = std::move(o); } |
| 203 | Slot& operator=(Slot&& o) { |
| 204 | val = std::move(o.val); |
| 205 | hash = o.hash; |
| 206 | return *this; |
| 207 | } |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 208 | |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 209 | bool empty() const { return this->hash == 0; } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 210 | |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 211 | T val; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 212 | uint32_t hash; |
| 213 | }; |
| 214 | |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 215 | int fCount, fCapacity; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 216 | SkAutoTArray<Slot> fSlots; |
| 217 | }; |
| 218 | |
| 219 | // Maps K->V. A more user-friendly wrapper around SkTHashTable, suitable for most use cases. |
| 220 | // K and V are treated as ordinary copyable C++ types, with no assumed relationship between the two. |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 221 | template <typename K, typename V, typename HashK = SkGoodHash> |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 222 | class SkTHashMap : SkNoncopyable { |
| 223 | public: |
| 224 | SkTHashMap() {} |
| 225 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 226 | // Clear the map. |
| 227 | void reset() { fTable.reset(); } |
| 228 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 229 | // How many key/value pairs are in the table? |
| 230 | int count() const { return fTable.count(); } |
| 231 | |
mtklein | 469a3fe | 2015-08-07 09:33:37 -0700 | [diff] [blame] | 232 | // Approximately how many bytes of memory do we use beyond sizeof(*this)? |
| 233 | size_t approxBytesUsed() const { return fTable.approxBytesUsed(); } |
| 234 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 235 | // N.B. The pointers returned by set() and find() are valid only until the next call to set(). |
| 236 | |
| 237 | // Set key to val in the table, replacing any previous value with the same key. |
| 238 | // We copy both key and val, and return a pointer to the value copy now in the table. |
Mike Klein | 6b00a07 | 2016-12-15 15:13:13 -0500 | [diff] [blame] | 239 | V* set(K key, V val) { |
Mike Klein | db402ca | 2016-12-13 12:46:05 -0500 | [diff] [blame] | 240 | Pair* out = fTable.set({std::move(key), std::move(val)}); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 241 | return &out->val; |
| 242 | } |
| 243 | |
| 244 | // If there is key/value entry in the table with this key, return a pointer to the value. |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 245 | // If not, return null. |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 246 | V* find(const K& key) const { |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 247 | if (Pair* p = fTable.find(key)) { |
| 248 | return &p->val; |
| 249 | } |
Herb Derby | acef51f | 2016-12-14 14:20:08 -0500 | [diff] [blame] | 250 | return nullptr; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 251 | } |
| 252 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 253 | // Remove the key/value entry in the table with this key. |
| 254 | void remove(const K& key) { |
| 255 | SkASSERT(this->find(key)); |
| 256 | fTable.remove(key); |
| 257 | } |
| 258 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 259 | // Call fn on every key/value pair in the table. You may mutate the value but not the key. |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 260 | template <typename Fn> // f(K, V*) or f(const K&, V*) |
| 261 | void foreach(Fn&& fn) { |
| 262 | fTable.foreach([&fn](Pair* p){ fn(p->key, &p->val); }); |
| 263 | } |
| 264 | |
| 265 | // Call fn on every key/value pair in the table. You may not mutate anything. |
| 266 | template <typename Fn> // f(K, V), f(const K&, V), f(K, const V&) or f(const K&, const V&). |
| 267 | void foreach(Fn&& fn) const { |
| 268 | fTable.foreach([&fn](const Pair& p){ fn(p.key, p.val); }); |
| 269 | } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 270 | |
| 271 | private: |
| 272 | struct Pair { |
| 273 | K key; |
| 274 | V val; |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 275 | static const K& GetKey(const Pair& p) { return p.key; } |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 276 | static uint32_t Hash(const K& key) { return HashK()(key); } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 277 | }; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 278 | |
| 279 | SkTHashTable<Pair, K> fTable; |
| 280 | }; |
| 281 | |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 282 | // A set of T. T is treated as an ordinary copyable C++ type. |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 283 | template <typename T, typename HashT = SkGoodHash> |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 284 | class SkTHashSet : SkNoncopyable { |
| 285 | public: |
| 286 | SkTHashSet() {} |
| 287 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 288 | // Clear the set. |
| 289 | void reset() { fTable.reset(); } |
| 290 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 291 | // How many items are in the set? |
| 292 | int count() const { return fTable.count(); } |
| 293 | |
mtklein | 469a3fe | 2015-08-07 09:33:37 -0700 | [diff] [blame] | 294 | // Approximately how many bytes of memory do we use beyond sizeof(*this)? |
| 295 | size_t approxBytesUsed() const { return fTable.approxBytesUsed(); } |
| 296 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 297 | // Copy an item into the set. |
Mike Klein | 6b00a07 | 2016-12-15 15:13:13 -0500 | [diff] [blame] | 298 | void add(T item) { fTable.set(std::move(item)); } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 299 | |
| 300 | // Is this item in the set? |
mtklein | fb8307c | 2015-04-01 11:21:27 -0700 | [diff] [blame] | 301 | bool contains(const T& item) const { return SkToBool(this->find(item)); } |
| 302 | |
| 303 | // If an item equal to this is in the set, return a pointer to it, otherwise null. |
| 304 | // This pointer remains valid until the next call to add(). |
| 305 | const T* find(const T& item) const { return fTable.find(item); } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 306 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 307 | // Remove the item in the set equal to this. |
| 308 | void remove(const T& item) { |
| 309 | SkASSERT(this->contains(item)); |
| 310 | fTable.remove(item); |
| 311 | } |
| 312 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 313 | // Call fn on every item in the set. You may not mutate anything. |
| 314 | template <typename Fn> // f(T), f(const T&) |
| 315 | void foreach (Fn&& fn) const { |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 316 | fTable.foreach(fn); |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 317 | } |
| 318 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 319 | private: |
| 320 | struct Traits { |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 321 | static const T& GetKey(const T& item) { return item; } |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 322 | static uint32_t Hash(const T& item) { return HashT()(item); } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 323 | }; |
| 324 | SkTHashTable<T, T, Traits> fTable; |
| 325 | }; |
| 326 | |
| 327 | #endif//SkTHash_DEFINED |