From 8a838e172f3f796b2d5d01cb51e05b37ae6f48f5 Mon Sep 17 00:00:00 2001 From: Connor Abbott Date: Wed, 27 Mar 2019 12:00:54 +0100 Subject: util/set: Add a _mesa_set_search_or_add() function Unlike _mesa_set_search_and_add(), it doesn't replace an entry if it's found, returning it instead. This is useful for nir_instr_set, where we have to know both the original original instruction and its equivalent. Reviewed-by: Eric Anholt Acked-by: Jason Ekstrand --- src/util/tests/set/set_test.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/util/tests/set/set_test.cpp') diff --git a/src/util/tests/set/set_test.cpp b/src/util/tests/set/set_test.cpp index a1eef0b3d98..0b4355af128 100644 --- a/src/util/tests/set/set_test.cpp +++ b/src/util/tests/set/set_test.cpp @@ -113,3 +113,36 @@ TEST(set, remove_key) _mesa_set_destroy(s, NULL); } + +static uint32_t hash_int(const void *p) +{ + int i = *(const int *)p; + return i; +} + +static bool cmp_int(const void *p1, const void *p2) +{ + int i1 = *(const int *)p1, i2 = *(const int *)p2; + return i1 == i2; +} + +TEST(set, search_or_add) +{ + struct set *s = _mesa_set_create(NULL, hash_int, cmp_int); + + int a = 10, b = 20, c = 20, d = 30; + + _mesa_set_add(s, &a); + _mesa_set_add(s, &b); + EXPECT_EQ(s->entries, 2); + + struct set_entry *entry = _mesa_set_search_or_add(s, &c); + EXPECT_EQ(entry->key, (void *)&b); + EXPECT_EQ(s->entries, 2); + + struct set_entry *entry3 = _mesa_set_search_or_add(s, &d); + EXPECT_EQ(entry3->key, &d); + EXPECT_EQ(s->entries, 3); + + _mesa_set_destroy(s, NULL); +} -- cgit v1.2.3