-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.c
234 lines (220 loc) · 5.77 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
#include "table.h"
#include "mem.h"
#include "assert.h"
#include <limits.h>
#include <stddef.h>
#define T Table_T
#define LOAD_FACTOR (3)
struct T {
int size;
int (*cmp)(const void *x, const void *y);
unsigned (*hash)(const void *key);
int length;
unsigned timestamp;
struct binding {
struct binding *link;
const void *key;
void *value;
} **buckets;
};
static int cmpatom(const void *x, const void *y)
{
return x != y;
}
static unsigned hashatom(const void *key)
{
return (unsigned long)key>>2;
}
/* Return a prime not bigger than hints. */
static int getPrime(int hints)
{
static int primes[] = {47, 91, 181, 509, 509, 1021, 2053, 4093,
8191, 16381, 32771, 65521, INT_MAX};
int i;
for (i = 1; primes[i] < hints; i++)
;
return primes[i - 1];
}
T Table_new(int hints,
int cmp(const void *, const void *),
unsigned hash(const void *))
{
T table;
int i, size;
assert(hints >= 0);
size = getPrime(hints);
table = ALLOC(sizeof (*table) +
size * sizeof(table->buckets[0]));
table->size = size;
table->cmp = cmp ? cmp : cmpatom;
table->hash = hash ? hash : hashatom;
table->buckets = (struct binding **)(table + 1);
for (i = 0; i < table->size; i++)
table->buckets[i] = NULL;
table->length = 0;
table->timestamp = 0;
return table;
}
void *Table_get(T table, const void *key)
{
int i;
struct binding * p;
assert(table);
assert(key);
i = (*table->hash)(key) % table->size;
for (p = table->buckets[i]; p; p = p->link) {
if ((*table->cmp)(key, p->key) == 0)
break;
}
return p ? p->value : NULL;
}
/* When is table is overloading(Table_loading() > 4)
* We can extend its size.
* */
void Table_extend(T *table)
{
int i, hash;
int newsize = getPrime(LOAD_FACTOR * (*table)->size);
T newtab, tmp;
struct binding *p, *q;
/* If the table is too large, no need to extend */
if (newsize == (*table)->size)
return;
newtab = ALLOC(sizeof(*newtab) +
newsize * sizeof(newtab->buckets[0]));
if (newtab == NULL)
return;
/* Initalize the new table */
newtab->size = newsize;
newtab->hash = (*table)->hash;
newtab->cmp = (*table)->cmp;
newtab->length = (*table)->length;
newtab->timestamp = (*table)->timestamp;
newtab->buckets = (struct binding **)(newtab + 1);
for (i = 0; i < newsize; i++)
newtab->buckets[i] = NULL;
/* Rehash the elements into the new table */
for (i = 0; i < (*table)->size; i++) {
for (p = (*table)->buckets[i]; p; p = q) {
hash = (*((*table)->hash))(p->key) % newtab->size;
q = p->link;
p->link = newtab->buckets[hash];
newtab->buckets[hash] = p;
}
}
/* Free the old table */
tmp = (*table);
(*table) = newtab;
FREE(tmp);
}
/* Return the ratio of length and size */
int Table_loading(T table)
{
assert(table);
return table->length / table->size;
}
/*
* If the key is not exists, then put a new key-value pair
* into the table.Othewise, renew the value and return the
* previous value.
* */
void *Table_put(T table, const void *key, void *value)
{
int i;
struct binding *p;
void *prev;
assert(table);
assert(key);
i = (*table->hash)(key) % table->size;
for (p = table->buckets[i]; p; p = p->link) {
if ((*table->cmp)(key, p->key) == 0)
break;
}
if (p == NULL) {
NEW(p);
p->key = key;
p->link = table->buckets[i];
table->buckets[i] = p;
table->length++;
prev = NULL;
} else {
prev = p->value;
}
p->value = value;
table->timestamp++;
return prev;
}
int Table_length(T table)
{
assert(table);
return table->length;
}
/* Map the each element in the table
* And call the apply function in the clousre cl.
* */
void Table_map(T table,
void apply(const void *key, void **value, void *cl),
void *cl)
{
int i;
unsigned stamp;
struct binding *p;
assert(table);
assert(apply);
stamp = table->timestamp;
for (i = 0; i < table->size; i++) {
for (p = table->buckets[i]; p; p = p->link) {
apply(p->key, &p->value, cl);
assert(table->timestamp == stamp);
}
}
}
void *Table_remove(T table, const void *key)
{
int i;
struct binding **pp;
assert(table);
assert(key);
table->timestamp++;
i = (*table->hash)(key) % table->size;
for (pp = &table->buckets[i]; *pp; pp = &(*pp)->link) {
if ((*table->cmp)(key, (*pp)->key) == 0) {
struct binding *p = *pp;
void *value = p->value;
*pp = p->link;
FREE(p);
table->length--;
return value;
}
}
return NULL;
}
void **Table_toArray(T table, void *end)
{
int i, j = 0;
void **array;
struct binding *p;
assert(table);
array = ALLOC((2 * table->length + 1) * sizeof(*array));
for (i = 0; i < table->size; i++)
for (p = table->buckets[i]; p; p = p->link) {
array[j++] = (void *)p->key;
array[j++] = p->value;
}
array[j] = end;
return array;
}
void Table_free(T *table)
{
assert(table && *table);
if ((*table)->length > 0) {
int i;
struct binding *p, *q;
for (i = 0; i < (*table)->size; i++)
for (p = (*table)->buckets[i]; p; p = q) {
q = p->link;
FREE(p);
}
}
FREE(*table);
}