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
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkChecksum.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
2022-03-03 21:57:22 +00:00
|
|
|
|
|
|
|
#include <initializer_list>
|
2018-06-11 14:45:26 +00:00
|
|
|
#include <new>
|
2020-12-22 19:57:16 +00:00
|
|
|
#include <utility>
|
2015-02-12 21:20:08 +00:00
|
|
|
|
|
|
|
// 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>
|
2018-06-14 19:54:29 +00:00
|
|
|
class SkTHashTable {
|
2015-02-12 21:20:08 +00:00
|
|
|
public:
|
2020-10-08 15:13:01 +00:00
|
|
|
SkTHashTable() = default;
|
|
|
|
~SkTHashTable() = default;
|
|
|
|
|
|
|
|
SkTHashTable(const SkTHashTable& that) { *this = that; }
|
|
|
|
SkTHashTable( SkTHashTable&& that) { *this = std::move(that); }
|
|
|
|
|
|
|
|
SkTHashTable& operator=(const SkTHashTable& that) {
|
|
|
|
if (this != &that) {
|
|
|
|
fCount = that.fCount;
|
|
|
|
fCapacity = that.fCapacity;
|
|
|
|
fSlots.reset(that.fCapacity);
|
|
|
|
for (int i = 0; i < fCapacity; i++) {
|
|
|
|
fSlots[i] = that.fSlots[i];
|
|
|
|
}
|
2018-06-14 19:54:29 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:13:01 +00:00
|
|
|
SkTHashTable& operator=(SkTHashTable&& that) {
|
2020-10-08 15:01:00 +00:00
|
|
|
if (this != &that) {
|
2020-10-08 15:13:01 +00:00
|
|
|
fCount = that.fCount;
|
|
|
|
fCapacity = that.fCapacity;
|
|
|
|
fSlots = std::move(that.fSlots);
|
|
|
|
|
|
|
|
that.fCount = that.fCapacity = 0;
|
2020-10-08 15:01:00 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:35:32 +00:00
|
|
|
// Clear the table.
|
2018-06-14 19:54:29 +00:00
|
|
|
void reset() { *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; }
|
|
|
|
|
2020-12-22 19:57:16 +00:00
|
|
|
// How many slots does the table contain? (Note that unlike an array, hash tables can grow
|
|
|
|
// before reaching 100% capacity.)
|
|
|
|
int capacity() const { return fCapacity; }
|
|
|
|
|
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.
|
2016-12-15 20:13:13 +00:00
|
|
|
T* set(T val) {
|
2016-12-14 19:20:08 +00:00
|
|
|
if (4 * fCount >= 3 * fCapacity) {
|
2015-02-12 21:20:08 +00:00
|
|
|
this->resize(fCapacity > 0 ? fCapacity * 2 : 4);
|
|
|
|
}
|
2016-12-13 17:46:05 +00:00
|
|
|
return this->uncheckedSet(std::move(val));
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 19:20:08 +00:00
|
|
|
// 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()) {
|
2016-12-14 19:20:08 +00:00
|
|
|
return nullptr;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
2021-11-03 00:57:46 +00:00
|
|
|
if (hash == s.hash && key == Traits::GetKey(*s)) {
|
|
|
|
return &*s;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
2016-12-14 19:20:08 +00:00
|
|
|
index = this->next(index);
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
SkASSERT(fCapacity == 0);
|
2016-12-14 19:20:08 +00:00
|
|
|
return nullptr;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 18:20:57 +00:00
|
|
|
// If there is an entry in the table with this key, return it. If not, null.
|
|
|
|
// This only works for pointer type T, and cannot be used to find an nullptr entry.
|
|
|
|
T findOrNull(const K& key) const {
|
|
|
|
if (T* p = this->find(key)) {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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];
|
2021-11-03 00:57:46 +00:00
|
|
|
SkASSERT(s.has_value());
|
|
|
|
if (hash == s.hash && key == Traits::GetKey(*s)) {
|
2019-12-12 20:07:11 +00:00
|
|
|
this->removeSlot(index);
|
2020-03-14 15:03:50 +00:00
|
|
|
if (4 * fCount <= fCapacity && fCapacity > 4) {
|
|
|
|
this->resize(fCapacity / 2);
|
|
|
|
}
|
2019-12-12 20:07:11 +00:00
|
|
|
return;
|
2015-04-21 13:53:56 +00:00
|
|
|
}
|
2016-12-14 19:20:08 +00:00
|
|
|
index = this->next(index);
|
|
|
|
}
|
2015-04-21 13:53:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 21:57:22 +00:00
|
|
|
// Hash tables will automatically resize themselves when set() and remove() are called, but
|
|
|
|
// resize() can be called to manually grow capacity before a bulk insertion.
|
|
|
|
void resize(int capacity) {
|
|
|
|
SkASSERT(capacity >= fCount);
|
|
|
|
int oldCapacity = fCapacity;
|
|
|
|
SkDEBUGCODE(int oldCount = fCount);
|
|
|
|
|
|
|
|
fCount = 0;
|
|
|
|
fCapacity = capacity;
|
|
|
|
SkAutoTArray<Slot> oldSlots = std::move(fSlots);
|
|
|
|
fSlots = SkAutoTArray<Slot>(capacity);
|
|
|
|
|
|
|
|
for (int i = 0; i < oldCapacity; i++) {
|
|
|
|
Slot& s = oldSlots[i];
|
|
|
|
if (s.has_value()) {
|
|
|
|
this->uncheckedSet(*std::move(s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkASSERT(fCount == oldCount);
|
|
|
|
}
|
|
|
|
|
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++) {
|
2021-11-03 00:57:46 +00:00
|
|
|
if (fSlots[i].has_value()) {
|
|
|
|
fn(&*fSlots[i]);
|
2015-03-20 20:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++) {
|
2021-11-03 00:57:46 +00:00
|
|
|
if (fSlots[i].has_value()) {
|
|
|
|
fn(*fSlots[i]);
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 19:57:16 +00:00
|
|
|
// A basic iterator-like class which disallows mutation; sufficient for range-based for loops.
|
|
|
|
// Intended for use by SkTHashMap and SkTHashSet via begin() and end().
|
|
|
|
// Adding or removing elements may invalidate all iterators.
|
|
|
|
template <typename SlotVal>
|
|
|
|
class Iter {
|
|
|
|
public:
|
|
|
|
using TTable = SkTHashTable<T, K, Traits>;
|
|
|
|
|
|
|
|
Iter(const TTable* table, int slot) : fTable(table), fSlot(slot) {}
|
|
|
|
|
|
|
|
static Iter MakeBegin(const TTable* table) {
|
|
|
|
return Iter{table, table->firstPopulatedSlot()};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Iter MakeEnd(const TTable* table) {
|
|
|
|
return Iter{table, table->capacity()};
|
|
|
|
}
|
|
|
|
|
|
|
|
const SlotVal& operator*() const {
|
|
|
|
return *fTable->slot(fSlot);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SlotVal* operator->() const {
|
|
|
|
return fTable->slot(fSlot);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Iter& that) const {
|
|
|
|
// Iterators from different tables shouldn't be compared against each other.
|
|
|
|
SkASSERT(fTable == that.fTable);
|
|
|
|
return fSlot == that.fSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Iter& that) const {
|
|
|
|
return !(*this == that);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iter& operator++() {
|
|
|
|
fSlot = fTable->nextPopulatedSlot(fSlot);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iter operator++(int) {
|
|
|
|
Iter old = *this;
|
|
|
|
this->operator++();
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const TTable* fTable;
|
|
|
|
int fSlot;
|
|
|
|
};
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
private:
|
2020-12-22 19:57:16 +00:00
|
|
|
// Finds the first non-empty slot for an iterator.
|
|
|
|
int firstPopulatedSlot() const {
|
|
|
|
for (int i = 0; i < fCapacity; i++) {
|
2021-11-03 00:57:46 +00:00
|
|
|
if (fSlots[i].has_value()) {
|
2020-12-22 19:57:16 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increments an iterator's slot.
|
|
|
|
int nextPopulatedSlot(int currentSlot) const {
|
|
|
|
for (int i = currentSlot + 1; i < fCapacity; i++) {
|
2021-11-03 00:57:46 +00:00
|
|
|
if (fSlots[i].has_value()) {
|
2020-12-22 19:57:16 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads from an iterator's slot.
|
|
|
|
const T* slot(int i) const {
|
2021-11-03 00:57:46 +00:00
|
|
|
SkASSERT(fSlots[i].has_value());
|
|
|
|
return &*fSlots[i];
|
2020-12-22 19:57:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:46:05 +00:00
|
|
|
T* uncheckedSet(T&& val) {
|
2015-02-13 01:32:49 +00:00
|
|
|
const K& key = Traits::GetKey(val);
|
2021-05-07 20:58:51 +00:00
|
|
|
SkASSERT(key == key);
|
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];
|
2016-12-14 19:20:08 +00:00
|
|
|
if (s.empty()) {
|
2015-02-12 21:20:08 +00:00
|
|
|
// New entry.
|
2021-11-03 00:57:46 +00:00
|
|
|
s.emplace(std::move(val), hash);
|
2015-02-12 21:20:08 +00:00
|
|
|
fCount++;
|
2021-11-03 00:57:46 +00:00
|
|
|
return &*s;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
2021-11-03 00:57:46 +00:00
|
|
|
if (hash == s.hash && key == Traits::GetKey(*s)) {
|
2015-02-12 21:20:08 +00:00
|
|
|
// Overwrite previous entry.
|
2015-02-13 01:32:49 +00:00
|
|
|
// Note: this triggers extra copies when adding the same value repeatedly.
|
2021-11-03 00:57:46 +00:00
|
|
|
s.emplace(std::move(val), hash);
|
|
|
|
return &*s;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
2016-12-14 19:20:08 +00:00
|
|
|
|
|
|
|
index = this->next(index);
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
SkASSERT(false);
|
2016-12-14 19:20:08 +00:00
|
|
|
return nullptr;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 20:07:11 +00:00
|
|
|
void removeSlot(int index) {
|
|
|
|
fCount--;
|
|
|
|
|
|
|
|
// Rearrange elements to restore the invariants for linear probing.
|
|
|
|
for (;;) {
|
|
|
|
Slot& emptySlot = fSlots[index];
|
|
|
|
int emptyIndex = index;
|
|
|
|
int originalIndex;
|
|
|
|
// Look for an element that can be moved into the empty slot.
|
|
|
|
// If the empty slot is in between where an element landed, and its native slot, then
|
|
|
|
// move it to the empty slot. Don't move it if its native slot is in between where
|
|
|
|
// the element landed and the empty slot.
|
|
|
|
// [native] <= [empty] < [candidate] == GOOD, can move candidate to empty slot
|
|
|
|
// [empty] < [native] < [candidate] == BAD, need to leave candidate where it is
|
|
|
|
do {
|
|
|
|
index = this->next(index);
|
|
|
|
Slot& s = fSlots[index];
|
|
|
|
if (s.empty()) {
|
|
|
|
// We're done shuffling elements around. Clear the last empty slot.
|
2021-11-03 00:57:46 +00:00
|
|
|
emptySlot.reset();
|
2019-12-12 20:07:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
originalIndex = s.hash & (fCapacity - 1);
|
|
|
|
} while ((index <= originalIndex && originalIndex < emptyIndex)
|
|
|
|
|| (originalIndex < emptyIndex && emptyIndex < index)
|
|
|
|
|| (emptyIndex < index && index <= originalIndex));
|
|
|
|
// Move the element to the empty slot.
|
|
|
|
Slot& moveFrom = fSlots[index];
|
|
|
|
emptySlot = std::move(moveFrom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 19:20:08 +00:00
|
|
|
int next(int index) const {
|
|
|
|
index--;
|
|
|
|
if (index < 0) { index += fCapacity; }
|
|
|
|
return index;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 01:32:49 +00:00
|
|
|
static uint32_t Hash(const K& key) {
|
2019-06-23 16:50:51 +00:00
|
|
|
uint32_t hash = Traits::Hash(key) & 0xffffffff;
|
2016-12-14 19:20:08 +00:00
|
|
|
return hash ? hash : 1; // We reserve hash 0 to mark empty.
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Slot {
|
2020-10-08 15:01:00 +00:00
|
|
|
Slot() = default;
|
2021-11-03 00:57:46 +00:00
|
|
|
~Slot() { this->reset(); }
|
2016-12-14 19:20:08 +00:00
|
|
|
|
2021-11-03 00:57:46 +00:00
|
|
|
Slot(const Slot& that) { *this = that; }
|
|
|
|
Slot& operator=(const Slot& that) {
|
|
|
|
if (this == &that) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (hash) {
|
|
|
|
if (that.hash) {
|
|
|
|
val.storage = that.val.storage;
|
|
|
|
hash = that.hash;
|
|
|
|
} else {
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (that.hash) {
|
|
|
|
new (&val.storage) T(that.val.storage);
|
|
|
|
hash = that.hash;
|
|
|
|
} else {
|
|
|
|
// do nothing, no value on either side
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slot(Slot&& that) { *this = std::move(that); }
|
|
|
|
Slot& operator=(Slot&& that) {
|
|
|
|
if (this == &that) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (hash) {
|
|
|
|
if (that.hash) {
|
|
|
|
val.storage = std::move(that.val.storage);
|
|
|
|
hash = that.hash;
|
|
|
|
} else {
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (that.hash) {
|
|
|
|
new (&val.storage) T(std::move(that.val.storage));
|
|
|
|
hash = that.hash;
|
|
|
|
} else {
|
|
|
|
// do nothing, no value on either side
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() & { return val.storage; }
|
|
|
|
const T& operator*() const& { return val.storage; }
|
|
|
|
T&& operator*() && { return std::move(val.storage); }
|
|
|
|
const T&& operator*() const&& { return std::move(val.storage); }
|
|
|
|
|
|
|
|
Slot& emplace(T&& v, uint32_t h) {
|
|
|
|
this->reset();
|
|
|
|
new (&val.storage) T(std::move(v));
|
|
|
|
hash = h;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_value() const { return hash != 0; }
|
|
|
|
explicit operator bool() const { return this->has_value(); }
|
|
|
|
bool empty() const { return !this->has_value(); }
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
if (hash) {
|
|
|
|
val.storage.~T();
|
|
|
|
hash = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 13:53:56 +00:00
|
|
|
|
2020-10-08 15:01:00 +00:00
|
|
|
uint32_t hash = 0;
|
2021-11-03 00:57:46 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
union Storage {
|
|
|
|
T storage;
|
|
|
|
Storage() {}
|
|
|
|
~Storage() {}
|
|
|
|
} val;
|
2015-02-12 21:20:08 +00:00
|
|
|
};
|
|
|
|
|
2020-10-08 15:13:01 +00:00
|
|
|
int fCount = 0,
|
|
|
|
fCapacity = 0;
|
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>
|
2018-06-14 19:54:29 +00:00
|
|
|
class SkTHashMap {
|
2015-02-12 21:20:08 +00:00
|
|
|
public:
|
2022-03-03 21:57:22 +00:00
|
|
|
// Allow default construction and assignment.
|
|
|
|
SkTHashMap() = default;
|
|
|
|
|
|
|
|
SkTHashMap(SkTHashMap<K, V, HashK>&& that) = default;
|
|
|
|
SkTHashMap(const SkTHashMap<K, V, HashK>& that) = default;
|
|
|
|
|
|
|
|
SkTHashMap<K, V, HashK>& operator=(SkTHashMap<K, V, HashK>&& that) = default;
|
|
|
|
SkTHashMap<K, V, HashK>& operator=(const SkTHashMap<K, V, HashK>& that) = default;
|
|
|
|
|
|
|
|
// Construct with an initializer list of key-value pairs.
|
|
|
|
struct Pair : public std::pair<K, V> {
|
|
|
|
using std::pair<K, V>::pair;
|
|
|
|
static const K& GetKey(const Pair& p) { return p.first; }
|
|
|
|
static auto Hash(const K& key) { return HashK()(key); }
|
|
|
|
};
|
|
|
|
|
|
|
|
SkTHashMap(std::initializer_list<Pair> pairs) {
|
|
|
|
fTable.resize(pairs.size() * 5 / 3);
|
|
|
|
for (const Pair& p : pairs) {
|
|
|
|
fTable.set(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(); }
|
|
|
|
|
2022-03-04 18:23:53 +00:00
|
|
|
// Is empty?
|
|
|
|
bool empty() const { return fTable.count() == 0; }
|
|
|
|
|
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.
|
2016-12-15 20:13:13 +00:00
|
|
|
V* set(K key, V val) {
|
2016-12-13 17:46:05 +00:00
|
|
|
Pair* out = fTable.set({std::move(key), std::move(val)});
|
2020-12-22 19:57:16 +00:00
|
|
|
return &out->second;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is key/value entry in the table with this key, return a pointer to the value.
|
2016-12-14 19:20:08 +00:00
|
|
|
// 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)) {
|
2020-12-22 19:57:16 +00:00
|
|
|
return &p->second;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
2016-12-14 19:20:08 +00:00
|
|
|
return nullptr;
|
2015-02-12 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
sketch out structure for ops with immediates
Lots of x86 instructions can take their right hand side argument from
memory directly rather than a register. We can use this to avoid the
need to allocate a register for many constants.
The strategy in this CL is one of several I've been stewing over, the
simplest of those strategies I think. There are some trade offs
particularly on ARM; this naive ARM implementation means we'll load&op
every time, even though the load part of the operation can logically be
hoisted. From here on I'm going to just briefly enumerate a few other
approaches that allow the optimization on x86 and still allow the
immediate splats to hoist on ARM.
1) don't do it on ARM
A very simple approach is to simply not perform this optimization on
ARM. ARM has more vector registers than x86, and so register pressure
is lower there. We're going to end up with splatted constants in
registers anyway, so maybe just let that happen the normal way instead
of some roundabout complicated hack like I'll talk about in 2). The
only downside in my mind is that this approach would make high-level
program descriptions platform dependent, which isn't so bad, but it's
been nice to be able to compare and diff debug dumps.
2) split Op::splat up
The next less-simple approach to this problem could fix this by
splitting splats into two Ops internally, one inner Op::immediate that
guantees at least the constant is in memory and is compatible with
immediate-aware Ops like mul_f32_imm, and an outer Op::constant that
depends on that Op::immediate and further guarantees that constant has
been broadcast into a register to be compatible with non-immediate-aware
ops like div_f32. When building a program, immediate-aware ops would
peek for Op::constants as they do today for Op::splats, but instead of
embedding the immediate themselves, they'd replace their dependency with
the inner Op::immediate.
On x86 these new Ops would work just as advertised, with Op::immediate a
runtime no-op, Op::constant the usual vbroadcastss. On ARM
Op::immediate needs to go all the way and splat out a register to make
the constant compatible with immediate-aware ops, and the Op::constant
becomes a noop now instead. All this comes together to let the
Op::immediate splat hoist up out of the loop while still feeding
Op::mul_f32_imm and co. It's a rather complicated approach to solving
this issue, but I might want to explore it just to see how bad it is.
3) do it inside the x86 JIT
The conceptually best approach is to find a way to do this peepholing
only inside the JIT only on x86, avoiding the need for new
Op::mul_f32_imm and co. ARM and the interpreter don't benefit from this
peephole, so the x86 JIT is the logical owner of this optimization.
Finding a clean way to do this without too much disruption is the least
baked idea I've got here, though I think the most desirable long-term.
Cq-Include-Trybots: skia.primary:Test-Debian9-Clang-GCE-CPU-AVX2-x86_64-Debug-All-SK_USE_SKVM_BLITTER,Test-Debian9-Clang-GCE-CPU-AVX2-x86_64-Release-All-SK_USE_SKVM_BLITTER
Change-Id: Ie9c6336ed08b6fbeb89acf920a48a319f74f3643
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/254217
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Herb Derby <herb@google.com>
2019-11-12 15:07:23 +00:00
|
|
|
V& operator[](const K& key) {
|
|
|
|
if (V* val = this->find(key)) {
|
|
|
|
return *val;
|
|
|
|
}
|
|
|
|
return *this->set(key, V{});
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-22 19:57:16 +00:00
|
|
|
fTable.foreach([&fn](Pair* p){ fn(p->first, &p->second); });
|
2015-03-20 20:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-12-22 19:57:16 +00:00
|
|
|
fTable.foreach([&fn](const Pair& p){ fn(p.first, p.second); });
|
2015-03-20 20:48:42 +00:00
|
|
|
}
|
2015-02-12 21:20:08 +00:00
|
|
|
|
2020-12-22 19:57:16 +00:00
|
|
|
// Dereferencing an iterator gives back a key-value pair, suitable for structured binding.
|
|
|
|
using Iter = typename SkTHashTable<Pair, K>::template Iter<std::pair<K, V>>;
|
|
|
|
|
|
|
|
Iter begin() const {
|
|
|
|
return Iter::MakeBegin(&fTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iter end() const {
|
|
|
|
return Iter::MakeEnd(&fTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-12 21:20:08 +00:00
|
|
|
SkTHashTable<Pair, K> fTable;
|
|
|
|
};
|
|
|
|
|
2017-07-07 16:56:11 +00:00
|
|
|
// A set of T. T is treated as an ordinary copyable C++ type.
|
2015-10-15 19:23:01 +00:00
|
|
|
template <typename T, typename HashT = SkGoodHash>
|
2018-06-14 19:54:29 +00:00
|
|
|
class SkTHashSet {
|
2015-02-12 21:20:08 +00:00
|
|
|
public:
|
2022-03-03 21:57:22 +00:00
|
|
|
// Allow default construction and assignment.
|
|
|
|
SkTHashSet() = default;
|
|
|
|
|
|
|
|
SkTHashSet(SkTHashSet<T, HashT>&& that) = default;
|
|
|
|
SkTHashSet(const SkTHashSet<T, HashT>& that) = default;
|
|
|
|
|
|
|
|
SkTHashSet<T, HashT>& operator=(SkTHashSet<T, HashT>&& that) = default;
|
|
|
|
SkTHashSet<T, HashT>& operator=(const SkTHashSet<T, HashT>& that) = default;
|
|
|
|
|
|
|
|
// Construct with an initializer list of Ts.
|
|
|
|
SkTHashSet(std::initializer_list<T> vals) {
|
|
|
|
fTable.resize(vals.size() * 5 / 3);
|
|
|
|
for (const T& val : vals) {
|
|
|
|
fTable.set(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(); }
|
|
|
|
|
2019-08-26 18:55:07 +00:00
|
|
|
// Is empty?
|
|
|
|
bool empty() const { return fTable.count() == 0; }
|
|
|
|
|
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.
|
2016-12-15 20:13:13 +00:00
|
|
|
void add(T item) { fTable.set(std::move(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; }
|
2019-06-23 16:50:51 +00:00
|
|
|
static auto Hash(const T& item) { return HashT()(item); }
|
2015-02-12 21:20:08 +00:00
|
|
|
};
|
2020-12-22 19:57:16 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
using Iter = typename SkTHashTable<T, T, Traits>::template Iter<T>;
|
|
|
|
|
|
|
|
Iter begin() const {
|
|
|
|
return Iter::MakeBegin(&fTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iter end() const {
|
|
|
|
return Iter::MakeEnd(&fTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-12 21:20:08 +00:00
|
|
|
SkTHashTable<T, T, Traits> fTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif//SkTHash_DEFINED
|