commit:     3d6b64ef4c4dfd0fe2e008c7cc28a94904775e1e
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Jan 19 09:44:57 2020 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Jan 19 09:44:57 2020 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=3d6b64ef

libq/set: change interface of contains_set to return internal key

Allow to refer to the internal allocated key name, which can avoid
another duplicate in certain cases.

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

 libq/set.c | 11 ++++++-----
 libq/set.h |  3 +--
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/libq/set.c b/libq/set.c
index 4529c3a..ceb6f47 100644
--- a/libq/set.c
+++ b/libq/set.c
@@ -148,23 +148,24 @@ add_set_value(const char *name, void *ptr, set *q)
        return NULL;
 }
 
-/* returns whether s is in set */
-bool
+/* returns whether name is in set, and if so, the set-internal key
+ * representation (an internal copy of name made during addition) */
+const char *
 contains_set(const char *name, set *q)
 {
        unsigned int hash;
        int pos;
        set_elem *w;
-       bool found;
+       const char *found;
 
        hash = fnv1a32(name);
        pos = hash % _SET_HASH_SIZE;
 
-       found = false;
+       found = NULL;
        if (q->buckets[pos] != NULL) {
                for (w = q->buckets[pos]; w != NULL; w = w->next) {
                        if (w->hash == hash && strcmp(w->name, name) == 0) {
-                               found = true;
+                               found = w->name;
                                break;
                        }
                }

diff --git a/libq/set.h b/libq/set.h
index c65eb0f..5d53f95 100644
--- a/libq/set.h
+++ b/libq/set.h
@@ -7,7 +7,6 @@
 #define _SET_H 1
 
 #include <stdlib.h>
-#include <stdbool.h>
 #include <unistd.h>
 
 #include "xarray.h"
@@ -32,7 +31,7 @@ set *create_set(void);
 set *add_set(const char *name, set *q);
 set *add_set_unique(const char *name, set *q, bool *unique);
 void *add_set_value(const char *name, void *ptr, set *q);
-bool contains_set(const char *name, set *q);
+const char *contains_set(const char *name, set *q);
 void *get_set(const char *name, set *q);
 void *del_set(const char *s, set *q, bool *removed);
 size_t list_set(set *q, char ***l);

Reply via email to