tint/utils: Add Hashmap::Keys(), Hashmap::Values()

Change-Id: If51cb3f42e50db842ecd006bcfbbbd060957e650
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112281
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-11-29 20:01:42 +00:00 committed by Dawn LUCI CQ
parent 05f3ebfc68
commit 028092a843
2 changed files with 35 additions and 0 deletions

View File

@ -182,6 +182,30 @@ class Hashmap : public HashmapBase<KEY, VALUE, N, HASH, EQUAL> {
/// @returns a reference to the entry that is equal to the given value.
ConstReference Find(const Key& key) const { return ConstReference(*this, key); }
/// @returns the keys of the map as a vector.
/// @note the order of the returned vector is non-deterministic between compilers.
template <size_t N2 = N>
Vector<Key, N2> Keys() const {
Vector<Key, N2> out;
out.Reserve(this->Count());
for (auto it : *this) {
out.Push(it.key);
}
return out;
}
/// @returns the values of the map as a vector
/// @note the order of the returned vector is non-deterministic between compilers.
template <size_t N2 = N>
Vector<Value, N2> Values() const {
Vector<Value, N2> out;
out.Reserve(this->Count());
for (auto it : *this) {
out.Push(it.value);
}
return out;
}
private:
Value* Lookup(const Key& key) {
if (auto [found, index] = this->IndexOf(key); found) {

View File

@ -182,6 +182,17 @@ TEST(Hashmap, MutableIterator) {
Entry{3, "three!"}, Entry{4, "four!"}));
}
TEST(Hashmap, KeysValues) {
using Map = Hashmap<int, std::string, 8>;
Map map;
map.Add(1, "one");
map.Add(4, "four");
map.Add(3, "three");
map.Add(2, "two");
EXPECT_THAT(map.Keys(), testing::UnorderedElementsAre(1, 2, 3, 4));
EXPECT_THAT(map.Values(), testing::UnorderedElementsAre("one", "two", "three", "four"));
}
TEST(Hashmap, AddMany) {
Hashmap<int, std::string, 8> map;
for (size_t i = 0; i < kPrimes.size(); i++) {