From d288eeb79abc08d09f535bfd5d64b623d26bd129 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 13 May 2021 13:39:23 +0200 Subject: BLI: support looking up a key stored in Map or VectorSet Sometimes it is useful to find the key that compares equal to a known key. Typically that happens when the key itself has additional data attached that is not part of its hash. Note that the returned key reference/pointer is const, because the caller must not change the key in a way that changes its hash or how it compares to other keys. --- source/blender/blenlib/tests/BLI_map_test.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'source/blender/blenlib/tests/BLI_map_test.cc') diff --git a/source/blender/blenlib/tests/BLI_map_test.cc b/source/blender/blenlib/tests/BLI_map_test.cc index 18be456bd20..679a10e9ce0 100644 --- a/source/blender/blenlib/tests/BLI_map_test.cc +++ b/source/blender/blenlib/tests/BLI_map_test.cc @@ -640,6 +640,19 @@ TEST(map, RemoveDuringIteration) EXPECT_EQ(map.lookup(3), 3); } +TEST(map, LookupKey) +{ + Map map; + map.add("a", 0); + map.add("b", 1); + map.add("c", 2); + EXPECT_EQ(map.lookup_key("a"), "a"); + EXPECT_EQ(map.lookup_key_as("c"), "c"); + EXPECT_EQ(map.lookup_key_ptr_as("d"), nullptr); + EXPECT_EQ(map.lookup_key_ptr_as("b")->size(), 1); + EXPECT_EQ(map.lookup_key_ptr("a"), map.lookup_key_ptr_as("a")); +} + /** * Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot. */ -- cgit v1.2.3