BLI: support mutable lookup from multi value map

This commit is contained in:
Jacques Lucke 2021-03-04 17:37:46 +01:00
parent d09b874244
commit bb1f02510b
2 changed files with 37 additions and 0 deletions

View File

@ -103,6 +103,22 @@ template<typename Key, typename Value> class MultiValueMap {
return {};
}
/**
* Get a mutable span to all the values that are stored for the given key.
*/
MutableSpan<Value> lookup(const Key &key)
{
return this->lookup_as(key);
}
template<typename ForwardKey> MutableSpan<Value> lookup_as(const ForwardKey &key)
{
Vector<Value> *vector = map_.lookup_ptr_as(key);
if (vector != nullptr) {
return vector->as_mutable_span();
}
return {};
}
/**
* Note: This signature will change when the implementation changes.
*/

View File

@ -29,6 +29,27 @@ TEST(multi_value_map, LookupExistant)
EXPECT_EQ(map.lookup(3)[0], 6);
}
TEST(multi_value_map, LookupMutable)
{
MultiValueMap<int, int> map;
map.add(1, 2);
map.add(4, 5);
map.add(4, 6);
map.add(6, 7);
MutableSpan<int> span = map.lookup(4);
EXPECT_EQ(span.size(), 2);
span[0] = 10;
span[1] = 20;
map.add(4, 5);
MutableSpan<int> new_span = map.lookup(4);
EXPECT_EQ(new_span.size(), 3);
EXPECT_EQ(new_span[0], 10);
EXPECT_EQ(new_span[1], 20);
EXPECT_EQ(new_span[2], 5);
}
TEST(multi_value_map, AddMultiple)
{
MultiValueMap<int, int> map;