-
Notifications
You must be signed in to change notification settings - Fork 3
/
map_template.h
442 lines (416 loc) · 12.2 KB
/
map_template.h
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*******************************************************************************
Copyright (c) 2016 Jannis Mohlin Tsiroyannis
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#ifndef MAP_TEMPLATE_H_
#define MAP_TEMPLATE_H_
#include <stdlib.h>
/*******************************************************************************
Generic, typed, hash table implementation
Insertion causes a struct assignment (=) into an internal buffer, owned and
maintained by the map itself.
If your type is (or contains) a pointer or other resource that needs to
be freed, you yourself are of course responsible for freeing that resource
when appropriate.
Example usage:
// In header files needing access to the map:
_DECLARE_MAP(int, const char*, struct StrMap, str_map)
// In ONE c file:
_IMPLEMENT_MAP(int, const char*, struct StrMap, str_map, strcmp, string_hash)
Access example:
struct StrMap map = str_map_create(10, 0);
char* str = malloc(...);
const char key = "A string key.";
str_map_insert(&s_map, 1, key);
int value;
if ( str_map_get(&map, key, &value) == 0 )
printf("Map had \"%s\" mapped to %d\n", key, value);
******************************************************************************/
#define _DECLARE_MAP( V, K, SN, FN ) \
SN \
{ \
SN##Element* data; \
unsigned int tableSize; \
unsigned int insertedElements; \
char growing; \
}; \
\
SN##Element \
{ \
K key; \
V value; \
\
SN##Element* next; \
\
char inUse; \
}; \
\
/**
Creates a new instance of this map.
size: the initial size of the hashtable (in
objects, not bytes).
growing: a nonzero value will cause the map to
grow dynamically when needed.
Returns: an instance of this map.
*/ \
SN \
FN##_create(unsigned int size, char growing); \
\
/**
Destroys a previously created instance of this map.
*/ \
void FN##_destroy(SN* table); \
\
/**
Inserts a key-value pair into the map.
Returns: 0 on success, 1 if the key is already in
the map (=no changes made).
*/ \
int FN##_insert( \
SN* table, V value, K key); \
\
/**
Removes a key-value pair from the map.
returns: 0 on success, 1 if the key was not in the
map (=no changes made).
*/ \
int \
FN##_remove(SN* table, K key); \
\
/**
Gets a value from the map.
Returns 0 on success and sets "out_value".
Returns 1 if the key was not in the map.
*/ \
int \
FN##_get( \
SN* table, \
K key, \
V* out_value); \
\
/**
Gets a pointer to a value from the map.
Returns valid pointer on success.
Returns NULL-pointer if the key was not in the map.
*/ \
V* \
FN##_get_ind( \
SN* table, \
K key); \
\
/**
Gets the number of key-value pairs in the map.
*/ \
unsigned int \
FN##_get_count(SN* table); \
\
/**
Execute "func" once for every key in the map.
Optionally delete each key-value pair after
executing func.
func: Must be a function with signature:
int func(K); This function should return
non-zero if the given key-value pair should
be deleted and zero otherwise.
*/ \
void \
FN##_for_each_key(SN* table, \
int (*func) (K) ); \
\
/**
Execute "func" once for every key in the map (as a
pointer).
Optionally delete each key-value pair after
executing func.
func: Must be a function with signature:
int func(K*); This function should return
non-zero if the given key-value pair should
be deleted and zero otherwise.
*/ \
void \
FN##_for_each_key_ind(SN* table, \
int (*func) (K*) ); \
#define _IMPLEMENT_MAP( V, K, SN, FN, C, H ) \
static void FN##_mapGrow(SN* table) \
{ \
unsigned int newSize = table->tableSize * 2 + 1; \
SN newTable = \
FN##_create(newSize, table->growing); \
\
for (unsigned int index = 0; index < table->tableSize; ++index) \
{ \
SN##Element* element = &table->data[index]; \
while(element && element->inUse) \
{ \
FN##_insert( \
&newTable, element->value, element->key); \
element = element->next; \
} \
} \
\
FN##_destroy(table); \
*table = newTable; \
} \
\
SN \
FN##_create(unsigned int size, char growing) \
{ \
SN table; \
table.data = calloc(size, sizeof(SN##Element)); \
table.tableSize = size; \
table.insertedElements = 0; \
table.growing = growing; \
return table; \
} \
\
void \
FN##_destroy(SN* table) \
{ \
for (unsigned int i = 0; i < table->tableSize; ++i) \
{ \
SN##Element* element = &table->data[i]; \
if (element->next != NULL) \
{ \
element = element->next; \
while (element->next != NULL) \
{ \
SN##Element* toBeFreed = element; \
element = element->next; \
free(toBeFreed); \
} \
free(element); \
} \
} \
\
free(table->data); \
} \
\
int \
FN##_insert( \
SN* table, \
V value, K key) \
{ \
if (table->growing && \
table->insertedElements >= table->tableSize) \
FN##_mapGrow(table); \
\
unsigned int index = H(key) % table->tableSize; \
SN##Element* element = &table->data[index]; \
\
if (element->inUse) \
{ \
if ( C(element->key, key) == 0 ) \
return 1; \
while (element->next != NULL) \
{ \
element = element->next; \
if ( C(element->key, key) == 0 ) \
return 1; \
} \
element->next = malloc(sizeof(SN##Element)); \
element = element->next; \
} \
\
element->key = key; \
element->inUse = 1; \
element->value = value; \
element->next = NULL; \
++(table->insertedElements); \
return 0; \
} \
\
int \
FN##_remove(SN* table, K key) \
{ \
unsigned int index = H(key) % table->tableSize; \
SN##Element* element = &table->data[index]; \
\
if (!element->inUse) \
return 1; \
\
if ( C(element->key, key) == 0 ) \
{ \
if (element->next) \
{ \
SN##Element* next = element->next; \
*element = *(element->next); \
free(next); \
} \
else \
element->inUse = 0; \
--(table->insertedElements); \
return 0; \
} \
else \
{ \
SN##Element* previous = element; \
element = element->next; \
while ( element->inUse && C(element->key, key) != 0 ) \
{ \
if (element->next) \
{ \
previous = element; \
element = element->next; \
} \
else \
return 1; \
} \
previous->next = element->next; \
free(element); \
--(table->insertedElements); \
return 0; \
} \
} \
\
int \
FN##_get( \
SN* table, \
K key, \
V* out_value) \
{ \
int index = H(key) % table->tableSize; \
SN##Element* element = &table->data[index]; \
\
while ( element->inUse ) \
{ \
if ( C(element->key, key) == 0 ) \
{ \
*out_value = element->value; \
return 0; \
} \
if (element->next) \
element = element->next; \
else \
return 1; \
} \
\
return 1; \
}; \
\
V* \
FN##_get_ind( \
SN* table, \
K key) \
{ \
int index = H(key) % table->tableSize; \
SN##Element* element = &table->data[index]; \
\
while ( element->inUse ) \
{ \
if ( C(element->key, key) == 0 ) \
{ \
return &element->value; \
} \
if (element->next) \
element = element->next; \
else \
return NULL; \
} \
\
return NULL; \
}; \
\
unsigned \
FN##_get_count(SN* table) \
{ \
return table->insertedElements; \
} \
\
void \
FN##_for_each_key(SN* table, \
int (*func) (K) ) \
{ \
for (unsigned index = 0; index < table->tableSize; ++index) \
{ \
int first = 1; \
SN##Element* element = &table->data[index]; \
SN##Element* previous = NULL; \
while(element && element->inUse) \
{ \
if (func( element->key )) \
{ \
if (first) \
{ \
if (element->next) \
{ \
SN##Element* next = element->next; \
*element = *(element->next); \
free(next); \
} \
else \
element->inUse = 0; \
--(table->insertedElements); \
} \
else \
{ \
previous->next = element->next; \
free(element); \
--(table->insertedElements); \
} \
} \
else \
{ \
previous = element; \
element = element->next; \
first = 0; \
} \
} \
} \
} \
\
void \
FN##_for_each_key_ind(SN* table, \
int (*func) (K*) ) \
{ \
for (unsigned index = 0; index < table->tableSize; ++index) \
{ \
int first = 1; \
SN##Element* element = &table->data[index]; \
SN##Element* previous = NULL; \
while(element && element->inUse) \
{ \
if (func( & element->key )) \
{ \
if (first) \
{ \
if (element->next) \
{ \
SN##Element* next = element->next; \
*element = *(element->next); \
free(next); \
} \
else \
element->inUse = 0; \
--(table->insertedElements); \
} \
else \
{ \
previous->next = element->next; \
free(element); \
--(table->insertedElements); \
} \
} \
else \
{ \
previous = element; \
element = element->next; \
first = 0; \
} \
} \
} \
} \
#endif