-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
304 lines (258 loc) · 9.64 KB
/
hash.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
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "hash.h"
typedef void *pointer;
struct Hashtable {
unsigned long int bucketSize;
unsigned long int capacity;
pointer (*createValue )(pointer);
int (*cmp )(pointer, pointer);
unsigned long int (*hash)(pointer, unsigned long int);
unsigned long int (*destroy)(pointer);
pointer *table;
};
/***Private functions***/
void _getCount(pointer bucket, unsigned long int bucketSize, unsigned long int *count) {
memcpy(count, bucket + bucketSize - sizeof(unsigned long int) - sizeof(pointer), sizeof(unsigned long int));
}
void _setCount(pointer bucket, unsigned long int bucketSize, const unsigned long int count) {
memcpy(bucket + bucketSize - sizeof(unsigned long int) - sizeof(pointer), &count, sizeof(unsigned long int));
}
void _getNext(pointer bucket, unsigned long int bucketSize, pointer *next) {
memcpy(next, bucket + bucketSize - sizeof(pointer), sizeof(pointer));
}
void _setNext(pointer bucket, unsigned long int bucketSize, pointer next) {
memcpy(bucket + bucketSize - sizeof(pointer), &next, sizeof(pointer));
}
void _getValue(pointer bucket, unsigned long int offset, pointer *value) {
memcpy(value, bucket + offset * sizeof(pointer), sizeof(pointer));
}
void _setValue(pointer bucket, unsigned long int offset, pointer value) {
memcpy(bucket + offset * sizeof(pointer), &value, sizeof(pointer));
}
unsigned long int _getEmptySlots(unsigned long int bucketSize, unsigned long int count) {
return (bucketSize - sizeof(pointer) - sizeof(unsigned long int)) / sizeof(pointer) - count;
}
const pointer _allocBucket(unsigned long int size) {
pointer bucket = malloc((size_t) size);
if (bucket != NULL) {
_setNext(bucket, size, NULL);
}
return bucket;
}
/***Public functions***/
bool HT_Init(Hashtable *ht,
unsigned long int capacity,
unsigned long int bucketSize,
pointer (*createValue)(pointer),
int (*cmp)(pointer, pointer),
unsigned long (*hash)(pointer, unsigned long int),
unsigned long (*destroy)(pointer)
) {
assert(bucketSize >= sizeof(pointer) * 2 + sizeof(unsigned long int));
assert(capacity > 0);
int i;
*ht = (Hashtable) malloc(sizeof(struct Hashtable));
if ((*ht) != NULL) {
(*ht)->bucketSize = bucketSize;
(*ht)->capacity = capacity;
(*ht)->createValue = createValue;
(*ht)->cmp = cmp;
(*ht)->hash = hash;
(*ht)->destroy = destroy;
(*ht)->table = malloc(sizeof(pointer) * capacity);
if ((*ht)->table != NULL) {
for (i = 0; i < capacity; i++) {
(*ht)->table[i] = NULL;
}
return true;
}
}
return false;
}
int HT_Insert(Hashtable ht, pointer key, pointer valueParams, pointer *value) {
unsigned long int index = 0, count = 0, slots = 0, slot = 0;
pointer bucket = NULL, b = NULL, next = NULL, slotValue = NULL;
assert(ht != NULL);
assert(key != NULL);
assert(value != NULL);
index = ht->hash(key, ht->capacity);
//printf("[%.3lu] ", index);
bucket = ht->table[index];
/* Check if current bucket exists */
if (bucket == NULL) {
slotValue = ht->createValue(valueParams);
if (slotValue != NULL) {
bucket = _allocBucket(ht->bucketSize);
_setValue(bucket, 0, slotValue);
*value = slotValue;
//printf("--> [%p] ", *value);
_setCount(bucket, ht->bucketSize, 1);
ht->table[index] = bucket;
} else {
return false;
}
} else {
//printf(" --> !!! Collision !!! ");
next = bucket;
/* Check each bucket to detect possibly duplicate values and
* determine where is the target slot to write the new value.*/
while (next != NULL) {
_getCount(bucket, ht->bucketSize, &count);
slots = _getEmptySlots(ht->bucketSize, count);
/*Get value for each slot of bucket*/
for (slot = 0; slot < count; slot++) {
_getValue(bucket, slot, &slotValue);
if (!ht->cmp(slotValue, valueParams)) {
//printf(":::DUPLICATE::: ");
//printf("\n");
*value = slotValue;
return false;
}
}
/*Get next pointer to determine if this bucket has an overflow bucket*/
_getNext(bucket, ht->bucketSize, &next);
if (next != NULL) {
bucket = next;
}
};
/*Check if there are exists empty slots at the last bucket*/
if (slots) {
//printf(":::BUCKET [%p] HAS %lu SLOTS::: ", bucket, slots - 1);
slotValue = ht->createValue(valueParams);
if (slotValue != NULL) {
_setValue(bucket, slot, slotValue);
*value = slotValue;
_setCount(bucket, ht->bucketSize, count + 1);
} else {
return false;
}
} else {
//printf(":::BUCKET [%p] IS FULL::: ", bucket);
slotValue = ht->createValue(valueParams);
if (slotValue != NULL) {
b = _allocBucket(ht->bucketSize);
//printf(" --> ALLOCATE BUCKET [%p] ", b);
_setNext(bucket, ht->bucketSize, b);
_setValue(b, 0, slotValue);
*value = slotValue;
_setCount(b, ht->bucketSize, 1);
} else {
return false;
}
}
}
//printf("\n");
return true;
}
pointer HT_Get(Hashtable ht, pointer key) {
unsigned long int index = 0, count = 0, slot = 0;
pointer bucket = NULL, v = NULL, next = NULL;
assert(ht != NULL);
assert(key != NULL);
index = ht->hash(key, ht->capacity);
bucket = ht->table[index];
/* Check if current bucket exists */
if (bucket != NULL) {
next = bucket;
/* Check each bucket to determine where is the target slot.*/
while (next != NULL) {
_getCount(bucket, ht->bucketSize, &count);
/*Get value for each slot of bucket*/
for (slot = 0; slot < count; slot++) {
_getValue(bucket, slot, &v);
if (!ht->cmp(v, key)) {
return v;
}
}
/*Get next pointer to determine if this bucket has an overflow bucket*/
_getNext(bucket, ht->bucketSize, &next);
if (next != NULL) {
bucket = next;
}
};
}
return NULL;
}
int HT_Remove(Hashtable ht, pointer key, pointer valueParams, bool forceDestroyItem) {
unsigned long int index = 0, count = 0, slot = 0, targetSlot = 0;
pointer bucket = NULL, targetBucket = NULL, next = NULL, slotValue = NULL;
assert(ht != NULL);
assert(key != NULL);
index = ht->hash(key, ht->capacity);
//printf("[%.3lu] ", index);
bucket = ht->table[index];
/* Check if current bucket exists */
if (bucket == NULL) {
return false; /* Nothing to remove*/
} else {
next = bucket;
/* Check each bucket to detect target value, resume until last bucket.*/
while (next != NULL) {
_getCount(bucket, ht->bucketSize, &count);
/* Get value for each slot of bucket*/
for (slot = 0; slot < count; slot++) {
_getValue(bucket, slot, &slotValue);
/* Check for target slot*/
if (!ht->cmp(slotValue, valueParams)) {
targetBucket = bucket;
targetSlot = slot;
if (forceDestroyItem) {
ht->destroy(slotValue);
}
_setValue(bucket, slot, NULL);
slotValue = NULL;
}
}
/* Get next pointer to determine if this bucket has an overflow bucket*/
_getNext(bucket, ht->bucketSize, &next);
if (next != NULL) {
bucket = next;
}
};
/* Move last found slotValue in targetSlotValue*/
if (targetBucket != NULL) {
_setValue(targetBucket, targetSlot, slotValue);
_setValue(bucket, slot, NULL);
_getCount(bucket, ht->bucketSize, &count);
if (count > 0) {
_setCount(bucket, ht->bucketSize, --count);
}
}
}
//printf("\n");
return true;
}
void HT_Destroy(Hashtable *ht, bool forceDestroyItem) {
assert((*ht) != NULL);
pointer next = NULL, slotValue = NULL, bucket = NULL;
unsigned long int count = 0, i, slot;
//printf("\n");
for (i = 0; i < (*ht)->capacity; i++) {
bucket = next = (*ht)->table[i];
if (bucket != NULL) {
//printf("[%.3lu] -->", i);
while (next != NULL) {
_getCount(bucket, (*ht)->bucketSize, &count);
/*Get value for each slot of bucket to destroy it.*/
if (forceDestroyItem) {
for (slot = 0; slot < count; slot++) {
_getValue(bucket, slot, &slotValue);
(*ht)->destroy(slotValue);
//printf("[%p] ", slotValue);
}
}
/*Get next pointer to determine if this bucket has an overflow bucket.*/
_getNext(bucket, (*ht)->bucketSize, &next);
free(bucket);
if (next != NULL) {
bucket = next;
}
};
//printf("\n");
}
}
free((*ht)->table);
free(*ht);
}