5 #ifndef MAP_H |
5 #ifndef MAP_H |
6 #define MAP_H |
6 #define MAP_H |
7 |
7 |
8 #include "ucx.h" |
8 #include "ucx.h" |
9 #include "string.h" |
9 #include "string.h" |
|
10 #include "mempool.h" |
|
11 #include <stdio.h> |
10 |
12 |
11 #ifdef __cplusplus |
13 #ifdef __cplusplus |
12 extern "C" { |
14 extern "C" { |
13 #endif |
15 #endif |
14 |
16 |
15 #define UCX_MAP_FOREACH(type,elm,map,iter) \ |
17 #define UCX_MAP_FOREACH(elm,iter) \ |
16 for(type elm;ucx_map_iter_next(&iter,(void*)&elm)==0;) |
18 for(;ucx_map_iter_next(&iter,(void*)&elm)==0;) |
17 |
19 |
18 typedef struct UcxMap UcxMap; |
20 typedef struct UcxMap UcxMap; |
19 typedef struct UcxKey UcxKey; |
21 typedef struct UcxKey UcxKey; |
20 typedef struct UcxMapElement UcxMapElement; |
22 typedef struct UcxMapElement UcxMapElement; |
21 typedef struct UcxMapIterator UcxMapIterator; |
23 typedef struct UcxMapIterator UcxMapIterator; |
22 |
24 |
|
25 /* |
|
26 * param 1: element |
|
27 * param 2: additional data |
|
28 * param 3: size of encoded data will be stored here |
|
29 * returns encoded / decoded string or NULL on failure |
|
30 */ |
|
31 typedef void*(*ucx_map_coder)(void*,void*,size_t*); |
|
32 |
23 struct UcxMap { |
33 struct UcxMap { |
24 UcxMapElement **map; |
34 UcxMapElement **map; |
25 size_t size; |
35 size_t size; |
|
36 size_t count; |
26 }; |
37 }; |
27 |
38 |
28 struct UcxKey { |
39 struct UcxKey { |
29 void *data; |
40 void *data; |
30 size_t len; |
41 size_t len; |
44 }; |
55 }; |
45 |
56 |
46 |
57 |
47 UcxMap *ucx_map_new(size_t size); |
58 UcxMap *ucx_map_new(size_t size); |
48 void ucx_map_free(UcxMap *map); |
59 void ucx_map_free(UcxMap *map); |
|
60 /* you cannot clone maps with more than 390 mio entries */ |
|
61 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data); |
|
62 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); |
|
63 int ucx_map_rehash(UcxMap *map); |
49 |
64 |
50 int ucx_map_put(UcxMap *map, UcxKey key, void *data); |
65 int ucx_map_put(UcxMap *map, UcxKey key, void *data); |
51 void* ucx_map_get(UcxMap *map, UcxKey key); |
66 void* ucx_map_get(UcxMap *map, UcxKey key); |
52 |
67 |
53 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) |
68 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) |
61 |
76 |
62 UcxMapIterator ucx_map_iterator(UcxMap *map); |
77 UcxMapIterator ucx_map_iterator(UcxMap *map); |
63 |
78 |
64 int ucx_map_iter_next(UcxMapIterator *i, void **elm); |
79 int ucx_map_iter_next(UcxMapIterator *i, void **elm); |
65 |
80 |
|
81 /* use macros for string maps only, values are not encoded */ |
|
82 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL) |
|
83 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) |
|
84 |
|
85 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, |
|
86 ucx_map_coder decoder, void* decdata); |
|
87 /* encoders shall provide null terminated strings*/ |
|
88 int ucx_map_store_enc(UcxMap *map, FILE *f, |
|
89 ucx_map_coder encoder, void* encdata); |
|
90 |
66 #ifdef __cplusplus |
91 #ifdef __cplusplus |
67 } |
92 } |
68 #endif |
93 #endif |
69 |
94 |
70 #endif /* MAP_H */ |
95 #endif /* MAP_H */ |