diff options
| author | grunfink <grunfink@comam.es> | 2025-08-23 20:41:16 +0200 |
|---|---|---|
| committer | grunfink <grunfink@comam.es> | 2025-08-23 20:41:16 +0200 |
| commit | 75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b (patch) | |
| tree | e8988779f9a6ac8d93695ad0e6adbf7b2c3aac29 /xs_set.h | |
| parent | fee442dbf292856e6a8f6541cbe82e0daf721ac5 (diff) | |
Added some more helping functions.
Diffstat (limited to 'xs_set.h')
| -rw-r--r-- | xs_set.h | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -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 */ |