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 
8 #ifndef SkTHash_DEFINED
9 #define SkTHash_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/private/SkChecksum.h"
13 #include "include/private/SkTemplates.h"
14 #include <new>
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>
26 class SkTHashTable {
27 public:
SkTHashTable()28     SkTHashTable() : fCount(0), fCapacity(0) {}
SkTHashTable(SkTHashTable && other)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 
42     // Clear the table.
reset()43     void reset() { *this = SkTHashTable(); }
44 
45     // How many entries are in the table?
count()46     int count() const { return fCount; }
47 
48     // Approximately how many bytes of memory do we use beyond sizeof(*this)?
approxBytesUsed()49     size_t approxBytesUsed() const { return fCapacity * sizeof(Slot); }
50 
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.
set(T val)63     T* set(T val) {
64         if (4 * fCount >= 3 * fCapacity) {
65             this->resize(fCapacity > 0 ? fCapacity * 2 : 4);
66         }
67         return this->uncheckedSet(std::move(val));
68     }
69 
70     // If there is an entry in the table with this key, return a pointer to it.  If not, null.
find(const K & key)71     T* find(const K& key) const {
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()) {
77                 return nullptr;
78             }
79             if (hash == s.hash && key == Traits::GetKey(s.val)) {
80                 return &s.val;
81             }
82             index = this->next(index);
83         }
84         SkASSERT(fCapacity == 0);
85         return nullptr;
86     }
87 
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.
findOrNull(const K & key)90     T findOrNull(const K& key) const {
91         if (T* p = this->find(key)) {
92             return *p;
93         }
94         return nullptr;
95     }
96 
97     // Remove the value with this key from the hash table.
remove(const K & key)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());
106             if (hash == s.hash && key == Traits::GetKey(s.val)) {
107                 fCount--;
108                 break;
109             }
110             index = this->next(index);
111         }
112 
113         // Rearrange elements to restore the invariants for linear probing.
114         for (;;) {
115             Slot& emptySlot = fSlots[index];
116             int emptyIndex = index;
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];
127                 if (s.empty()) {
128                     // We're done shuffling elements around.  Clear the last empty slot.
129                     emptySlot = Slot();
130                     return;
131                 }
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         }
140     }
141 
142     // Call fn on every entry in the table.  You may mutate the entries, but be very careful.
143     template <typename Fn>  // f(T*)
foreach(Fn && fn)144     void foreach(Fn&& fn) {
145         for (int i = 0; i < fCapacity; i++) {
146             if (!fSlots[i].empty()) {
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&)
foreach(Fn && fn)154     void foreach(Fn&& fn) const {
155         for (int i = 0; i < fCapacity; i++) {
156             if (!fSlots[i].empty()) {
157                 fn(fSlots[i].val);
158             }
159         }
160     }
161 
162 private:
uncheckedSet(T && val)163     T* uncheckedSet(T&& val) {
164         const K& key = Traits::GetKey(val);
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];
169             if (s.empty()) {
170                 // New entry.
171                 s.val  = std::move(val);
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.
178                 // Note: this triggers extra copies when adding the same value repeatedly.
179                 s.val = std::move(val);
180                 return &s.val;
181             }
182 
183             index = this->next(index);
184         }
185         SkASSERT(false);
186         return nullptr;
187     }
188 
resize(int capacity)189     void resize(int capacity) {
190         int oldCapacity = fCapacity;
191         SkDEBUGCODE(int oldCount = fCount);
192 
193         fCount = 0;
194         fCapacity = capacity;
195         SkAutoTArray<Slot> oldSlots = std::move(fSlots);
196         fSlots = SkAutoTArray<Slot>(capacity);
197 
198         for (int i = 0; i < oldCapacity; i++) {
199             Slot& s = oldSlots[i];
200             if (!s.empty()) {
201                 this->uncheckedSet(std::move(s.val));
202             }
203         }
204         SkASSERT(fCount == oldCount);
205     }
206 
next(int index)207     int next(int index) const {
208         index--;
209         if (index < 0) { index += fCapacity; }
210         return index;
211     }
212 
Hash(const K & key)213     static uint32_t Hash(const K& key) {
214         uint32_t hash = Traits::Hash(key) & 0xffffffff;
215         return hash ? hash : 1;  // We reserve hash 0 to mark empty.
216     }
217 
218     struct Slot {
SlotSlot219         Slot() : val{}, hash(0) {}
SlotSlot220         Slot(T&& v, uint32_t h) : val(std::move(v)), hash(h) {}
SlotSlot221         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         }
227 
emptySlot228         bool empty() const { return this->hash == 0; }
229 
230         T        val;
231         uint32_t hash;
232     };
233 
234     int fCount, fCapacity;
235     SkAutoTArray<Slot> fSlots;
236 
237     SkTHashTable(const SkTHashTable&) = delete;
238     SkTHashTable& operator=(const SkTHashTable&) = delete;
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.
243 template <typename K, typename V, typename HashK = SkGoodHash>
244 class SkTHashMap {
245 public:
SkTHashMap()246     SkTHashMap() {}
247     SkTHashMap(SkTHashMap&&) = default;
248     SkTHashMap& operator=(SkTHashMap&&) = default;
249 
250     // Clear the map.
reset()251     void reset() { fTable.reset(); }
252 
253     // How many key/value pairs are in the table?
count()254     int count() const { return fTable.count(); }
255 
256     // Approximately how many bytes of memory do we use beyond sizeof(*this)?
approxBytesUsed()257     size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
258 
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.
set(K key,V val)263     V* set(K key, V val) {
264         Pair* out = fTable.set({std::move(key), std::move(val)});
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.
269     // If not, return null.
find(const K & key)270     V* find(const K& key) const {
271         if (Pair* p = fTable.find(key)) {
272             return &p->val;
273         }
274         return nullptr;
275     }
276 
277     // Remove the key/value entry in the table with this key.
remove(const K & key)278     void remove(const K& key) {
279         SkASSERT(this->find(key));
280         fTable.remove(key);
281     }
282 
283     // Call fn on every key/value pair in the table.  You may mutate the value but not the key.
284     template <typename Fn>  // f(K, V*) or f(const K&, V*)
foreach(Fn && fn)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&).
foreach(Fn && fn)291     void foreach(Fn&& fn) const {
292         fTable.foreach([&fn](const Pair& p){ fn(p.key, p.val); });
293     }
294 
295 private:
296     struct Pair {
297         K key;
298         V val;
GetKeyPair299         static const K& GetKey(const Pair& p) { return p.key; }
HashPair300         static auto Hash(const K& key) { return HashK()(key); }
301     };
302 
303     SkTHashTable<Pair, K> fTable;
304 
305     SkTHashMap(const SkTHashMap&) = delete;
306     SkTHashMap& operator=(const SkTHashMap&) = delete;
307 };
308 
309 // A set of T.  T is treated as an ordinary copyable C++ type.
310 template <typename T, typename HashT = SkGoodHash>
311 class SkTHashSet {
312 public:
SkTHashSet()313     SkTHashSet() {}
314     SkTHashSet(SkTHashSet&&) = default;
315     SkTHashSet& operator=(SkTHashSet&&) = default;
316 
317     // Clear the set.
reset()318     void reset() { fTable.reset(); }
319 
320     // How many items are in the set?
count()321     int count() const { return fTable.count(); }
322 
323     // Is empty?
empty()324     bool empty() const { return fTable.count() == 0; }
325 
326     // Approximately how many bytes of memory do we use beyond sizeof(*this)?
approxBytesUsed()327     size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
328 
329     // Copy an item into the set.
add(T item)330     void add(T item) { fTable.set(std::move(item)); }
331 
332     // Is this item in the set?
contains(const T & item)333     bool contains(const T& item) const { return SkToBool(this->find(item)); }
334 
335     // If an item equal to this is in the set, return a pointer to it, otherwise null.
336     // This pointer remains valid until the next call to add().
find(const T & item)337     const T* find(const T& item) const { return fTable.find(item); }
338 
339     // Remove the item in the set equal to this.
remove(const T & item)340     void remove(const T& item) {
341         SkASSERT(this->contains(item));
342         fTable.remove(item);
343     }
344 
345     // Call fn on every item in the set.  You may not mutate anything.
346     template <typename Fn>  // f(T), f(const T&)
foreach(Fn && fn)347     void foreach (Fn&& fn) const {
348         fTable.foreach(fn);
349     }
350 
351 private:
352     struct Traits {
GetKeyTraits353         static const T& GetKey(const T& item) { return item; }
HashTraits354         static auto Hash(const T& item) { return HashT()(item); }
355     };
356     SkTHashTable<T, T, Traits> fTable;
357 
358     SkTHashSet(const SkTHashSet&) = delete;
359     SkTHashSet& operator=(const SkTHashSet&) = delete;
360 };
361 
362 #endif//SkTHash_DEFINED
363