| 1 |
/* |
| 2 |
* Copyright 2013 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 SkTDynamicHash_DEFINED |
| 9 |
#define SkTDynamicHash_DEFINED |
| 10 |
|
| 11 |
// This is now a simple API wrapper around THashTable<T*>; |
| 12 |
// please just use SkTHash{Map,Set,Table} directly for new code. |
| 13 |
#include "src/core/SkTHash.h" |
| 14 |
|
| 15 |
// Traits requires: |
| 16 |
// static const Key& GetKey(const T&) { ... } |
| 17 |
// static uint32_t Hash(const Key&) { ... } |
| 18 |
// We'll look on T for these by default, or you can pass a custom Traits type. |
| 19 |
template <typename T, |
| 20 |
typename Key, |
| 21 |
typename Traits = T> |
| 22 |
class SkTDynamicHash { |
| 23 |
public: |
| 24 |
SkTDynamicHash() {} |
| 25 |
|
| 26 |
// It is not safe to call set() or remove() while iterating with either foreach(). |
| 27 |
// If you mutate the entries be very careful not to change the Key. |
| 28 |
|
| 29 |
template <typename Fn> // f(T*) |
| 30 |
void foreach(Fn&& fn) { |
| 31 |
fTable.foreach([&](T** entry) { fn(*entry); }); |
| 32 |
} |
| 33 |
template <typename Fn> // f(T) or f(const T&) |
| 34 |
void foreach(Fn&& fn) const { |
| 35 |
fTable.foreach([&](T* entry) { fn(*entry); }); |
| 36 |
} |
| 37 |
|
| 38 |
int count() const { return fTable.count(); } |
| 39 |
|
| 40 |
size_t approxBytesUsed() const { return fTable.approxBytesUsed(); } |
| 41 |
|
| 42 |
T* find(const Key& key) const { return fTable.findOrNull(key); } |
| 43 |
|
| 44 |
void add(T* entry) { fTable.set(entry); } |
| 45 |
void remove(const Key& key) { fTable.remove(key); } |
| 46 |
|
| 47 |
void rewind() { fTable.reset(); } |
| 48 |
void reset () { fTable.reset(); } |
| 49 |
|
| 50 |
private: |
| 51 |
struct AdaptedTraits { |
| 52 |
static const Key& GetKey(T* entry) { return Traits::GetKey(*entry); } |
| 53 |
static uint32_t Hash(const Key& key) { return Traits::Hash(key); } |
| 54 |
}; |
| 55 |
skia_private::THashTable<T*, Key, AdaptedTraits> fTable; |
| 56 |
}; |
| 57 |
|
| 58 |
#endif |