src/server/ucx/map.h

changeset 88
73b3485e96f1
parent 67
50505dc3f8a6
equal deleted inserted replaced
87:bdec069d2239 88:73b3485e96f1
36 36
37 #ifdef __cplusplus 37 #ifdef __cplusplus
38 extern "C" { 38 extern "C" {
39 #endif 39 #endif
40 40
41 #define UCX_MAP_FOREACH(elm,iter) \ 41 #define UCX_MAP_FOREACH(key,elm,iter) \
42 for(;ucx_map_iter_next(&iter,(void**)&elm)==0;) 42 for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;)
43 43
44 typedef struct UcxMap UcxMap; 44 typedef struct UcxMap UcxMap;
45 typedef struct UcxKey UcxKey; 45 typedef struct UcxKey UcxKey;
46 typedef struct UcxMapElement UcxMapElement; 46 typedef struct UcxMapElement UcxMapElement;
47 typedef struct UcxMapIterator UcxMapIterator; 47 typedef struct UcxMapIterator UcxMapIterator;
53 * returns encoded / decoded string or NULL on failure 53 * returns encoded / decoded string or NULL on failure
54 */ 54 */
55 typedef void*(*ucx_map_coder)(void*,void*,size_t*); 55 typedef void*(*ucx_map_coder)(void*,void*,size_t*);
56 56
57 struct UcxMap { 57 struct UcxMap {
58 UcxAllocator *allocator;
58 UcxMapElement **map; 59 UcxMapElement **map;
59 size_t size; 60 size_t size;
60 size_t count; 61 size_t count;
61 }; 62 };
62 63
78 size_t index; 79 size_t index;
79 }; 80 };
80 81
81 82
82 UcxMap *ucx_map_new(size_t size); 83 UcxMap *ucx_map_new(size_t size);
84 UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator);
83 void ucx_map_free(UcxMap *map); 85 void ucx_map_free(UcxMap *map);
84 /* you cannot clone maps with more than 390 mio entries */ 86 /* you cannot clone maps with more than 390 mio entries */
85 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
86 copy_func fnc, void *data); 88 copy_func fnc, void *data);
87 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); 89 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
90 int ucx_map_put(UcxMap *map, UcxKey key, void *data); 92 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
91 void* ucx_map_get(UcxMap *map, UcxKey key); 93 void* ucx_map_get(UcxMap *map, UcxKey key);
92 void* ucx_map_remove(UcxMap *map, UcxKey key); 94 void* ucx_map_remove(UcxMap *map, UcxKey key);
93 95
94 #define ucx_map_sstr_put(m, s, d) \ 96 #define ucx_map_sstr_put(m, s, d) \
95 ucx_map_put(m, ucx_key(s.ptr, s.length), d) 97 ucx_map_put(m, ucx_key(s.ptr, s.length), (void*)d)
96 #define ucx_map_cstr_put(m, s, d) \ 98 #define ucx_map_cstr_put(m, s, d) \
97 ucx_map_put(m, ucx_key((void*)s, strlen(s)), d) 99 ucx_map_put(m, ucx_key((void*)s, strlen(s)), (void*)d)
98 #define ucx_map_int_put(m, i, d) \ 100 #define ucx_map_int_put(m, i, d) \
99 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), d) 101 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), (void*)d)
100 102
101 #define ucx_map_sstr_get(m, s) \ 103 #define ucx_map_sstr_get(m, s) \
102 ucx_map_get(m, ucx_key(s.ptr, s.length)) 104 ucx_map_get(m, ucx_key(s.ptr, s.length))
103 #define ucx_map_cstr_get(m, s) \ 105 #define ucx_map_cstr_get(m, s) \
104 ucx_map_get(m, ucx_key((void*)s, strlen(s))) 106 ucx_map_get(m, ucx_key((void*)s, strlen(s)))
116 118
117 int ucx_hash(const char *data, size_t len); 119 int ucx_hash(const char *data, size_t len);
118 120
119 UcxMapIterator ucx_map_iterator(UcxMap *map); 121 UcxMapIterator ucx_map_iterator(UcxMap *map);
120 122
121 int ucx_map_iter_next(UcxMapIterator *i, void **elm); 123 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm);
122 124
123 /* use macros for string maps only, values are not encoded */
124 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
125 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
126
127 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
128 ucx_map_coder decoder, void* decdata);
129 /* encoders shall provide null terminated strings*/
130 int ucx_map_store_enc(UcxMap *map, FILE *f,
131 ucx_map_coder encoder, void* encdata);
132 125
133 #ifdef __cplusplus 126 #ifdef __cplusplus
134 } 127 }
135 #endif 128 #endif
136 129

mercurial