-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
291 lines (219 loc) · 6.6 KB
/
table.c
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
283
284
285
286
287
288
289
290
291
/*
* table.c - Hash table
* Copyright (c) 2020 Nicholas West, Hantao Cui, CURENT, et. al.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided "as is" and the author disclaims all
* warranties with regard to this software including all implied warranties
* of merchantability and fitness. In no event shall the author be liable
* for any special, direct, indirect, or consequential damages or any
* damages whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action, arising
* out of or in connection with the use or performance of this software.
*/
#include <stdlib.h>
#include "table.h"
enum dime_table_elem_use {
ELEM_FREE,
ELEM_OCCUPIED,
ELEM_REMOVED
};
/* Maximum ratio of used buckets to total buckets; 1 / 2 */
static const double MAX_LOAD_FACTOR = 0.5;
/* Maximum ratio of hash collisions to used buckets; φ / 8 */
static const double MAX_OVERFLOW_FACTOR = 0.20225424859373685602;
static void dime_table_relocate(dime_table_t *tbl, size_t i0) {
size_t i, k;
dime_table_elem_t tmp;
if (tbl->arr[i0].use != ELEM_OCCUPIED) {
tbl->arr[i0].use = ELEM_FREE;
return;
}
i = tbl->arr[i0].hash & (tbl->cap - 1);
k = 1;
if (i == i0) {
return;
}
tmp = tbl->arr[i0];
tbl->arr[i0].use = ELEM_FREE;
while (dime_table_relocate(tbl, i), tbl->arr[i].use == ELEM_OCCUPIED) {
i = (i + k) & (tbl->cap - 1);
k++;
}
tbl->arr[i] = tmp;
if (k > 1) {
tbl->ncollisions++;
}
}
static int dime_table_grow(dime_table_t *tbl) {
dime_table_elem_t *arr;
size_t cap;
cap = tbl->cap;
arr = realloc(tbl->arr, (cap << 1) * sizeof(dime_table_elem_t));
if (arr == NULL) {
return 0;
}
tbl->arr = arr;
tbl->cap <<= 1;
tbl->ncollisions = 0;
tbl->nfree = tbl->cap - tbl->len;
for (size_t i = cap; i < tbl->cap; i++) {
tbl->arr[i].use = ELEM_FREE;
}
for (size_t i = cap; i > 0; i--) {
dime_table_relocate(tbl, i - 1);
}
return 1;
}
int dime_table_init(dime_table_t *tbl, int (*cmp_f)(const void *, const void *), uint64_t (*hash_f)(const void *)) {
tbl->cap = 32;
tbl->arr = malloc(tbl->cap * sizeof(dime_table_elem_t));
if (tbl->arr == NULL) {
return -1;
}
tbl->len = 0;
tbl->cmp_f = cmp_f;
tbl->hash_f = hash_f;
for (size_t i = 0; i < tbl->cap; i++) {
tbl->arr[i].use = ELEM_FREE;
}
tbl->ncollisions = 0;
tbl->nfree = tbl->cap;
return 0;
}
void dime_table_destroy(dime_table_t *tbl) {
free(tbl->arr);
}
int dime_table_insert(dime_table_t *tbl, const void *key, void *val) {
if (tbl->len >= tbl->cap) {
if (!dime_table_grow(tbl)) {
return -1;
}
} else if ((double)tbl->len > MAX_LOAD_FACTOR * tbl->cap) {
dime_table_grow(tbl);
}
size_t i, k;
uint64_t hash;
hash = tbl->hash_f(key);
i = hash & (tbl->cap - 1);
k = 1;
while (tbl->arr[i].use == ELEM_OCCUPIED) {
if (tbl->cmp_f(key, tbl->arr[i].key) == 0) {
return -1;
}
i = (i + k) & (tbl->cap - 1);
k++;
}
int prevuse = tbl->arr[i].use;
tbl->arr[i].use = ELEM_OCCUPIED;
tbl->arr[i].hash = hash;
tbl->arr[i].key = key;
tbl->arr[i].val = val;
tbl->len++;
if (k > 1) {
tbl->ncollisions++;
if ((double)tbl->ncollisions > MAX_OVERFLOW_FACTOR * tbl->len) {
dime_table_grow(tbl);
}
}
if (prevuse == ELEM_FREE) {
tbl->nfree--;
if ((double)tbl->nfree < MAX_LOAD_FACTOR * tbl->cap) {
tbl->nfree = tbl->cap - tbl->len;
for (i = 0; i < tbl->cap; i++) {
dime_table_relocate(tbl, i);
}
}
}
return 0;
}
void *dime_table_search(dime_table_t *tbl, const void *key) {
size_t i, k, first, firstavail;
i = tbl->hash_f(key) & (tbl->cap - 1);
k = 1;
first = i;
firstavail = ((size_t)-1);
while (tbl->arr[i].use != ELEM_FREE) {
if (tbl->arr[i].use == ELEM_OCCUPIED) {
int cmp = tbl->cmp_f(key, tbl->arr[i].key);
if (cmp == 0) {
if (firstavail != ((size_t)-1)) {
tbl->arr[firstavail] = tbl->arr[i];
tbl->arr[i].use = ELEM_REMOVED;
if (first == firstavail) {
tbl->ncollisions--;
}
i = firstavail;
}
return tbl->arr[i].val;
}
} else if (firstavail == ((size_t)-1)) {
firstavail = i;
}
i = (i + k) & (tbl->cap - 1);
k++;
}
return NULL;
}
const void *dime_table_search_r(const dime_table_t *tbl, const void *key) {
size_t i, k;
i = tbl->hash_f(key) & (tbl->cap - 1);
k = 1;
while (tbl->arr[i].use != ELEM_FREE) {
if (tbl->arr[i].use == ELEM_OCCUPIED) {
int cmp = tbl->cmp_f(key, tbl->arr[i].key);
if (cmp == 0) {
return tbl->arr[i].val;
}
}
i = (i + k) & (tbl->cap - 1);
k++;
}
return NULL;
}
void *dime_table_remove(dime_table_t *tbl, const void *key) {
size_t i, k;
i = tbl->hash_f(key) & (tbl->cap - 1);
k = 1;
while (tbl->arr[i].use != ELEM_FREE) {
if (tbl->arr[i].use == ELEM_OCCUPIED) {
int cmp = tbl->cmp_f(key, tbl->arr[i].key);
if (cmp == 0) {
tbl->arr[i].use = ELEM_REMOVED;
tbl->len--;
return tbl->arr[i].val;
}
}
i = (i + k) & (tbl->cap - 1);
k++;
}
return NULL;
}
size_t dime_table_len(const dime_table_t *tbl) {
return tbl->len;
}
void dime_table_iter_init(dime_table_iter_t *it, dime_table_t *tbl) {
it->tbl = tbl;
it->i = (size_t)-1;
}
int dime_table_iter_next(dime_table_iter_t *it) {
do {
it->i++;
} while (it->i < it->tbl->cap && it->tbl->arr[it->i].use != ELEM_OCCUPIED);
if (it->i == it->tbl->cap) {
return 0;
}
it->key = it->tbl->arr[it->i].key;
it->val = it->tbl->arr[it->i].val;
return 1;
}
void dime_table_apply(dime_table_t *tbl, int(*f)(const void *, void *, void *), void *p) {
for (size_t i = 0; i < tbl->cap; i++) {
if (tbl->arr[i].use == ELEM_OCCUPIED && !f(tbl->arr[i].key, tbl->arr[i].val, p)) {
break;
}
}
}