add SkTHashTable::LookupOrNull()

This should help avoid confusion from T**.

Change-Id: I1851baa2a55714721fa935d234b6a4a1c6d6504f
Reviewed-on: https://skia-review.googlesource.com/c/182562
Commit-Queue: Mike Klein <mtklein@google.com>
Commit-Queue: Herb Derby <herb@google.com>
Auto-Submit: Mike Klein <mtklein@google.com>
Reviewed-by: Herb Derby <herb@google.com>
This commit is contained in:
Mike Klein 2019-01-09 13:20:57 -05:00 committed by Skia Commit-Bot
parent 97c7cf16de
commit 4284ec6ba5
2 changed files with 31 additions and 0 deletions

View File

@ -85,6 +85,15 @@ public:
return nullptr;
}
// 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;
}
// Remove the value with this key from the hash table.
void remove(const K& key) {
SkASSERT(this->find(key));

View File

@ -177,3 +177,25 @@ DEF_TEST(HashSetCopyCounter, r) {
// We allow copies for same-value adds for now.
REPORTER_ASSERT(r, globalCounter == 5);
}
DEF_TEST(HashFindOrNull, r) {
struct Entry {
int key = 0;
int val = 0;
};
struct HashTraits {
static int GetKey(const Entry* e) { return e->key; }
static uint32_t Hash(int key) { return key; }
};
SkTHashTable<Entry*, int, HashTraits> table;
REPORTER_ASSERT(r, nullptr == table.findOrNull(7));
Entry seven = { 7, 24 };
table.set(&seven);
REPORTER_ASSERT(r, &seven == table.findOrNull(7));
}