Hash :
d04f9d06
Author :
Thomas de Grivel
Date :
2022-12-30T19:17:02
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
/* c3
* Copyright 2022 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers 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=_NAME$ TYPE=_TYPE$ */
#include <assert.h>
#include <stdlib.h>
#include "compare.h"
#include "_NAME$.h"
#include "set___NAME$.h"
#include "set_item___NAME$.h"
s_set_item___NAME$ *
set_add___NAME$ (s_set___NAME$ *set, const _TYPE$ *data)
{
uw hash;
assert(set);
assert(data);
hash = _NAME$_hash_uw(data);
return set_add_h___NAME$(set, data, hash);
}
s_set_item___NAME$ *
set_add_collision___NAME$ (s_set___NAME$ *set, const _TYPE$ *data, uw hash, s_set_item___NAME$ *item)
{
s_set_item___NAME$ *new_item;
new_item = set_item_new___NAME$(data, hash, item->next);
item->next = new_item;
set->count++;
set->collisions++;
return new_item;
}
s_set_item___NAME$ *
set_add_h___NAME$ (s_set___NAME$ *set, const _TYPE$ *data, uw hash)
{
uw h;
s_set_item___NAME$ *i;
assert(set);
assert(data);
if ((i = set_get_h___NAME$(set, data, hash)))
return i;
h = hash % set->max;
if ((i = set->items[h]))
return set_add_collision___NAME$(set, data, hash, i);
i = set_item_new___NAME$(data, hash, NULL);
set->items[h] = i;
set->count++;
return i;
}
void
set_clean___NAME$ (s_set___NAME$ *set)
{
uw i;
assert(set);
for (i = 0; i < set->max; i++) {
set_item_delete_all___NAME$(set->items[i]);
}
free(set->items);
}
s_set_item___NAME$ *
set_get___NAME$ (const s_set___NAME$ *set, const _TYPE$ *data)
{
uw hash;
assert(set);
assert(data);
hash = _NAME$_hash_uw(data);
return set_get_h___NAME$(set, data, hash);
}
s_set_item___NAME$ *
set_get_h___NAME$ (const s_set___NAME$ *set, const _TYPE$ *data, uw hash)
{
s_set_item___NAME$ *i;
assert(set);
assert(data);
i = set_get_hash___NAME$(set, hash);
while (i) {
if (compare__NAME$(&i->data, data) == 0)
return i;
i = set_get_hash_next___NAME$(i);
}
return NULL;
}
s_set_item___NAME$ *
set_get_hash___NAME$ (const s_set___NAME$ *set, uw hash)
{
uw h;
s_set_item___NAME$ *i;
assert(set);
h = hash % set->max;
i = set->items[h];
while (i && i->hash != hash)
i = i->next;
return i;
}
s_set_item___NAME$ *
set_get_hash_next___NAME$ (const s_set_item___NAME$ *item)
{
s_set_item___NAME$ *i;
assert(item);
i = item->next;
while (i && i->hash != item->hash)
i = i->next;
return i;
}
s_set___NAME$ *
set_init___NAME$ (s_set___NAME$ *set, uw max)
{
assert(set);
assert(max > 0);
set->max = max;
set->items = calloc(max, sizeof(s_set_item___NAME$ *));
set->count = 0;
set->collisions = 0;
return set;
}
e_bool
set_remove___NAME$ (s_set___NAME$ *set, const _TYPE$ *data)
{
s_set_item___NAME$ *item;
if ((item = set_get___NAME$(set, data)))
return set_remove_item___NAME$(set, item);
return false;
}
e_bool
set_remove_item___NAME$ (s_set___NAME$ *set, s_set_item___NAME$ *item)
{
sw h;
s_set_item___NAME$ *i;
s_set_item___NAME$ **j;
s_set_item___NAME$ *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___NAME$(i);
*j = k;
set->count--;
if (set->items[h])
set->collisions--;
return true;
}
s_set___NAME$ *
set_resize___NAME$ (s_set___NAME$ *set, uw max)
{
uw i;
s_set_item___NAME$ *item;
s_set___NAME$ n;
if (set->max == max)
return set;
set_init___NAME$(&n, max);
for (i = 0; i < set->max; i++) {
item = set->items[i];
while (item) {
set_add_h___NAME$(&n, &item->data, item->hash);
item = item->next;
}
}
set_clean___NAME$(set);
set->max = n.max;
set->items = n.items;
set->count = n.count;
set->collisions = n.collisions;
return set;
}