2015-03-25 17:22:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
#ifndef SkTHash_DEFINED
|
|
|
|
#define SkTHash_DEFINED
|
|
|
|
|
2015-03-25 17:22:53 +00:00
|
|
|
#include "SkChecksum.h"
|
2015-02-12 21:20:08 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "SkTemplates.h"
|
|
|
|
|
|
|
|
// Before trying to use SkTHashTable, look below to see if SkTHashMap or SkTHashSet works for you.
|
|
|
|
// They're easier to use, usually perform the same, and have fewer sharp edges.
|
|
|
|
|
|
|
|
// T and K are treated as ordinary copyable C++ types.
|
|
|
|
// Traits must have:
|
|
|
|
// - static K GetKey(T)
|
|
|
|
// - static uint32_t Hash(K)
|
|
|
|
// If the key is large and stored inside T, you may want to make K a const&.
|
|
|
|
// Similarly, if T is large you might want it to be a pointer.
|
|
|
|
template <typename T, typename K, typename Traits = T>
|
|
|
|
class SkTHashTable : SkNoncopyable {
|
|
|
|
public:
|
2015-04-21 13:53:56 +00:00
|
|
|
SkTHashTable() : fCount(0), fRemoved(0), fCapacity(0) {}
|
2015-02-12 21:20:08 +00:00
|
|
|
|
2015-02-20 20:35:32 +00:00
|
|
|
// Clear the table.
|
|
|
|
void reset() {
|
|
|
|
this->~SkTHashTable();
|
2015-08-26 20:07:48 +00:00
|
|
|
new (this) SkTHashTable;
|
2015-02-20 20:35:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// How many entries are in the table?
|
|
|
|
int count() const { return fCount; }
|
|
|
|
|
2015-08-07 16:33:37 +00:00
|
|
|
// Approximately how many bytes of memory do we use beyond sizeof(*this)?
|
|
|
|
size_t approxBytesUsed() const { return fCapacity * sizeof(Slot); }
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// !!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!!!!!
|
|
|
|
// set(), find() and foreach() all allow mutable access to table entries.
|
|
|
|
// If you change an entry so that it no longer has the same key, all hell
|
|
|
|
// will break loose. Do not do that!
|
|
|
|
//
|
|
|
|
// Please prefer to use SkTHashMap or SkTHashSet, which do not have this danger.
|
|
|
|
|
|
|
|
// The pointers returned by set() and find() are valid only until the next call to set().
|
|
|
|
// The pointers you receive in foreach() are only valid for its duration.
|
|
|
|
|
|
|
|
// Copy val into the hash table, returning a pointer to the copy now in the table.
|
|
|
|
// If there already is an entry in the table with the same key, we overwrite it.
|
2015-02-13 01:32:49 +00:00
|
|
|
T* set(const T& val) {
|
2015-04-21 13:53:56 +00:00
|
|
|
if (4 * (fCount+fRemoved) >= 3 * fCapacity) {
|
2015-02-12 21:20:08 +00:00
|
|
|
this->resize(fCapacity > 0 ? fCapacity * 2 : 4);
|
|
|
|
}
|
|
|
|
return this->uncheckedSet(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is an entry in the table with this key, return a pointer to it. If not, NULL.
|
2015-02-13 01:32:49 +00:00
|
|
|
T* find(const K& key) const {
|
2015-02-12 21:20:08 +00:00
|
|
|
uint32_t hash = Hash(key);
|
|
|
|
int index = hash & (fCapacity-1);
|
|
|
|
for (int n = 0; n < fCapacity; n++) {
|
|
|
|
Slot& s = fSlots[index];
|
|
|
|
if (s.empty()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-21 13:53:56 +00:00
|
|
|
if (!s.removed() && hash == s.hash && key == Traits::GetKey(s.val)) {
|
2015-02-12 21:20:08 +00:00
|
|
|
return &s.val;
|
|
|
|
}
|
|
|
|
index = this->next(index, n);
|
|
|
|
}
|
|
|
|
SkASSERT(fCapacity == 0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
// Remove the value with this key from the hash table.
|
|
|
|
void remove(const K& key) {
|
|
|
|
SkASSERT(this->find(key));
|
|
|
|
|
|
|
|
uint32_t hash = Hash(key);
|
|
|
|
int index = hash & (fCapacity-1);
|
|
|
|
for (int n = 0; n < fCapacity; n++) {
|
|
|
|
Slot& s = fSlots[index];
|
|
|
|
SkASSERT(!s.empty());
|
|
|
|
if (!s.removed() && hash == s.hash && key == Traits::GetKey(s.val)) {
|
|
|
|
fRemoved++;
|
|
|
|
fCount--;
|
|
|
|
s.markRemoved();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
index = this->next(index, n);
|
|
|
|
}
|
|
|
|
SkASSERT(fCapacity == 0);
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// Call fn on every entry in the table. You may mutate the entries, but be very careful.
|
2015-03-20 20:48:42 +00:00
|
|
|
template <typename Fn> // f(T*)
|
|
|
|
void foreach(Fn&& fn) {
|
2015-02-12 21:20:08 +00:00
|
|
|
for (int i = 0; i < fCapacity; i++) {
|
2015-04-21 13:53:56 +00:00
|
|
|
if (!fSlots[i].empty() && !fSlots[i].removed()) {
|
2015-03-20 20:48:42 +00:00
|
|
|
fn(&fSlots[i].val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call fn on every entry in the table. You may not mutate anything.
|
|
|
|
template <typename Fn> // f(T) or f(const T&)
|
|
|
|
void foreach(Fn&& fn) const {
|
|
|
|
for (int i = 0; i < fCapacity; i++) {
|
2015-04-21 13:53:56 +00:00
|
|
|
if (!fSlots[i].empty() && !fSlots[i].removed()) {
|
2015-03-20 20:48:42 +00:00
|
|
|
fn(fSlots[i].val);
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-13 01:32:49 +00:00
|
|
|
T* uncheckedSet(const T& val) {
|
|
|
|
const K& key = Traits::GetKey(val);
|
2015-02-12 21:20:08 +00:00
|
|
|
uint32_t hash = Hash(key);
|
|
|
|
int index = hash & (fCapacity-1);
|
|
|
|
for (int n = 0; n < fCapacity; n++) {
|
|
|
|
Slot& s = fSlots[index];
|
2015-04-21 13:53:56 +00:00
|
|
|
if (s.empty() || s.removed()) {
|
2015-02-12 21:20:08 +00:00
|
|
|
// New entry.
|
2015-04-21 13:53:56 +00:00
|
|
|
if (s.removed()) {
|
|
|
|
fRemoved--;
|
|
|
|
}
|
2015-02-12 21:20:08 +00:00
|
|
|
s.val = val;
|
|
|
|
s.hash = hash;
|
|
|
|
fCount++;
|
|
|
|
return &s.val;
|
|
|
|
}
|
|
|
|
if (hash == s.hash && key == Traits::GetKey(s.val)) {
|
|
|
|
// Overwrite previous entry.
|
2015-02-13 01:32:49 +00:00
|
|
|
// Note: this triggers extra copies when adding the same value repeatedly.
|
2015-02-12 21:20:08 +00:00
|
|
|
s.val = val;
|
|
|
|
return &s.val;
|
|
|
|
}
|
|
|
|
index = this->next(index, n);
|
|
|
|
}
|
|
|
|
SkASSERT(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize(int capacity) {
|
|
|
|
int oldCapacity = fCapacity;
|
|
|
|
SkDEBUGCODE(int oldCount = fCount);
|
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
fCount = fRemoved = 0;
|
2015-02-12 21:20:08 +00:00
|
|
|
fCapacity = capacity;
|
|
|
|
SkAutoTArray<Slot> oldSlots(capacity);
|
|
|
|
oldSlots.swap(fSlots);
|
|
|
|
|
|
|
|
for (int i = 0; i < oldCapacity; i++) {
|
|
|
|
const Slot& s = oldSlots[i];
|
2015-04-21 13:53:56 +00:00
|
|
|
if (!s.empty() && !s.removed()) {
|
2015-02-12 21:20:08 +00:00
|
|
|
this->uncheckedSet(s.val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkASSERT(fCount == oldCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
int next(int index, int n) const {
|
|
|
|
// A valid strategy explores all slots in [0, fCapacity) as n walks from 0 to fCapacity-1.
|
|
|
|
// Both of these strategies are valid:
|
|
|
|
//return (index + 0 + 1) & (fCapacity-1); // Linear probing.
|
|
|
|
return (index + n + 1) & (fCapacity-1); // Quadratic probing.
|
|
|
|
}
|
|
|
|
|
2015-02-13 01:32:49 +00:00
|
|
|
static uint32_t Hash(const K& key) {
|
2015-02-12 21:20:08 +00:00
|
|
|
uint32_t hash = Traits::Hash(key);
|
2015-04-21 13:53:56 +00:00
|
|
|
return hash < 2 ? hash+2 : hash; // We reserve hash 0 and 1 to mark empty or removed slots.
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Slot {
|
|
|
|
Slot() : hash(0) {}
|
2015-04-21 13:53:56 +00:00
|
|
|
bool empty() const { return this->hash == 0; }
|
|
|
|
bool removed() const { return this->hash == 1; }
|
|
|
|
|
|
|
|
void markRemoved() { this->hash = 1; }
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
T val;
|
|
|
|
uint32_t hash;
|
|
|
|
};
|
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
int fCount, fRemoved, fCapacity;
|
2015-02-12 21:20:08 +00:00
|
|
|
SkAutoTArray<Slot> fSlots;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Maps K->V. A more user-friendly wrapper around SkTHashTable, suitable for most use cases.
|
|
|
|
// K and V are treated as ordinary copyable C++ types, with no assumed relationship between the two.
|
2015-10-15 19:23:01 +00:00
|
|
|
template <typename K, typename V, typename HashK = SkGoodHash>
|
2015-02-12 21:20:08 +00:00
|
|
|
class SkTHashMap : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
SkTHashMap() {}
|
|
|
|
|
2015-02-20 20:35:32 +00:00
|
|
|
// Clear the map.
|
|
|
|
void reset() { fTable.reset(); }
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// How many key/value pairs are in the table?
|
|
|
|
int count() const { return fTable.count(); }
|
|
|
|
|
2015-08-07 16:33:37 +00:00
|
|
|
// Approximately how many bytes of memory do we use beyond sizeof(*this)?
|
|
|
|
size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// N.B. The pointers returned by set() and find() are valid only until the next call to set().
|
|
|
|
|
|
|
|
// Set key to val in the table, replacing any previous value with the same key.
|
|
|
|
// We copy both key and val, and return a pointer to the value copy now in the table.
|
2015-02-13 01:32:49 +00:00
|
|
|
V* set(const K& key, const V& val) {
|
2015-02-12 21:20:08 +00:00
|
|
|
Pair in = { key, val };
|
|
|
|
Pair* out = fTable.set(in);
|
|
|
|
return &out->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is key/value entry in the table with this key, return a pointer to the value.
|
|
|
|
// If not, return NULL.
|
2015-02-13 01:32:49 +00:00
|
|
|
V* find(const K& key) const {
|
2015-02-12 21:20:08 +00:00
|
|
|
if (Pair* p = fTable.find(key)) {
|
|
|
|
return &p->val;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
// Remove the key/value entry in the table with this key.
|
|
|
|
void remove(const K& key) {
|
|
|
|
SkASSERT(this->find(key));
|
|
|
|
fTable.remove(key);
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// Call fn on every key/value pair in the table. You may mutate the value but not the key.
|
2015-03-20 20:48:42 +00:00
|
|
|
template <typename Fn> // f(K, V*) or f(const K&, V*)
|
|
|
|
void foreach(Fn&& fn) {
|
|
|
|
fTable.foreach([&fn](Pair* p){ fn(p->key, &p->val); });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call fn on every key/value pair in the table. You may not mutate anything.
|
|
|
|
template <typename Fn> // f(K, V), f(const K&, V), f(K, const V&) or f(const K&, const V&).
|
|
|
|
void foreach(Fn&& fn) const {
|
|
|
|
fTable.foreach([&fn](const Pair& p){ fn(p.key, p.val); });
|
|
|
|
}
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Pair {
|
|
|
|
K key;
|
|
|
|
V val;
|
2015-02-13 01:32:49 +00:00
|
|
|
static const K& GetKey(const Pair& p) { return p.key; }
|
2015-10-15 19:23:01 +00:00
|
|
|
static uint32_t Hash(const K& key) { return HashK()(key); }
|
2015-02-12 21:20:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkTHashTable<Pair, K> fTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A set of T. T is treated as an ordiary copyable C++ type.
|
2015-10-15 19:23:01 +00:00
|
|
|
template <typename T, typename HashT = SkGoodHash>
|
2015-02-12 21:20:08 +00:00
|
|
|
class SkTHashSet : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
SkTHashSet() {}
|
|
|
|
|
2015-02-20 20:35:32 +00:00
|
|
|
// Clear the set.
|
|
|
|
void reset() { fTable.reset(); }
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// How many items are in the set?
|
|
|
|
int count() const { return fTable.count(); }
|
|
|
|
|
2015-08-07 16:33:37 +00:00
|
|
|
// Approximately how many bytes of memory do we use beyond sizeof(*this)?
|
|
|
|
size_t approxBytesUsed() const { return fTable.approxBytesUsed(); }
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
// Copy an item into the set.
|
2015-02-13 01:32:49 +00:00
|
|
|
void add(const T& item) { fTable.set(item); }
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
// Is this item in the set?
|
2015-04-01 18:21:27 +00:00
|
|
|
bool contains(const T& item) const { return SkToBool(this->find(item)); }
|
|
|
|
|
|
|
|
// If an item equal to this is in the set, return a pointer to it, otherwise null.
|
|
|
|
// This pointer remains valid until the next call to add().
|
|
|
|
const T* find(const T& item) const { return fTable.find(item); }
|
2015-02-12 21:20:08 +00:00
|
|
|
|
2015-04-21 13:53:56 +00:00
|
|
|
// Remove the item in the set equal to this.
|
|
|
|
void remove(const T& item) {
|
|
|
|
SkASSERT(this->contains(item));
|
|
|
|
fTable.remove(item);
|
|
|
|
}
|
|
|
|
|
2015-04-01 20:31:19 +00:00
|
|
|
// Call fn on every item in the set. You may not mutate anything.
|
|
|
|
template <typename Fn> // f(T), f(const T&)
|
|
|
|
void foreach (Fn&& fn) const {
|
2015-04-21 13:53:56 +00:00
|
|
|
fTable.foreach(fn);
|
2015-04-01 20:31:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
private:
|
|
|
|
struct Traits {
|
2015-02-13 01:32:49 +00:00
|
|
|
static const T& GetKey(const T& item) { return item; }
|
2015-10-15 19:23:01 +00:00
|
|
|
static uint32_t Hash(const T& item) { return HashT()(item); }
|
2015-02-12 21:20:08 +00:00
|
|
|
};
|
|
|
|
SkTHashTable<T, T, Traits> fTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif//SkTHash_DEFINED
|