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