diff options
| author | 2025-08-23 20:41:16 +0200 | |
|---|---|---|
| committer | 2025-08-23 20:41:16 +0200 | |
| commit | 75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b (patch) | |
| tree | e8988779f9a6ac8d93695ad0e6adbf7b2c3aac29 /xs_set.h | |
| parent | Ensure the tag is a string in server_get_handler(). (diff) | |
| download | snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.tar.gz snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.tar.xz snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.zip | |
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 { | |||
| 14 | void xs_set_init(xs_set *s); | 14 | void xs_set_init(xs_set *s); |
| 15 | xs_list *xs_set_result(xs_set *s); | 15 | xs_list *xs_set_result(xs_set *s); |
| 16 | void xs_set_free(xs_set *s); | 16 | void xs_set_free(xs_set *s); |
| 17 | int xs_set_in(const xs_set *s, const xs_val *data); | ||
| 17 | int xs_set_add(xs_set *s, const xs_val *data); | 18 | int 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 | ||
| 83 | int 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 | |||
| 82 | int xs_set_add(xs_set *s, const xs_val *data) | 107 | int 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 */ |