Branch
Hash :
c78e5bd0
Author :
Thomas de Grivel
Date :
2025-09-04T18:34:55
all tests pass without dump
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/* kc3
* Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
/* Gen from set.c.in NAME=uw TYPE=uw */
#include "alloc.h"
#include "assert.h"
#include "compare.h"
#include "config.h"
#include "uw.h"
#include "set__uw.h"
#include "set_item__uw.h"
s_set_item__uw *
set_add__uw (s_set__uw *set, uw data)
{
uw hash;
assert(set);
assert(data);
if (! uw_hash_uw(data, &hash)) {
err_puts("set_add__uw: uw_hash_uw");
assert(! "set_add__uw: uw_hash_uw");
return NULL;
}
return set_add_h__uw(set, data, hash);
}
s_set_item__uw *
set_add_collision__uw
(s_set__uw *set,
uw data,
uw hash,
s_set_item__uw *item)
{
s_set_item__uw *new_item;
new_item = set_item_new__uw(data, hash, item->next);
item->next = new_item;
set->count++;
set->collisions++;
return new_item;
}
s_set_item__uw *
set_add_h__uw (s_set__uw *set, uw data, uw hash)
{
uw h;
s_set_item__uw *item;
assert(set);
assert(data);
if ((item = set_get_h__uw(set, data, hash)))
return item;
h = hash % set->max;
if ((item = set->items[h]))
return set_add_collision__uw(set, data, hash, item);
if (! (item = set_item_new__uw(data, hash, NULL))) {
err_puts("set_add_h__uw: set_item_new__uw");
assert(! "set_add_h__uw: set_item_new__uw");
return NULL;
}
set->items[h] = item;
set->count++;
return item;
}
void
set_clean__uw (s_set__uw *set)
{
uw i;
assert(set);
for (i = 0; i < set->max; i++) {
set_item_delete_all__uw(set->items[i]);
}
free(set->items);
}
s_set_item__uw *
set_get__uw (const s_set__uw *set, const uw data)
{
uw hash;
assert(set);
assert(data);
if (! uw_hash_uw(data, &hash))
return NULL;
return set_get_h__uw(set, data, hash);
}
s_set_item__uw *
set_get_h__uw
(const s_set__uw *set,
const uw data,
uw hash)
{
s_set_item__uw *item;
assert(set);
assert(data);
item = set_get_hash__uw(set, hash);
while (item) {
if (! compare_uw(_Generic(item->data,
f32: item->data,
f64: item->data,
f128: item->data,
s8: item->data,
s16: item->data,
s32: item->data,
s64: item->data,
#if HAVE_GENERIC_SW_UW
sw: item->data,
#endif
u8: item->data,
u16: item->data,
u32: item->data,
u64: item->data,
#if HAVE_GENERIC_SW_UW
uw: item->data,
#endif
default: &item->data),
data))
return item;
item = set_get_hash_next__uw(item);
}
return NULL;
}
s_set_item__uw *
set_get_hash__uw (const s_set__uw *set, uw hash)
{
uw h;
s_set_item__uw *item;
assert(set);
h = hash % set->max;
item = set->items[h];
while (item && item->hash != hash)
item = item->next;
return item;
}
s_set_item__uw *
set_get_hash_next__uw (const s_set_item__uw *item)
{
s_set_item__uw *i;
uw hash;
assert(item);
hash = item->hash;
i = item->next;
while (i && i->hash != hash)
i = i->next;
return i;
}
bool *
set_has__uw
(const s_set__uw *set,
const uw data,
bool *dest)
{
uw hash;
assert(set);
assert(dest);
if (! uw_hash_uw(data, &hash))
return NULL;
if (! set_get_h__uw(set, data, hash)) {
*dest = false;
return dest;
}
*dest = true;
return dest;
}
s_set__uw *
set_init__uw
(s_set__uw *set,
uw max)
{
s_set__uw tmp = {0};
assert(set);
assert(max > 0);
tmp.max = max;
tmp.items = alloc(max * sizeof(s_set_item__uw *));
if (! tmp.items)
return NULL;
tmp.count = 0;
tmp.collisions = 0;
*set = tmp;
return set;
}
bool
set_remove__uw (s_set__uw *set, const uw data)
{
s_set_item__uw *item;
if ((item = set_get__uw(set, data)))
return set_remove_item__uw(set, item);
return false;
}
void
set_remove_all__uw (s_set__uw *set)
{
uw i = 0;
assert(set);
while (i < set->max) {
set_item_delete_all__uw(set->items[i]);
i++;
}
}
bool
set_remove_item__uw (s_set__uw *set, s_set_item__uw *item)
{
sw h;
s_set_item__uw *i;
s_set_item__uw **j;
s_set_item__uw *k;
assert(set);
if (! item)
return false;
h = item->hash % set->max;
j = set->items + h;
while (*j && *j != item)
j = &(*j)->next;
if (!*j)
return false;
i = *j;
k = i->next;
set_item_delete__uw(i);
*j = k;
set->count--;
if (set->items[h])
set->collisions--;
return true;
}
s_set__uw *
set_resize__uw (s_set__uw *set, uw max)
{
uw i;
s_set_item__uw *item;
s_set__uw n;
if (set->max == max)
return set;
set_init__uw(&n, max);
for (i = 0; i < set->max; i++) {
item = set->items[i];
while (item) {
set_add_h__uw(&n, _Generic(item->data,
f32: item->data,
f64: item->data,
f128: item->data,
s8: item->data,
s16: item->data,
s32: item->data,
s64: item->data,
#if HAVE_GENERIC_SW_UW
sw: item->data,
#endif
u8: item->data,
u16: item->data,
u32: item->data,
u64: item->data,
#if HAVE_GENERIC_SW_UW
uw: item->data,
#endif
default: &item->data),
item->hash);
item = item->next;
}
}
set_clean__uw(set);
set->max = n.max;
set->items = n.items;
set->count = n.count;
set->collisions = n.collisions;
return set;
}