blob: 43234c5fe440a904598587273a445a7d9e5079ca [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
halcanary038cb5e2015-03-25 10:22:53 -070011#include "SkChecksum.h"
mtklein979e0ea2015-02-12 13:20:08 -080012#include "SkTypes.h"
13#include "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
mtklein33d73c32015-04-21 06:53:56 -070088 // 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 Derbyacef51f2016-12-14 14:20:08 -050097 if (hash == s.hash && key == Traits::GetKey(s.val)) {
mtklein33d73c32015-04-21 06:53:56 -070098 fCount--;
Herb Derbyacef51f2016-12-14 14:20:08 -050099 break;
mtklein33d73c32015-04-21 06:53:56 -0700100 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500101 index = this->next(index);
mtklein33d73c32015-04-21 06:53:56 -0700102 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500103
104 // Rearrange elements to restore the invariants for linear probing.
105 for (;;) {
106 Slot& emptySlot = fSlots[index];
107 int emptyIndex = index;
Herb Derbyacef51f2016-12-14 14:20:08 -0500108 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 Malita053730d2017-03-10 11:51:07 -0500118 if (s.empty()) {
119 // We're done shuffling elements around. Clear the last empty slot.
120 emptySlot = Slot();
121 return;
122 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500123 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 }
mtklein33d73c32015-04-21 06:53:56 -0700131 }
132
mtklein979e0ea2015-02-12 13:20:08 -0800133 // Call fn on every entry in the table. You may mutate the entries, but be very careful.
mtklein02f46cf2015-03-20 13:48:42 -0700134 template <typename Fn> // f(T*)
135 void foreach(Fn&& fn) {
mtklein979e0ea2015-02-12 13:20:08 -0800136 for (int i = 0; i < fCapacity; i++) {
Herb Derbyacef51f2016-12-14 14:20:08 -0500137 if (!fSlots[i].empty()) {
mtklein02f46cf2015-03-20 13:48:42 -0700138 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 Derbyacef51f2016-12-14 14:20:08 -0500147 if (!fSlots[i].empty()) {
mtklein02f46cf2015-03-20 13:48:42 -0700148 fn(fSlots[i].val);
mtklein979e0ea2015-02-12 13:20:08 -0800149 }
150 }
151 }
152
153private:
Mike Kleindb402ca2016-12-13 12:46:05 -0500154 T* uncheckedSet(T&& val) {
fmalita79ca0812015-02-12 17:32:49 -0800155 const K& key = Traits::GetKey(val);
mtklein979e0ea2015-02-12 13:20:08 -0800156 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 Derbyacef51f2016-12-14 14:20:08 -0500160 if (s.empty()) {
mtklein979e0ea2015-02-12 13:20:08 -0800161 // New entry.
Mike Kleindb402ca2016-12-13 12:46:05 -0500162 s.val = std::move(val);
mtklein979e0ea2015-02-12 13:20:08 -0800163 s.hash = hash;
164 fCount++;
165 return &s.val;
166 }
167 if (hash == s.hash && key == Traits::GetKey(s.val)) {
168 // Overwrite previous entry.
fmalita79ca0812015-02-12 17:32:49 -0800169 // Note: this triggers extra copies when adding the same value repeatedly.
Mike Kleindb402ca2016-12-13 12:46:05 -0500170 s.val = std::move(val);
mtklein979e0ea2015-02-12 13:20:08 -0800171 return &s.val;
172 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500173
174 index = this->next(index);
mtklein979e0ea2015-02-12 13:20:08 -0800175 }
176 SkASSERT(false);
Herb Derbyacef51f2016-12-14 14:20:08 -0500177 return nullptr;
mtklein979e0ea2015-02-12 13:20:08 -0800178 }
179
180 void resize(int capacity) {
181 int oldCapacity = fCapacity;
182 SkDEBUGCODE(int oldCount = fCount);
183
Herb Derbyacef51f2016-12-14 14:20:08 -0500184 fCount = 0;
mtklein979e0ea2015-02-12 13:20:08 -0800185 fCapacity = capacity;
Hal Canary67362362018-04-24 13:58:37 -0400186 SkAutoTArray<Slot> oldSlots = std::move(fSlots);
187 fSlots = SkAutoTArray<Slot>(capacity);
mtklein979e0ea2015-02-12 13:20:08 -0800188
189 for (int i = 0; i < oldCapacity; i++) {
Mike Kleindb402ca2016-12-13 12:46:05 -0500190 Slot& s = oldSlots[i];
Herb Derbyacef51f2016-12-14 14:20:08 -0500191 if (!s.empty()) {
Mike Kleindb402ca2016-12-13 12:46:05 -0500192 this->uncheckedSet(std::move(s.val));
mtklein979e0ea2015-02-12 13:20:08 -0800193 }
194 }
195 SkASSERT(fCount == oldCount);
196 }
197
Herb Derbyacef51f2016-12-14 14:20:08 -0500198 int next(int index) const {
199 index--;
200 if (index < 0) { index += fCapacity; }
201 return index;
mtklein979e0ea2015-02-12 13:20:08 -0800202 }
203
fmalita79ca0812015-02-12 17:32:49 -0800204 static uint32_t Hash(const K& key) {
mtklein979e0ea2015-02-12 13:20:08 -0800205 uint32_t hash = Traits::Hash(key);
Herb Derbyacef51f2016-12-14 14:20:08 -0500206 return hash ? hash : 1; // We reserve hash 0 to mark empty.
mtklein979e0ea2015-02-12 13:20:08 -0800207 }
208
209 struct Slot {
210 Slot() : hash(0) {}
Herb Derbyacef51f2016-12-14 14:20:08 -0500211 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 }
mtklein33d73c32015-04-21 06:53:56 -0700218
Herb Derbyacef51f2016-12-14 14:20:08 -0500219 bool empty() const { return this->hash == 0; }
mtklein979e0ea2015-02-12 13:20:08 -0800220
Herb Derbyacef51f2016-12-14 14:20:08 -0500221 T val;
mtklein979e0ea2015-02-12 13:20:08 -0800222 uint32_t hash;
223 };
224
Herb Derbyacef51f2016-12-14 14:20:08 -0500225 int fCount, fCapacity;
mtklein979e0ea2015-02-12 13:20:08 -0800226 SkAutoTArray<Slot> fSlots;
Hal Canary51382992018-06-15 20:27:52 -0400227
228 SkTHashTable(const SkTHashTable&) = delete;
229 SkTHashTable& operator=(const SkTHashTable&) = delete;
mtklein979e0ea2015-02-12 13:20:08 -0800230};
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.
mtkleinc8d1dd42015-10-15 12:23:01 -0700234template <typename K, typename V, typename HashK = SkGoodHash>
Hal Canary725d5ad2018-06-14 15:54:29 -0400235class SkTHashMap {
mtklein979e0ea2015-02-12 13:20:08 -0800236public:
237 SkTHashMap() {}
Hal Canary725d5ad2018-06-14 15:54:29 -0400238 SkTHashMap(SkTHashMap&&) = default;
Hal Canary725d5ad2018-06-14 15:54:29 -0400239 SkTHashMap& operator=(SkTHashMap&&) = default;
mtklein979e0ea2015-02-12 13:20:08 -0800240
mtklein2aa1f7e2015-02-20 12:35:32 -0800241 // Clear the map.
242 void reset() { fTable.reset(); }
243
mtklein979e0ea2015-02-12 13:20:08 -0800244 // How many key/value pairs are in the table?
245 int count() const { return fTable.count(); }
246
mtklein469a3fe2015-08-07 09:33:37 -0700247 // Approximately how many bytes of memory do we use beyond sizeof(*this)?
248 size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
249
mtklein979e0ea2015-02-12 13:20:08 -0800250 // 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 Klein6b00a072016-12-15 15:13:13 -0500254 V* set(K key, V val) {
Mike Kleindb402ca2016-12-13 12:46:05 -0500255 Pair* out = fTable.set({std::move(key), std::move(val)});
mtklein979e0ea2015-02-12 13:20:08 -0800256 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 Derbyacef51f2016-12-14 14:20:08 -0500260 // If not, return null.
fmalita79ca0812015-02-12 17:32:49 -0800261 V* find(const K& key) const {
mtklein979e0ea2015-02-12 13:20:08 -0800262 if (Pair* p = fTable.find(key)) {
263 return &p->val;
264 }
Herb Derbyacef51f2016-12-14 14:20:08 -0500265 return nullptr;
mtklein979e0ea2015-02-12 13:20:08 -0800266 }
267
mtklein33d73c32015-04-21 06:53:56 -0700268 // 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
mtklein979e0ea2015-02-12 13:20:08 -0800274 // 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 -0700275 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 }
mtklein979e0ea2015-02-12 13:20:08 -0800285
286private:
287 struct Pair {
288 K key;
289 V val;
fmalita79ca0812015-02-12 17:32:49 -0800290 static const K& GetKey(const Pair& p) { return p.key; }
mtkleinc8d1dd42015-10-15 12:23:01 -0700291 static uint32_t Hash(const K& key) { return HashK()(key); }
mtklein979e0ea2015-02-12 13:20:08 -0800292 };
mtklein979e0ea2015-02-12 13:20:08 -0800293
294 SkTHashTable<Pair, K> fTable;
Hal Canary51382992018-06-15 20:27:52 -0400295
296 SkTHashMap(const SkTHashMap&) = delete;
297 SkTHashMap& operator=(const SkTHashMap&) = delete;
mtklein979e0ea2015-02-12 13:20:08 -0800298};
299
Brian Salomon8fe24272017-07-07 12:56:11 -0400300// A set of T. T is treated as an ordinary copyable C++ type.
mtkleinc8d1dd42015-10-15 12:23:01 -0700301template <typename T, typename HashT = SkGoodHash>
Hal Canary725d5ad2018-06-14 15:54:29 -0400302class SkTHashSet {
mtklein979e0ea2015-02-12 13:20:08 -0800303public:
304 SkTHashSet() {}
Hal Canary725d5ad2018-06-14 15:54:29 -0400305 SkTHashSet(SkTHashSet&&) = default;
Hal Canary725d5ad2018-06-14 15:54:29 -0400306 SkTHashSet& operator=(SkTHashSet&&) = default;
mtklein979e0ea2015-02-12 13:20:08 -0800307
mtklein2aa1f7e2015-02-20 12:35:32 -0800308 // Clear the set.
309 void reset() { fTable.reset(); }
310
mtklein979e0ea2015-02-12 13:20:08 -0800311 // How many items are in the set?
312 int count() const { return fTable.count(); }
313
mtklein469a3fe2015-08-07 09:33:37 -0700314 // Approximately how many bytes of memory do we use beyond sizeof(*this)?
315 size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
316
mtklein979e0ea2015-02-12 13:20:08 -0800317 // Copy an item into the set.
Mike Klein6b00a072016-12-15 15:13:13 -0500318 void add(T item) { fTable.set(std::move(item)); }
mtklein979e0ea2015-02-12 13:20:08 -0800319
320 // Is this item in the set?
mtkleinfb8307c2015-04-01 11:21:27 -0700321 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); }
mtklein979e0ea2015-02-12 13:20:08 -0800326
mtklein33d73c32015-04-21 06:53:56 -0700327 // 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
halcanarybe27a112015-04-01 13:31:19 -0700333 // 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 {
mtklein33d73c32015-04-21 06:53:56 -0700336 fTable.foreach(fn);
halcanarybe27a112015-04-01 13:31:19 -0700337 }
338
mtklein979e0ea2015-02-12 13:20:08 -0800339private:
340 struct Traits {
fmalita79ca0812015-02-12 17:32:49 -0800341 static const T& GetKey(const T& item) { return item; }
mtkleinc8d1dd42015-10-15 12:23:01 -0700342 static uint32_t Hash(const T& item) { return HashT()(item); }
mtklein979e0ea2015-02-12 13:20:08 -0800343 };
344 SkTHashTable<T, T, Traits> fTable;
Hal Canary51382992018-06-15 20:27:52 -0400345
346 SkTHashSet(const SkTHashSet&) = delete;
347 SkTHashSet& operator=(const SkTHashSet&) = delete;
mtklein979e0ea2015-02-12 13:20:08 -0800348};
349
350#endif//SkTHash_DEFINED