From 75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b Mon Sep 17 00:00:00 2001 From: grunfink Date: Sat, 23 Aug 2025 20:41:16 +0200 Subject: Added some more helping functions. --- xs_set.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'xs_set.h') diff --git a/xs_set.h b/xs_set.h index 3eaefdf..8946e42 100644 --- a/xs_set.h +++ b/xs_set.h @@ -14,6 +14,7 @@ typedef struct _xs_set { void xs_set_init(xs_set *s); xs_list *xs_set_result(xs_set *s); void xs_set_free(xs_set *s); +int xs_set_in(const xs_set *s, const xs_val *data); int xs_set_add(xs_set *s, const xs_val *data); @@ -60,7 +61,7 @@ static int _store_hash(xs_set *s, const char *data, int value) while (s->hash[(i = hash % s->elems)]) { /* get the pointer to the stored data */ - char *p = &s->list[s->hash[i]]; + const char *p = &s->list[s->hash[i]]; /* already here? */ if (memcmp(p, data, sz) == 0) @@ -79,6 +80,30 @@ static int _store_hash(xs_set *s, const char *data, int value) } +int xs_set_in(const xs_set *s, const xs_val *data) +/* returns 1 if the data is already in the set */ +{ + unsigned int hash, i; + int sz = xs_size(data); + + hash = xs_hash_func(data, sz); + + while (s->hash[(i = hash % s->elems)]) { + /* get the pointer to the stored data */ + const char *p = &s->list[s->hash[i]]; + + /* already here? */ + if (memcmp(p, data, sz) == 0) + return 1; + + /* try next value */ + hash++; + } + + return 0; +} + + int xs_set_add(xs_set *s, const xs_val *data) /* adds the data to the set */ /* returns: 1 if added, 0 if already there */ -- cgit v1.2.3