proj/portage-utils:master commit in: libq/

"Fabian Groffen" <[email protected]>
Newsgroups gmane.linux.gentoo.cvs
Message-ID <1782055680.30478cabeba02f808b6399c75577e4d23017c18c.grobian@gentoo>
commit:     30478cabeba02f808b6399c75577e4d23017c18c
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Jun 21 15:28:00 2026 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Jun 21 15:28:00 2026 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=30478cab

libq/set: add set_has_intersection

The set_intersect function doesn't exist (yet) but this function is
cheaper in that it returns immediately from the first match between the
two input sets.

Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/set.c | 39 +++++++++++++++++++++++++++++++++++++++
 libq/set.h |  2 ++
 2 files changed, 41 insertions(+)

diff --git a/libq/set.c b/libq/set.c
index 96b1d597..b77095a5 100644
--- a/libq/set.c
+++ b/libq/set.c
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <ctype.h>
 #include <xalloc.h>
@@ -320,6 +321,44 @@ void *set_delete
   return ret;
 }
 
+/* this is a cheap version of intersect that just returns whether there
+ * is at least one element common in both sets */
+bool set_has_intersection
+(
+  set_t *l,
+  set_t *r
+)
+{
+  set_t      *s;
+  set_elem_t *w1;
+  set_elem_t *w2;
+  size_t      i;
+
+  /* find smallest set, assign to s, let l be largest */
+  if (l->len < r->len)
+  {
+    s = l;
+    l = r;
+  }
+  else
+  {
+    s = r;
+  }
+
+  for (i = 0; i < _SET_HASH_SIZE; i++)
+  {
+    for (w1 = s->buckets[i]; w1 != NULL; w1 = w1->next)
+    {
+      for (w2 = l->buckets[i]; w2 != NULL; w2 = w1->next)
+        if (w1->hash == w2->hash &&
+            strcmp(w1->name, w2->name) == 0)
+          return true;
+    }
+  }
+
+  return false;
+}
+
 size_t set_size
 (
   set_t *q

diff --git a/libq/set.h b/libq/set.h
index 5df23b72..4fde8ec7 100644
--- a/libq/set.h
+++ b/libq/set.h
@@ -7,6 +7,7 @@
 #define _SET_H 1
 
 #include <stdlib.h>
+#include <stdbool.h>
 #include <unistd.h>
 
 #include "array.h"
@@ -22,6 +23,7 @@ set_t      *set_clone(set_t *s);
 const char *set_get(set_t *s, const char *key);
 void       *set_delete(set_t *s, const char *key, bool *removed);
 #define     set_keys(S)        hash_keys((hash_t *)S)
+bool        set_has_intersection(set_t *l, set_t *r);
 size_t      set_size(set_t *s);
 void        set_clear(set_t *s);
 void        set_free(set_t *s);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.