summaryrefslogtreecommitdiff
path: root/xs_set.h
diff options
context:
space:
mode:
authorGravatar grunfink2025-08-23 20:41:16 +0200
committerGravatar grunfink2025-08-23 20:41:16 +0200
commit75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b (patch)
treee8988779f9a6ac8d93695ad0e6adbf7b2c3aac29 /xs_set.h
parentEnsure the tag is a string in server_get_handler(). (diff)
downloadpenes-snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.tar.gz
penes-snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.tar.xz
penes-snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.zip
Added some more helping functions.
Diffstat (limited to '')
-rw-r--r--xs_set.h27
1 files changed, 26 insertions, 1 deletions
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 {
14void xs_set_init(xs_set *s); 14void xs_set_init(xs_set *s);
15xs_list *xs_set_result(xs_set *s); 15xs_list *xs_set_result(xs_set *s);
16void xs_set_free(xs_set *s); 16void xs_set_free(xs_set *s);
17int xs_set_in(const xs_set *s, const xs_val *data);
17int xs_set_add(xs_set *s, const xs_val *data); 18int xs_set_add(xs_set *s, const xs_val *data);
18 19
19 20
@@ -60,7 +61,7 @@ static int _store_hash(xs_set *s, const char *data, int value)
60 61
61 while (s->hash[(i = hash % s->elems)]) { 62 while (s->hash[(i = hash % s->elems)]) {
62 /* get the pointer to the stored data */ 63 /* get the pointer to the stored data */
63 char *p = &s->list[s->hash[i]]; 64 const char *p = &s->list[s->hash[i]];
64 65
65 /* already here? */ 66 /* already here? */
66 if (memcmp(p, data, sz) == 0) 67 if (memcmp(p, data, sz) == 0)
@@ -79,6 +80,30 @@ static int _store_hash(xs_set *s, const char *data, int value)
79} 80}
80 81
81 82
83int xs_set_in(const xs_set *s, const xs_val *data)
84/* returns 1 if the data is already in the set */
85{
86 unsigned int hash, i;
87 int sz = xs_size(data);
88
89 hash = xs_hash_func(data, sz);
90
91 while (s->hash[(i = hash % s->elems)]) {
92 /* get the pointer to the stored data */
93 const char *p = &s->list[s->hash[i]];
94
95 /* already here? */
96 if (memcmp(p, data, sz) == 0)
97 return 1;
98
99 /* try next value */
100 hash++;
101 }
102
103 return 0;
104}
105
106
82int xs_set_add(xs_set *s, const xs_val *data) 107int xs_set_add(xs_set *s, const xs_val *data)
83/* adds the data to the set */ 108/* adds the data to the set */
84/* returns: 1 if added, 0 if already there */ 109/* returns: 1 if added, 0 if already there */