blob: 487661ce66c86160af499e43eb84be5b52d6fc9f [file] [log] [blame]
halcanary038cb5e2015-03-25 10:22:53 -07001/*
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
mtklein979e0ea2015-02-12 13:20:08 -08008#ifndef SkTHash_DEFINED
9#define SkTHash_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/private/SkChecksum.h"
13#include "include/private/SkTemplates.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040014#include <new>
mtklein979e0ea2015-02-12 13:20:08 -080015
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.
25template <typename T, typename K, typename Traits = T>
Hal Canary725d5ad2018-06-14 15:54:29 -040026class SkTHashTable {
mtklein979e0ea2015-02-12 13:20:08 -080027public:
Herb Derbyacef51f2016-12-14 14:20:08 -050028 SkTHashTable() : fCount(0), fCapacity(0) {}
Hal Canary725d5ad2018-06-14 15:54:29 -040029 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
mtklein2aa1f7e2015-02-20 12:35:32 -080042 // Clear the table.
Hal Canary725d5ad2018-06-14 15:54:29 -040043 void reset() { *this = SkTHashTable(); }
mtklein2aa1f7e2015-02-20 12:35:32 -080044
mtklein979e0ea2015-02-12 13:20:08 -080045 // How many entries are in the table?
46 int count() const { return fCount; }
47
mtklein469a3fe2015-08-07 09:33:37 -070048 // Approximately how many bytes of memory do we use beyond sizeof(*this)?
49 size_t approxBytesUsed() const { return fCapacity * sizeof(Slot); }
50
mtklein979e0ea2015-02-12 13:20:08 -080051 // !!!!!!!!!!!!!!!!! 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 Klein6b00a072016-12-15 15:13:13 -050063 T* set(T val) {
Herb Derbyacef51f2016-12-14 14:20:08 -050064 if (4 * fCount >= 3 * fCapacity) {
mtklein979e0ea2015-02-12 13:20:08 -080065 this->resize(fCapacity > 0 ? fCapacity * 2 : 4);
66 }
Mike Kleindb402ca2016-12-13 12:46:05 -050067 return this->uncheckedSet(std::move(val));
mtklein979e0ea2015-02-12 13:20:08 -080068 }
69
Herb Derbyacef51f2016-12-14 14:20:08 -050070 // If there is an entry in the table with this key, return a pointer to it. If not, null.
fmalita79ca0812015-02-12 17:32:49 -080071 T* find(const K& key) const {
mtklein979e0ea2015-02-12 13:20:08 -080072 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 Derbyacef51f2016-12-14 14:20:08 -050077 return nullptr;
mtklein979e0ea2015-02-12 13:20:08 -080078 }
Herb Derbyacef51f2016-12-14 14:20:08 -050079 if (hash == s.hash && key == Traits::GetKey(s.val)) {
mtklein979e0ea2015-02-12 13:20:08 -080080 return &s.val;
81 }
Herb Derbyacef51f2016-12-14 14:20:08 -050082 index = this->next(index);
mtklein979e0ea2015-02-12 13:20:08 -080083 }
84 SkASSERT(fCapacity == 0);
Herb Derbyacef51f2016-12-14 14:20:08 -050085 return nullptr;
mtklein979e0ea2015-02-12 13:20:08 -080086 }
87
Mike Klein4284ec62019-01-09 13:20:57 -050088 // 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
mtklein33d73c32015-04-21 06:53:56 -070097 // 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 Derbyacef51f2016-12-14 14:20:08 -0500106 if (hash == s.hash && key == Traits::GetKey(s.val)) {
mtklein33d73c32015-04-21 06:53:56 -0700107 fCount--;
Herb Derbyacef51f2016-12-14 14:20:08 -0500108 break;
mtklein33d73c32015-04-21 06:53:56 -0700109 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500110 index = this->next(index);
mtklein33d73c32015-04-21 06:53:56 -0700111 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500112
113 // Rearrange elements to restore the invariants for linear probing.
114 for (;;) {
115 Slot& emptySlot = fSlots[index];
116 int emptyIndex = index;
Herb Derbyacef51f2016-12-14 14:20:08 -0500117 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 Malita053730d2017-03-10 11:51:07 -0500127 if (s.empty()) {
128 // We're done shuffling elements around. Clear the last empty slot.
129 emptySlot = Slot();
130 return;
131 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500132 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 }
mtklein33d73c32015-04-21 06:53:56 -0700140 }
141
mtklein979e0ea2015-02-12 13:20:08 -0800142 // Call fn on every entry in the table. You may mutate the entries, but be very careful.
mtklein02f46cf2015-03-20 13:48:42 -0700143 template <typename Fn> // f(T*)
144 void foreach(Fn&& fn) {
mtklein979e0ea2015-02-12 13:20:08 -0800145 for (int i = 0; i < fCapacity; i++) {
Herb Derbyacef51f2016-12-14 14:20:08 -0500146 if (!fSlots[i].empty()) {
mtklein02f46cf2015-03-20 13:48:42 -0700147 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 Derbyacef51f2016-12-14 14:20:08 -0500156 if (!fSlots[i].empty()) {
mtklein02f46cf2015-03-20 13:48:42 -0700157 fn(fSlots[i].val);
mtklein979e0ea2015-02-12 13:20:08 -0800158 }
159 }
160 }
161
162private:
Mike Kleindb402ca2016-12-13 12:46:05 -0500163 T* uncheckedSet(T&& val) {
fmalita79ca0812015-02-12 17:32:49 -0800164 const K& key = Traits::GetKey(val);
mtklein979e0ea2015-02-12 13:20:08 -0800165 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 Derbyacef51f2016-12-14 14:20:08 -0500169 if (s.empty()) {
mtklein979e0ea2015-02-12 13:20:08 -0800170 // New entry.
Mike Kleindb402ca2016-12-13 12:46:05 -0500171 s.val = std::move(val);
mtklein979e0ea2015-02-12 13:20:08 -0800172 s.hash = hash;
173 fCount++;
174 return &s.val;
175 }
176 if (hash == s.hash && key == Traits::GetKey(s.val)) {
177 // Overwrite previous entry.
fmalita79ca0812015-02-12 17:32:49 -0800178 // Note: this triggers extra copies when adding the same value repeatedly.
Mike Kleindb402ca2016-12-13 12:46:05 -0500179 s.val = std::move(val);
mtklein979e0ea2015-02-12 13:20:08 -0800180 return &s.val;
181 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500182
183 index = this->next(index);
mtklein979e0ea2015-02-12 13:20:08 -0800184 }
185 SkASSERT(false);
Herb Derbyacef51f2016-12-14 14:20:08 -0500186 return nullptr;
mtklein979e0ea2015-02-12 13:20:08 -0800187 }
188
189 void resize(int capacity) {
190 int oldCapacity = fCapacity;
191 SkDEBUGCODE(int oldCount = fCount);
192
Herb Derbyacef51f2016-12-14 14:20:08 -0500193 fCount = 0;
mtklein979e0ea2015-02-12 13:20:08 -0800194 fCapacity = capacity;
Hal Canary67362362018-04-24 13:58:37 -0400195 SkAutoTArray<Slot> oldSlots = std::move(fSlots);
196 fSlots = SkAutoTArray<Slot>(capacity);
mtklein979e0ea2015-02-12 13:20:08 -0800197
198 for (int i = 0; i < oldCapacity; i++) {
Mike Kleindb402ca2016-12-13 12:46:05 -0500199 Slot& s = oldSlots[i];
Herb Derbyacef51f2016-12-14 14:20:08 -0500200 if (!s.empty()) {
Mike Kleindb402ca2016-12-13 12:46:05 -0500201 this->uncheckedSet(std::move(s.val));
mtklein979e0ea2015-02-12 13:20:08 -0800202 }
203 }
204 SkASSERT(fCount == oldCount);
205 }
206
Herb Derbyacef51f2016-12-14 14:20:08 -0500207 int next(int index) const {
208 index--;
209 if (index < 0) { index += fCapacity; }
210 return index;
mtklein979e0ea2015-02-12 13:20:08 -0800211 }
212
fmalita79ca0812015-02-12 17:32:49 -0800213 static uint32_t Hash(const K& key) {
Mike Klein089f13f2019-06-23 11:50:51 -0500214 uint32_t hash = Traits::Hash(key) & 0xffffffff;
Herb Derbyacef51f2016-12-14 14:20:08 -0500215 return hash ? hash : 1; // We reserve hash 0 to mark empty.
mtklein979e0ea2015-02-12 13:20:08 -0800216 }
217
218 struct Slot {
Mike Klein55f6fd52019-07-12 08:46:30 -0500219 Slot() : val{}, hash(0) {}
Herb Derbyacef51f2016-12-14 14:20:08 -0500220 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 }
mtklein33d73c32015-04-21 06:53:56 -0700227
Herb Derbyacef51f2016-12-14 14:20:08 -0500228 bool empty() const { return this->hash == 0; }
mtklein979e0ea2015-02-12 13:20:08 -0800229
Herb Derbyacef51f2016-12-14 14:20:08 -0500230 T val;
mtklein979e0ea2015-02-12 13:20:08 -0800231 uint32_t hash;
232 };
233
Herb Derbyacef51f2016-12-14 14:20:08 -0500234 int fCount, fCapacity;
mtklein979e0ea2015-02-12 13:20:08 -0800235 SkAutoTArray<Slot> fSlots;
Hal Canary51382992018-06-15 20:27:52 -0400236
237 SkTHashTable(const SkTHashTable&) = delete;
238 SkTHashTable& operator=(const SkTHashTable&) = delete;
mtklein979e0ea2015-02-12 13:20:08 -0800239};
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.
mtkleinc8d1dd42015-10-15 12:23:01 -0700243template <typename K, typename V, typename HashK = SkGoodHash>
Hal Canary725d5ad2018-06-14 15:54:29 -0400244class SkTHashMap {
mtklein979e0ea2015-02-12 13:20:08 -0800245public:
246 SkTHashMap() {}
Hal Canary725d5ad2018-06-14 15:54:29 -0400247 SkTHashMap(SkTHashMap&&) = default;
Hal Canary725d5ad2018-06-14 15:54:29 -0400248 SkTHashMap& operator=(SkTHashMap&&) = default;
mtklein979e0ea2015-02-12 13:20:08 -0800249
mtklein2aa1f7e2015-02-20 12:35:32 -0800250 // Clear the map.
251 void reset() { fTable.reset(); }
252
mtklein979e0ea2015-02-12 13:20:08 -0800253 // How many key/value pairs are in the table?
254 int count() const { return fTable.count(); }
255
mtklein469a3fe2015-08-07 09:33:37 -0700256 // Approximately how many bytes of memory do we use beyond sizeof(*this)?
257 size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
258
mtklein979e0ea2015-02-12 13:20:08 -0800259 // 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 Klein6b00a072016-12-15 15:13:13 -0500263 V* set(K key, V val) {
Mike Kleindb402ca2016-12-13 12:46:05 -0500264 Pair* out = fTable.set({std::move(key), std::move(val)});
mtklein979e0ea2015-02-12 13:20:08 -0800265 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 Derbyacef51f2016-12-14 14:20:08 -0500269 // If not, return null.
fmalita79ca0812015-02-12 17:32:49 -0800270 V* find(const K& key) const {
mtklein979e0ea2015-02-12 13:20:08 -0800271 if (Pair* p = fTable.find(key)) {
272 return &p->val;
273 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500274 return nullptr;
mtklein979e0ea2015-02-12 13:20:08 -0800275 }
276
mtklein33d73c32015-04-21 06:53:56 -0700277 // 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
mtklein979e0ea2015-02-12 13:20:08 -0800283 // Call fn on every key/value pair in the table. You may mutate the value but not the key.
mtklein02f46cf2015-03-20 13:48:42 -0700284 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 }
mtklein979e0ea2015-02-12 13:20:08 -0800294
295private:
296 struct Pair {
297 K key;
298 V val;
fmalita79ca0812015-02-12 17:32:49 -0800299 static const K& GetKey(const Pair& p) { return p.key; }
Mike Klein089f13f2019-06-23 11:50:51 -0500300 static auto Hash(const K& key) { return HashK()(key); }
mtklein979e0ea2015-02-12 13:20:08 -0800301 };
mtklein979e0ea2015-02-12 13:20:08 -0800302
303 SkTHashTable<Pair, K> fTable;
Hal Canary51382992018-06-15 20:27:52 -0400304
305 SkTHashMap(const SkTHashMap&) = delete;
306 SkTHashMap& operator=(const SkTHashMap&) = delete;
mtklein979e0ea2015-02-12 13:20:08 -0800307};
308
Brian Salomon8fe24272017-07-07 12:56:11 -0400309// A set of T. T is treated as an ordinary copyable C++ type.
mtkleinc8d1dd42015-10-15 12:23:01 -0700310template <typename T, typename HashT = SkGoodHash>
Hal Canary725d5ad2018-06-14 15:54:29 -0400311class SkTHashSet {
mtklein979e0ea2015-02-12 13:20:08 -0800312public:
313 SkTHashSet() {}
Hal Canary725d5ad2018-06-14 15:54:29 -0400314 SkTHashSet(SkTHashSet&&) = default;
Hal Canary725d5ad2018-06-14 15:54:29 -0400315 SkTHashSet& operator=(SkTHashSet&&) = default;
mtklein979e0ea2015-02-12 13:20:08 -0800316
mtklein2aa1f7e2015-02-20 12:35:32 -0800317 // Clear the set.
318 void reset() { fTable.reset(); }
319
mtklein979e0ea2015-02-12 13:20:08 -0800320 // How many items are in the set?
321 int count() const { return fTable.count(); }
322
mtklein469a3fe2015-08-07 09:33:37 -0700323 // Approximately how many bytes of memory do we use beyond sizeof(*this)?
324 size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
325
mtklein979e0ea2015-02-12 13:20:08 -0800326 // Copy an item into the set.
Mike Klein6b00a072016-12-15 15:13:13 -0500327 void add(T item) { fTable.set(std::move(item)); }
mtklein979e0ea2015-02-12 13:20:08 -0800328
329 // Is this item in the set?
mtkleinfb8307c2015-04-01 11:21:27 -0700330 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); }
mtklein979e0ea2015-02-12 13:20:08 -0800335
mtklein33d73c32015-04-21 06:53:56 -0700336 // 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
halcanarybe27a112015-04-01 13:31:19 -0700342 // 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 {
mtklein33d73c32015-04-21 06:53:56 -0700345 fTable.foreach(fn);
halcanarybe27a112015-04-01 13:31:19 -0700346 }
347
mtklein979e0ea2015-02-12 13:20:08 -0800348private:
349 struct Traits {
fmalita79ca0812015-02-12 17:32:49 -0800350 static const T& GetKey(const T& item) { return item; }
Mike Klein089f13f2019-06-23 11:50:51 -0500351 static auto Hash(const T& item) { return HashT()(item); }
mtklein979e0ea2015-02-12 13:20:08 -0800352 };
353 SkTHashTable<T, T, Traits> fTable;
Hal Canary51382992018-06-15 20:27:52 -0400354
355 SkTHashSet(const SkTHashSet&) = delete;
356 SkTHashSet& operator=(const SkTHashSet&) = delete;
mtklein979e0ea2015-02-12 13:20:08 -0800357};
358
359#endif//SkTHash_DEFINED