ucx/map.h

changeset 136
b798f2eed26a
parent 120
8170f658f017
child 137
81a02ad99388
child 138
7800811078b8
equal deleted inserted replaced
135:a0aa1c15f46b 136:b798f2eed26a
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 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
29 #ifndef UCX_MAP_H 41 #ifndef UCX_MAP_H
30 #define UCX_MAP_H 42 #define UCX_MAP_H
31 43
32 #include "ucx.h" 44 #include "ucx.h"
33 #include "string.h" 45 #include "string.h"
43 55
44 typedef struct UcxMap UcxMap; 56 typedef struct UcxMap UcxMap;
45 typedef struct UcxKey UcxKey; 57 typedef struct UcxKey UcxKey;
46 typedef struct UcxMapElement UcxMapElement; 58 typedef struct UcxMapElement UcxMapElement;
47 typedef struct UcxMapIterator UcxMapIterator; 59 typedef struct UcxMapIterator UcxMapIterator;
48
49 /*
50 * param 1: element
51 * param 2: additional data
52 * param 3: size of encoded data will be stored here
53 * returns encoded / decoded string or NULL on failure
54 */
55 typedef void*(*ucx_map_coder)(void*,void*,size_t*);
56 60
57 struct UcxMap { 61 struct UcxMap {
58 UcxAllocator *allocator; 62 UcxAllocator *allocator;
59 UcxMapElement **map; 63 UcxMapElement **map;
60 size_t size; 64 size_t size;
77 UcxMap *map; 81 UcxMap *map;
78 UcxMapElement *cur; 82 UcxMapElement *cur;
79 size_t index; 83 size_t index;
80 }; 84 };
81 85
82 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 */
83 UcxMap *ucx_map_new(size_t size); 91 UcxMap *ucx_map_new(size_t size);
84 UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator); 92
93 /**
94 * Creates a new hash map with the specified size using an UcxAllocator.
95 * @param size the size of the hash map
96 * @param allocator the allocator to use
97 * @return a pointer to the new hash map
98 */
99 UcxMap *ucx_map_new_a(size_t size, UcxAllocator *allocator);
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 */
85 void ucx_map_free(UcxMap *map); 109 void ucx_map_free(UcxMap *map);
110
86 /* you cannot clone maps with more than 390 mio entries */ 111 /* you cannot clone maps with more than 390 mio entries */
87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 112 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
88 copy_func fnc, void *data); 113 copy_func fnc, void *data);
89 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); 114 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
90 int ucx_map_rehash(UcxMap *map); 115 int ucx_map_rehash(UcxMap *map);
91 116
92 int ucx_map_put(UcxMap *map, UcxKey key, void *data); 117 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
93 void* ucx_map_get(UcxMap *map, UcxKey key); 118 void* ucx_map_get(UcxMap *map, UcxKey key);
94 void* ucx_map_remove(UcxMap *map, UcxKey key); 119 void* ucx_map_remove(UcxMap *map, UcxKey key);
95 120
96 #define ucx_map_sstr_put(m, s, d) \ 121 /**
97 ucx_map_put(m, ucx_key(s.ptr, s.length), (void*)d) 122 * Shorthand for putting data with a sstr_t key into the map.
98 #define ucx_map_cstr_put(m, s, d) \ 123 * @param map the map
99 ucx_map_put(m, ucx_key((void*)s, strlen(s)), (void*)d) 124 * @param key the key
100 #define ucx_map_int_put(m, i, d) \ 125 * @param value the value
101 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), (void*)d) 126 * @see ucx_map_put()
102 127 */
103 #define ucx_map_sstr_get(m, s) \ 128 #define ucx_map_sstr_put(map, key, value) \
104 ucx_map_get(m, ucx_key(s.ptr, s.length)) 129 ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
105 #define ucx_map_cstr_get(m, s) \ 130 /**
106 ucx_map_get(m, ucx_key((void*)s, strlen(s))) 131 * Shorthand for putting data with a C string key into the map.
107 #define ucx_map_int_get(m, i) \ 132 * @param map the map
108 ucx_map_get(m, ucx_key((void*)&i, sizeof(int))) 133 * @param key the key
109 134 * @param value the value
110 #define ucx_map_sstr_remove(m, s) \ 135 * @see ucx_map_put()
111 ucx_map_remove(m, ucx_key(s.ptr, s.length)) 136 */
112 #define ucx_map_cstr_remove(m, s) \ 137 #define ucx_map_cstr_put(map, key, value) \
113 ucx_map_remove(m, ucx_key((void*)s, strlen(s))) 138 ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
114 #define ucx_map_int_remove(m, i) \ 139 /**
115 ucx_map_remove(m, ucx_key((void*)&i, sizeof(i))) 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)))
116 196
117 UcxKey ucx_key(void *data, size_t len); 197 UcxKey ucx_key(void *data, size_t len);
118 198
119 int ucx_hash(const char *data, size_t len); 199 int ucx_hash(const char *data, size_t len);
120 200

mercurial