-
Notifications
You must be signed in to change notification settings - Fork 7
/
map.c
349 lines (299 loc) · 9.46 KB
/
map.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/***************************************************************************
map.c - description
-------------------
begin : Thu Jul 12 2001
copyright : (C) 2001 by John D. Robertson
email : john@rrci.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <string.h>
#include <stdlib.h>
#include "map.h"
struct MAP_node {
unsigned int len;
void *item_ptr;
/* Key goes here */
};
/* Store the variable length key after the 'item_ptr' member of the MAP_node by
* malloc()'ing extra memory. This avoids indirection and memory fragmentation.
*/
/* Macro to access the address of the variable length key */
#define KEY_PTR(ht_node_ptr)\
((void *)(((char *)(ht_node_ptr)) + sizeof(struct MAP_node)))
static unsigned int
hash(const unsigned char *key, unsigned int len)
/********************************************************************
* Hashing function lifted from http://burtleburtle.net/bob/hash/hashfaq.html
*/
{
unsigned int i, h;
for(h=0, i=0; i<len; ++i) {
h += key[i];
h += (h<<10);
h ^= (h>>6);
}
h += (h<<3);
h ^= (h>>11);
h += (h<<15);
return h;
}
MAP *
MAP_constructor(MAP *self,
unsigned int numBuckets,
unsigned int slotsPerBucket)
/****************************************************************
* Construct a hash table with numBuckets buckets. slotsPerBucket provides
* an initial sizing of the buckets, but they can grow dynamically.
*/
{
unsigned int i;
if(!self) return NULL;
memset(self, 0, sizeof(*self));
self->numBuckets= numBuckets;
/* Get the array of buckets */
if(!(self->bucketArr= malloc(numBuckets*sizeof(PTRVEC)))) goto abort;
/* Initialize each bucket */
for(i= 0; i < numBuckets; ++i) {
if(!PTRVEC_constructor(self->bucketArr+i, slotsPerBucket)) goto abort;
}
return self;
abort:
return NULL;
}
int
MAP_sinit(MAP *self,
unsigned int numBuckets,
unsigned int slotsPerBucket)
/***********************************************************
* Initialize or clear() for a static instance.
*/
{
int rtn= 1;
if(!self->bucketArr) {
if(!MAP_constructor(self, numBuckets, slotsPerBucket)) goto abort;
} else {
MAP_clear(self);
}
rtn= 0;
abort:
return rtn;
}
void*
MAP_destructor(MAP *self)
/****************************************************************/
{
unsigned int i;
void *ptr;
for(i= 0; i < self->numBuckets; ++i) {
while((ptr= PTRVEC_remHead(self->bucketArr+i))) free(ptr);
if(!PTRVEC_destructor(self->bucketArr+i)) return NULL;
}
free(self->bucketArr);
return self;
}
int
MAP_clearAndDestroy(MAP *self, void *(* destructor)(void *self))
/***********************************************************************/
{
int rtn= 0;
unsigned int ndx;
struct MAP_node *n_ptr;
/* Loop through the buckets */
for(ndx= 0; ndx < self->numBuckets; ++ndx) {
/* For each node in the bucket... */
while((n_ptr= PTRVEC_remHead(self->bucketArr+ndx))) {
/* Call the supplied destructor */
if(destructor) {
if(!(*destructor)(n_ptr->item_ptr)) rtn= 1;
/* Free the item */
free(n_ptr->item_ptr);
}
/* And the node */
free(n_ptr);
}
}
return rtn;
}
int
MAP_visitAllEntries(MAP *self, int (* func)(void *item_ptr, void *data), void *data)
/******************************************************************************/
{
unsigned ndx, i;
struct MAP_node *n_ptr;
/* Loop through the buckets */
for(ndx= 0; ndx < self->numBuckets; ++ndx) {
/* For each node in the bucket... */
PTRVEC_loopFwd(self->bucketArr+ndx, i, n_ptr) {
/* Call the supplied function */
int rc= (*func)(n_ptr->item_ptr, data);
if(rc) return rc;
}
}
return 0;
}
unsigned
MAP_numItems(MAP *self)
/******************************************************************************
* Return a count of the items indexed in the hash table.
*/
{
unsigned ndx, rtn= 0;
/* Loop through the buckets */
for(ndx= 0; ndx < self->numBuckets; ++ndx) {
rtn += PTRVEC_numItems(self->bucketArr+ndx);
}
return rtn;
}
int
MAP_addKey(MAP *self,
const void *key_ptr,
unsigned int keyLen,
void *item_ptr)
/***************************************************************************/
{
struct MAP_node *n_ptr;
unsigned int ndx;
/* Figure out in which bucket to dump it */
ndx= hash(key_ptr, keyLen) % self->numBuckets;
/* malloc() the mode and add it to the node list */
if(!(n_ptr= malloc(sizeof(*n_ptr) + keyLen)) ||
!PTRVEC_addTail(self->bucketArr+ndx, n_ptr)) return 1;
/* store pertinant information in the node */
n_ptr->len= keyLen;
n_ptr->item_ptr= item_ptr;
memcpy(KEY_PTR(n_ptr), key_ptr, keyLen);
return 0;
}
int
MAP_findItems(MAP *self,
void* rtnArr[],
unsigned int rtnArrSize,
const void *key_ptr,
unsigned int keyLen)
/**********************************************************/
{
unsigned int ndx, i;
int count= 0;
struct MAP_node *n_ptr;
/* Figure out which bucket to search in */
ndx= hash(key_ptr, keyLen) % self->numBuckets;
/* Loop through the bucket looking for a matching key */
PTRVEC_loopFwd(self->bucketArr + ndx, i, n_ptr) {
/* Compare the keys */
if(keyLen == n_ptr->len &&
!memcmp(KEY_PTR(n_ptr), key_ptr, keyLen)) {
/* Store item_ptr in the return array */
if(count == rtnArrSize) return -1;
rtnArr[count]= n_ptr->item_ptr;
++count;
}
}
return count;
}
static struct MAP_node *
_MAP_findNode(MAP *self,
unsigned int *rtnBucketNo_ptr,
const void *key_ptr,
unsigned int keyLen)
/**********************************************************************
* Find the node that matches the supplied key.
* Return it's pointer, or NULL if it cannot be found.
*/
{
unsigned int ndx, i;
struct MAP_node *n_ptr;
/* Figure out which bucket to search in */
ndx= hash(key_ptr, keyLen) % self->numBuckets;
/* Loop through the bucket looking for a matching key */
PTRVEC_loopFwd(self->bucketArr + ndx, i, n_ptr) {
/* Compare the keys */
if(keyLen == n_ptr->len &&
!memcmp(KEY_PTR(n_ptr), key_ptr, keyLen)) {
*rtnBucketNo_ptr= ndx;
return n_ptr;
}
}
return NULL;
}
void*
MAP_findItem(MAP *self,
const void *key_ptr,
unsigned int keyLen)
/*************************************************************************/
{
struct MAP_node *n_ptr;
unsigned int bucket;
if(!(n_ptr= _MAP_findNode(self, &bucket, key_ptr, keyLen))) return NULL;
return n_ptr->item_ptr;
}
void*
MAP_removeSpecificItem(MAP *self,
const void *key_ptr,
unsigned int keyLen,
void *pItem)
/******************************************************************************
* Find the the first matching key and remove it from the hash table.
* pItem is the address of the specific item to be removed.
* Returns:
* NULL Not found
* item_ptr first one found
*/
{
unsigned int ndx, i;
struct MAP_node *n_ptr;
void *rtn= NULL;
/* Figure out which bucket to search in */
ndx= hash(key_ptr, keyLen) % self->numBuckets;
/* Loop through the bucket looking for a matching key */
PTRVEC_loopFwd(self->bucketArr + ndx, i, n_ptr) {
/* Compare the keys */
if(keyLen == n_ptr->len &&
!memcmp(KEY_PTR(n_ptr), key_ptr, keyLen) &&
n_ptr->item_ptr == pItem) { /* And compare the item pointer */
rtn= n_ptr->item_ptr; /* Remember this for return value */
free(PTRVEC_remove(self->bucketArr + ndx, n_ptr)); /* Remove entry from this bucket */
break;
}
}
return rtn;
}
void*
MAP_removeItem(MAP *self,
const void *key_ptr,
unsigned int keyLen)
/******************************************************************************/
{
struct MAP_node *n_ptr;
unsigned int bucket;
void *item_ptr;
if(!(n_ptr= _MAP_findNode(self, &bucket, key_ptr, keyLen))) return NULL;
item_ptr= n_ptr->item_ptr;
free(PTRVEC_remove(self->bucketArr + bucket, n_ptr));
return item_ptr;
}
static int
load_arr(void *item_ptr, void *data)
/******************************************************************
* lambda function to load all CFG_DICT_ENTRY's into an array.
*/
{
void ***ppp= (void***)data;
**ppp= item_ptr;
++(*ppp);
return 0;
}
void
MAP_fetchAllItems(MAP *self, void **rtn_arr)
/******************************************************************************
* Place the itme pointers into the supplied array.
*/
{
MAP_visitAllEntries(self, load_arr, &rtn_arr);
}