util/set: helper to remove entry by key
authorCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Mon, 25 Jun 2018 20:42:22 +0000 (13:42 -0700)
committerRafael Antognolli <rafael.antognolli@intel.com>
Thu, 12 Jul 2018 21:03:51 +0000 (14:03 -0700)
v2: Add unit test. (Eric Anholt)

Reviewed-by: Eric Anholt <eric@anholt.net>
src/util/set.c
src/util/set.h
src/util/tests/set/set_test.cpp

index b2aa5ba..feef96d 100644 (file)
@@ -384,6 +384,15 @@ _mesa_set_remove(struct set *ht, struct set_entry *entry)
 }
 
 /**
+ * Removes the entry with the corresponding key, if exists.
+ */
+void
+_mesa_set_remove_key(struct set *set, const void *key)
+{
+   _mesa_set_remove(set, _mesa_set_search(set, key));
+}
+
+/**
  * This function is an iterator over the hash table.
  *
  * Pass in NULL for the first entry, as in the start of a for loop.  Note that
index 4db070a..ffd19a7 100644 (file)
@@ -81,6 +81,8 @@ _mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
 
 void
 _mesa_set_remove(struct set *set, struct set_entry *entry);
+void
+_mesa_set_remove_key(struct set *set, const void *key);
 
 struct set_entry *
 _mesa_set_next_entry(const struct set *set, struct set_entry *entry);
index c099856..a1eef0b 100644 (file)
@@ -85,3 +85,31 @@ TEST(set, clone)
    _mesa_set_destroy(s, NULL);
    _mesa_set_destroy(clone, NULL);
 }
+
+TEST(set, remove_key)
+{
+   struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer,
+                                    _mesa_key_pointer_equal);
+
+   const void *a = (const void *)10;
+   const void *b = (const void *)20;
+   const void *c = (const void *)30;
+
+   _mesa_set_add(s, a);
+   _mesa_set_add(s, b);
+   EXPECT_EQ(s->entries, 2);
+
+   /* Remove existing key. */
+   _mesa_set_remove_key(s, a);
+   EXPECT_EQ(s->entries, 1);
+   EXPECT_FALSE(_mesa_set_search(s, a));
+   EXPECT_TRUE(_mesa_set_search(s, b));
+
+   /* Remove non-existing key. */
+   _mesa_set_remove_key(s, c);
+   EXPECT_EQ(s->entries, 1);
+   EXPECT_FALSE(_mesa_set_search(s, a));
+   EXPECT_TRUE(_mesa_set_search(s, b));
+
+   _mesa_set_destroy(s, NULL);
+}