ucx/map.h

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
equal deleted inserted replaced
4:ae5a98f0545c 5:88625853ae74
1 /* 1 /*
2 * 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 */ 3 *
4 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 #ifndef MAP_H 5 *
6 #define MAP_H 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file map.h
31 *
32 * Hash map implementation.
33 *
34 * This implementation uses murmur hash 2 and separate chaining with linked
35 * lists.
36 *
37 * @author Mike Becker
38 * @author Olaf Wintermann
39 */
40
41 #ifndef UCX_MAP_H
42 #define UCX_MAP_H
7 43
8 #include "ucx.h" 44 #include "ucx.h"
9 #include "string.h" 45 #include "string.h"
10 #include "mempool.h" 46 #include "allocator.h"
11 #include <stdio.h> 47 #include <stdio.h>
12 48
13 #ifdef __cplusplus 49 #ifdef __cplusplus
14 extern "C" { 50 extern "C" {
15 #endif 51 #endif
16 52
17 #define UCX_MAP_FOREACH(elm,iter) \ 53 #define UCX_MAP_FOREACH(key,elm,iter) \
18 for(;ucx_map_iter_next(&iter,(void**)&elm)==0;) 54 for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;)
19 55
20 typedef struct UcxMap UcxMap; 56 typedef struct UcxMap UcxMap;
21 typedef struct UcxKey UcxKey; 57 typedef struct UcxKey UcxKey;
22 typedef struct UcxMapElement UcxMapElement; 58 typedef struct UcxMapElement UcxMapElement;
23 typedef struct UcxMapIterator UcxMapIterator; 59 typedef struct UcxMapIterator UcxMapIterator;
24 60
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
33 struct UcxMap { 61 struct UcxMap {
62 UcxAllocator *allocator;
34 UcxMapElement **map; 63 UcxMapElement **map;
35 size_t size; 64 size_t size;
36 size_t count; 65 size_t count;
37 }; 66 };
38 67
49 }; 78 };
50 79
51 struct UcxMapIterator { 80 struct UcxMapIterator {
52 UcxMap *map; 81 UcxMap *map;
53 UcxMapElement *cur; 82 UcxMapElement *cur;
54 int index; 83 size_t index;
55 }; 84 };
56 85
57 86 /**
87 * Creates a new hash map with the specified size.
88 * @param size the size of the hash map
89 * @return a pointer to the new hash map
90 */
58 UcxMap *ucx_map_new(size_t size); 91 UcxMap *ucx_map_new(size_t size);
92
93 /**
94 * Creates a new hash map with the specified size using an UcxAllocator.
95 * @param allocator the allocator to use
96 * @param size the size of the hash map
97 * @return a pointer to the new hash map
98 */
99 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size);
100
101 /**
102 * Frees a hash map.
103 *
104 * <b>Note:</b> the contents are <b>not</b> freed, use an UcxMempool for that
105 * purpose.
106 *
107 * @param map the map to be freed
108 */
59 void ucx_map_free(UcxMap *map); 109 void ucx_map_free(UcxMap *map);
110
60 /* you cannot clone maps with more than 390 mio entries */ 111 /* you cannot clone maps with more than 390 mio entries */
61 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 112 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
62 copy_func fnc, void *data); 113 copy_func fnc, void *data);
63 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); 114 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
64 int ucx_map_rehash(UcxMap *map); 115 int ucx_map_rehash(UcxMap *map);
65 116
66 int ucx_map_put(UcxMap *map, UcxKey key, void *data); 117 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
67 void* ucx_map_get(UcxMap *map, UcxKey key); 118 void* ucx_map_get(UcxMap *map, UcxKey key);
68 void* ucx_map_remove(UcxMap *map, UcxKey key); 119 void* ucx_map_remove(UcxMap *map, UcxKey key);
69 120
70 #define ucx_map_sstr_put(m, s, d) \ 121 /**
71 ucx_map_put(m, ucx_key((void*)s.ptr, s.length), d) 122 * Shorthand for putting data with a sstr_t key into the map.
72 #define ucx_map_cstr_put(m, s, d) \ 123 * @param map the map
73 ucx_map_put(m, ucx_key((void*)s, 1+strlen(s)), d) 124 * @param key the key
74 #define ucx_map_sstr_get(m, s) \ 125 * @param value the value
75 ucx_map_get(m, ucx_key((void*)s.ptr, s.length)) 126 * @see ucx_map_put()
76 #define ucx_map_cstr_get(m, s) \ 127 */
77 ucx_map_get(m, ucx_key((void*)s, 1+strlen(s))) 128 #define ucx_map_sstr_put(map, key, value) \
78 #define ucx_map_sstr_remove(m, s) \ 129 ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
79 ucx_map_remove(m, ucx_key((void*)s.ptr, s.length)) 130 /**
80 #define ucx_map_cstr_remove(m, s) \ 131 * Shorthand for putting data with a C string key into the map.
81 ucx_map_remove(m, ucx_key((void*)s, 1+strlen(s))) 132 * @param map the map
133 * @param key the key
134 * @param value the value
135 * @see ucx_map_put()
136 */
137 #define ucx_map_cstr_put(map, key, value) \
138 ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
139 /**
140 * Shorthand for putting data with an integer key into the map.
141 * @param map the map
142 * @param key the key
143 * @param value the value
144 * @see ucx_map_put()
145 */
146 #define ucx_map_int_put(map, key, value) \
147 ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
148
149
150 /**
151 * Shorthand for getting data from the map with a sstr_t key.
152 * @param map the map
153 * @param key the key
154 * @see ucx_map_get()
155 */
156 #define ucx_map_sstr_get(map, key) \
157 ucx_map_get(map, ucx_key(key.ptr, key.length))
158 /**
159 * Shorthand for getting data from the map with a C string key.
160 * @see ucx_map_get()
161 */
162 #define ucx_map_cstr_get(map, key) \
163 ucx_map_get(map, ucx_key((void*)key, strlen(key)))
164 /**
165 * Shorthand for getting data from the map with an integer key.
166 * @param map the map
167 * @param key the key
168 * @see ucx_map_get()
169 */
170 #define ucx_map_int_get(map, key) \
171 ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
172 /**
173 * Shorthand for removing data from the map with a sstr_t key.
174 * @param map the map
175 * @param key the key
176 * @see ucx_map_remove()
177 */
178 #define ucx_map_sstr_remove(map, key) \
179 ucx_map_remove(map, ucx_key(key.ptr, key.length))
180 /**
181 * Shorthand for removing data from the map with a C string key.
182 * @param map the map
183 * @param key the key
184 * @see ucx_map_remove()
185 */
186 #define ucx_map_cstr_remove(map, key) \
187 ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
188 /**
189 * Shorthand for removing data from the map with an integer key.
190 * @param map the map
191 * @param key the key
192 * @see ucx_map_remove()
193 */
194 #define ucx_map_int_remove(map, key) \
195 ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
82 196
83 UcxKey ucx_key(void *data, size_t len); 197 UcxKey ucx_key(void *data, size_t len);
84 198
85 int ucx_hash(const char *data, size_t len); 199 int ucx_hash(const char *data, size_t len);
86 200
87 UcxMapIterator ucx_map_iterator(UcxMap *map); 201 UcxMapIterator ucx_map_iterator(UcxMap *map);
88 202
89 int ucx_map_iter_next(UcxMapIterator *i, void **elm); 203 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm);
90 204
91 /* use macros for string maps only, values are not encoded */
92 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
93 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
94
95 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
96 ucx_map_coder decoder, void* decdata);
97 /* encoders shall provide null terminated strings*/
98 int ucx_map_store_enc(UcxMap *map, FILE *f,
99 ucx_map_coder encoder, void* encdata);
100 205
101 #ifdef __cplusplus 206 #ifdef __cplusplus
102 } 207 }
103 #endif 208 #endif
104 209
105 #endif /* MAP_H */ 210 #endif /* UCX_MAP_H */
106 211

mercurial