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