[Mesa-dev] [PATCH v2 07/11] util/set: helper to remove entry by key

Caio Marcelo de Oliveira Filho caio.oliveira at intel.com
Mon Jul 9 17:53:21 UTC 2018


v2: Add unit test. (Eric Anholt)
---
 src/util/set.c                  |  9 +++++++++
 src/util/set.h                  |  2 ++
 src/util/tests/set/set_test.cpp | 28 ++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/src/util/set.c b/src/util/set.c
index b2aa5ba13d5..feef96d16ea 100644
--- a/src/util/set.c
+++ b/src/util/set.c
@@ -383,6 +383,15 @@ _mesa_set_remove(struct set *ht, struct set_entry *entry)
    ht->deleted_entries++;
 }
 
+/**
+ * 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.
  *
diff --git a/src/util/set.h b/src/util/set.h
index 4db070a6f10..ffd19a798bd 100644
--- a/src/util/set.h
+++ b/src/util/set.h
@@ -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);
diff --git a/src/util/tests/set/set_test.cpp b/src/util/tests/set/set_test.cpp
index c0998560b50..a1eef0b3d98 100644
--- a/src/util/tests/set/set_test.cpp
+++ b/src/util/tests/set/set_test.cpp
@@ -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);
+}
-- 
2.18.0



More information about the mesa-dev mailing list