• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                this->removeSlot(index);
108                return;
109             }
110             index = this->next(index);
111         }
112     }
113 
114     // Call fn on every entry in the table.  You may mutate the entries, but be very careful.
115     template <typename Fn>  // f(T*)
foreach(Fn && fn)116     void foreach(Fn&& fn) {
117         for (int i = 0; i < fCapacity; i++) {
118             if (!fSlots[i].empty()) {
119                 fn(&fSlots[i].val);
120             }
121         }
122     }
123 
124     // Call fn on every entry in the table.  You may not mutate anything.
125     template <typename Fn>  // f(T) or f(const T&)
foreach(Fn && fn)126     void foreach(Fn&& fn) const {
127         for (int i = 0; i < fCapacity; i++) {
128             if (!fSlots[i].empty()) {
129                 fn(fSlots[i].val);
130             }
131         }
132     }
133 
134     // Call fn on every entry in the table. Fn can return false to remove the entry. You may mutate
135     // the entries, but be very careful.
136     template <typename Fn>  // f(T*)
mutate(Fn && fn)137     void mutate(Fn&& fn) {
138         for (int i = 0; i < fCapacity;) {
139             bool keep = true;
140             if (!fSlots[i].empty()) {
141                 keep = fn(&fSlots[i].val);
142             }
143             if (keep) {
144                 i++;
145             } else {
146                 this->removeSlot(i);
147                 // Something may now have moved into slot i, so we'll loop
148                 // around to check slot i again.
149             }
150         }
151     }
152 
153 private:
uncheckedSet(T && val)154     T* uncheckedSet(T&& val) {
155         const K& key = Traits::GetKey(val);
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];
160             if (s.empty()) {
161                 // New entry.
162                 s.val  = std::move(val);
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.
169                 // Note: this triggers extra copies when adding the same value repeatedly.
170                 s.val = std::move(val);
171                 return &s.val;
172             }
173 
174             index = this->next(index);
175         }
176         SkASSERT(false);
177         return nullptr;
178     }
179 
resize(int capacity)180     void resize(int capacity) {
181         int oldCapacity = fCapacity;
182         SkDEBUGCODE(int oldCount = fCount);
183 
184         fCount = 0;
185         fCapacity = capacity;
186         SkAutoTArray<Slot> oldSlots = std::move(fSlots);
187         fSlots = SkAutoTArray<Slot>(capacity);
188 
189         for (int i = 0; i < oldCapacity; i++) {
190             Slot& s = oldSlots[i];
191             if (!s.empty()) {
192                 this->uncheckedSet(std::move(s.val));
193             }
194         }
195         SkASSERT(fCount == oldCount);
196     }
197 
removeSlot(int index)198     void removeSlot(int index) {
199         fCount--;
200 
201         // Rearrange elements to restore the invariants for linear probing.
202         for (;;) {
203             Slot& emptySlot = fSlots[index];
204             int emptyIndex = index;
205             int originalIndex;
206             // Look for an element that can be moved into the empty slot.
207             // If the empty slot is in between where an element landed, and its native slot, then
208             // move it to the empty slot. Don't move it if its native slot is in between where
209             // the element landed and the empty slot.
210             // [native] <= [empty] < [candidate] == GOOD, can move candidate to empty slot
211             // [empty] < [native] < [candidate] == BAD, need to leave candidate where it is
212             do {
213                 index = this->next(index);
214                 Slot& s = fSlots[index];
215                 if (s.empty()) {
216                     // We're done shuffling elements around.  Clear the last empty slot.
217                     emptySlot = Slot();
218                     return;
219                 }
220                 originalIndex = s.hash & (fCapacity - 1);
221             } while ((index <= originalIndex && originalIndex < emptyIndex)
222                      || (originalIndex < emptyIndex && emptyIndex < index)
223                      || (emptyIndex < index && index <= originalIndex));
224             // Move the element to the empty slot.
225             Slot& moveFrom = fSlots[index];
226             emptySlot = std::move(moveFrom);
227         }
228     }
229 
next(int index)230     int next(int index) const {
231         index--;
232         if (index < 0) { index += fCapacity; }
233         return index;
234     }
235 
Hash(const K & key)236     static uint32_t Hash(const K& key) {
237         uint32_t hash = Traits::Hash(key) & 0xffffffff;
238         return hash ? hash : 1;  // We reserve hash 0 to mark empty.
239     }
240 
241     struct Slot {
SlotSlot242         Slot() : val{}, hash(0) {}
SlotSlot243         Slot(T&& v, uint32_t h) : val(std::move(v)), hash(h) {}
SlotSlot244         Slot(Slot&& o) { *this = std::move(o); }
245         Slot& operator=(Slot&& o) {
246             val  = std::move(o.val);
247             hash = o.hash;
248             return *this;
249         }
250 
emptySlot251         bool empty() const { return this->hash == 0; }
252 
253         T        val;
254         uint32_t hash;
255     };
256 
257     int fCount, fCapacity;
258     SkAutoTArray<Slot> fSlots;
259 
260     SkTHashTable(const SkTHashTable&) = delete;
261     SkTHashTable& operator=(const SkTHashTable&) = delete;
262 };
263 
264 // Maps K->V.  A more user-friendly wrapper around SkTHashTable, suitable for most use cases.
265 // K and V are treated as ordinary copyable C++ types, with no assumed relationship between the two.
266 template <typename K, typename V, typename HashK = SkGoodHash>
267 class SkTHashMap {
268 public:
SkTHashMap()269     SkTHashMap() {}
270     SkTHashMap(SkTHashMap&&) = default;
271     SkTHashMap& operator=(SkTHashMap&&) = default;
272 
273     // Clear the map.
reset()274     void reset() { fTable.reset(); }
275 
276     // How many key/value pairs are in the table?
count()277     int count() const { return fTable.count(); }
278 
279     // Approximately how many bytes of memory do we use beyond sizeof(*this)?
approxBytesUsed()280     size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
281 
282     // N.B. The pointers returned by set() and find() are valid only until the next call to set().
283 
284     // Set key to val in the table, replacing any previous value with the same key.
285     // We copy both key and val, and return a pointer to the value copy now in the table.
set(K key,V val)286     V* set(K key, V val) {
287         Pair* out = fTable.set({std::move(key), std::move(val)});
288         return &out->val;
289     }
290 
291     // If there is key/value entry in the table with this key, return a pointer to the value.
292     // If not, return null.
find(const K & key)293     V* find(const K& key) const {
294         if (Pair* p = fTable.find(key)) {
295             return &p->val;
296         }
297         return nullptr;
298     }
299 
300     V& operator[](const K& key) {
301         if (V* val = this->find(key)) {
302             return *val;
303         }
304         return *this->set(key, V{});
305     }
306 
307     // Remove the key/value entry in the table with this key.
remove(const K & key)308     void remove(const K& key) {
309         SkASSERT(this->find(key));
310         fTable.remove(key);
311     }
312 
313     // Call fn on every key/value pair in the table.  You may mutate the value but not the key.
314     template <typename Fn>  // f(K, V*) or f(const K&, V*)
foreach(Fn && fn)315     void foreach(Fn&& fn) {
316         fTable.foreach([&fn](Pair* p){ fn(p->key, &p->val); });
317     }
318 
319     // Call fn on every key/value pair in the table.  You may not mutate anything.
320     template <typename Fn>  // f(K, V), f(const K&, V), f(K, const V&) or f(const K&, const V&).
foreach(Fn && fn)321     void foreach(Fn&& fn) const {
322         fTable.foreach([&fn](const Pair& p){ fn(p.key, p.val); });
323     }
324 
325     // Call fn on every key/value pair in the table. Fn may return false to remove the entry. You
326     // may mutate the value but not the key.
327     template <typename Fn>  // f(K, V*) or f(const K&, V*)
mutate(Fn && fn)328     void mutate(Fn&& fn) {
329         fTable.mutate([&fn](Pair* p) { return fn(p->key, &p->val); });
330     }
331 
332 private:
333     struct Pair {
334         K key;
335         V val;
GetKeyPair336         static const K& GetKey(const Pair& p) { return p.key; }
HashPair337         static auto Hash(const K& key) { return HashK()(key); }
338     };
339 
340     SkTHashTable<Pair, K> fTable;
341 
342     SkTHashMap(const SkTHashMap&) = delete;
343     SkTHashMap& operator=(const SkTHashMap&) = delete;
344 };
345 
346 // A set of T.  T is treated as an ordinary copyable C++ type.
347 template <typename T, typename HashT = SkGoodHash>
348 class SkTHashSet {
349 public:
SkTHashSet()350     SkTHashSet() {}
351     SkTHashSet(SkTHashSet&&) = default;
352     SkTHashSet& operator=(SkTHashSet&&) = default;
353 
354     // Clear the set.
reset()355     void reset() { fTable.reset(); }
356 
357     // How many items are in the set?
count()358     int count() const { return fTable.count(); }
359 
360     // Is empty?
empty()361     bool empty() const { return fTable.count() == 0; }
362 
363     // Approximately how many bytes of memory do we use beyond sizeof(*this)?
approxBytesUsed()364     size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
365 
366     // Copy an item into the set.
add(T item)367     void add(T item) { fTable.set(std::move(item)); }
368 
369     // Is this item in the set?
contains(const T & item)370     bool contains(const T& item) const { return SkToBool(this->find(item)); }
371 
372     // If an item equal to this is in the set, return a pointer to it, otherwise null.
373     // This pointer remains valid until the next call to add().
find(const T & item)374     const T* find(const T& item) const { return fTable.find(item); }
375 
376     // Remove the item in the set equal to this.
remove(const T & item)377     void remove(const T& item) {
378         SkASSERT(this->contains(item));
379         fTable.remove(item);
380     }
381 
382     // Call fn on every item in the set.  You may not mutate anything.
383     template <typename Fn>  // f(T), f(const T&)
foreach(Fn && fn)384     void foreach (Fn&& fn) const {
385         fTable.foreach(fn);
386     }
387 
388 private:
389     struct Traits {
GetKeyTraits390         static const T& GetKey(const T& item) { return item; }
HashTraits391         static auto Hash(const T& item) { return HashT()(item); }
392     };
393     SkTHashTable<T, T, Traits> fTable;
394 
395     SkTHashSet(const SkTHashSet&) = delete;
396     SkTHashSet& operator=(const SkTHashSet&) = delete;
397 };
398 
399 #endif//SkTHash_DEFINED
400