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 #ifndef UCX_MAP_H
42 #define UCX_MAP_H
43
44 #include "ucx.h"
45 #include "string.h"
46 #include "allocator.h"
47 #include <stdio.h>
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53
54
55
56
57
58
59
60
61
62
63
64
65 #define UCX_MAP_FOREACH(key,value,iter) \
66 for(UcxKey key;ucx_map_iter_next(&iter,&key, (
void**)&value);)
67
68
69 typedef struct UcxMap UcxMap;
70
71
72 typedef struct UcxKey UcxKey;
73
74
75 typedef struct UcxMapElement UcxMapElement;
76
77
78 typedef struct UcxMapIterator UcxMapIterator;
79
80
81 struct UcxMap {
82
83 UcxAllocator *allocator;
84
85 UcxMapElement **map;
86
87 size_t size;
88
89 size_t count;
90 };
91
92
93 struct UcxKey {
94
95 void *data;
96
97 size_t len;
98
99 int hash;
100 };
101
102
103 struct UcxMapElement {
104
105 void *data;
106
107
108 UcxMapElement *next;
109
110
111 UcxKey key;
112 };
113
114
115 struct UcxMapIterator {
116
117 UcxMap *map;
118
119
120 UcxMapElement *cur;
121
122
123
124
125
126
127
128 size_t index;
129 };
130
131
132
133
134
135
136 UcxMap *ucx_map_new(
size_t size);
137
138
139
140
141
142
143
144 UcxMap *ucx_map_new_a(UcxAllocator *allocator,
size_t size);
145
146
147
148
149
150
151
152
153
154
155 void ucx_map_free(UcxMap *map);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 void ucx_map_free_content(UcxMap *map, ucx_destructor destr);
174
175
176
177
178
179
180
181
182
183
184 void ucx_map_clear(UcxMap *map);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
202 copy_func fnc,
void *data);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc,
void *data);
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 int ucx_map_rehash(UcxMap *map);
237
238
239
240
241
242
243
244
245
246 int ucx_map_put(UcxMap *map, UcxKey key,
void *value);
247
248
249
250
251
252
253
254
255 void* ucx_map_get(UcxMap *map, UcxKey key);
256
257
258
259
260
261
262
263
264 void* ucx_map_remove(UcxMap *map, UcxKey key);
265
266
267
268
269
270
271
272
273
274 #define ucx_map_sstr_put(map, key, value) \
275 ucx_map_put(map, ucx_key(key.ptr, key.length), (
void*)value)
276
277
278
279
280
281
282
283
284
285 #define ucx_map_cstr_put(map, key, value) \
286 ucx_map_put(map, ucx_key((
void*)key, strlen(key)), (
void*)value)
287
288
289
290
291
292
293
294
295
296 #define ucx_map_int_put(map, key, value) \
297 ucx_map_put(map, ucx_key((
void*)&key,
sizeof(key)), (
void*)value)
298
299
300
301
302
303
304
305
306 #define ucx_map_sstr_get(map, key) \
307 ucx_map_get(map, ucx_key(key.ptr, key.length))
308
309
310
311
312
313
314
315
316 #define ucx_map_cstr_get(map, key) \
317 ucx_map_get(map, ucx_key((
void*)key, strlen(key)))
318
319
320
321
322
323
324
325
326 #define ucx_map_int_get(map, key) \
327 ucx_map_get(map, ucx_key((
void*)&key,
sizeof(
int)))
328
329
330
331
332
333
334
335
336 #define ucx_map_sstr_remove(map, key) \
337 ucx_map_remove(map, ucx_key(key.ptr, key.length))
338
339
340
341
342
343
344
345
346 #define ucx_map_cstr_remove(map, key) \
347 ucx_map_remove(map, ucx_key((
void*)key, strlen(key)))
348
349
350
351
352
353
354
355
356 #define ucx_map_int_remove(map, key) \
357 ucx_map_remove(map, ucx_key((
void*)&key,
sizeof(key)))
358
359
360
361
362
363
364
365
366
367
368
369 UcxKey ucx_key(
void *data,
size_t len);
370
371
372
373
374
375
376
377
378 int ucx_hash(
const char *data,
size_t len);
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397 UcxMapIterator ucx_map_iterator(UcxMap *map);
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415 int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key,
void **value);
416
417
418 #ifdef __cplusplus
419 }
420 #endif
421
422 #endif
423
424