util/set: add _mesa_set_intersects
authorKarol Herbst <kherbst@redhat.com>
Fri, 15 May 2020 09:11:13 +0000 (11:11 +0200)
committerMarge Bot <eric+marge@anholt.net>
Fri, 14 Aug 2020 20:35:36 +0000 (20:35 +0000)
v2 (Jason Ekstrand): add asserts and iterate over smaller set

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2401>

src/util/set.c
src/util/set.h

index ffe0fe808eadd8d24ec45d16b423b01b66159fdc..a9012b4e6d4c6407cbf0d2a70a77d30b32feda58 100644 (file)
@@ -570,3 +570,23 @@ _mesa_pointer_set_create(void *mem_ctx)
    return _mesa_set_create(mem_ctx, _mesa_hash_pointer,
                            _mesa_key_pointer_equal);
 }
    return _mesa_set_create(mem_ctx, _mesa_hash_pointer,
                            _mesa_key_pointer_equal);
 }
+
+bool
+_mesa_set_intersects(struct set *a, struct set *b)
+{
+   assert(a->key_hash_function == b->key_hash_function);
+   assert(a->key_equals_function == b->key_equals_function);
+
+   /* iterate over the set with less entries */
+   if (b->entries < a->entries) {
+      struct set *tmp = a;
+      a = b;
+      b = tmp;
+   }
+
+   set_foreach(a, entry) {
+      if (_mesa_set_search_pre_hashed(b, entry->hash, entry->key))
+         return true;
+   }
+   return false;
+}
index 55857aca7ab4a4f9ac899eb4114af12ae2bef7e8..2e0a2d66f851450d1dd5a4fee7adf43478538790 100644 (file)
@@ -110,6 +110,9 @@ _mesa_set_random_entry(struct set *set,
 struct set *
 _mesa_pointer_set_create(void *mem_ctx);
 
 struct set *
 _mesa_pointer_set_create(void *mem_ctx);
 
+bool
+_mesa_set_intersects(struct set *a, struct set *b);
+
 /**
  * This foreach function is safe against deletion, but not against
  * insertion (which may rehash the set, making entry a dangling
 /**
  * This foreach function is safe against deletion, but not against
  * insertion (which may rehash the set, making entry a dangling