ucx/map.h

Fri, 05 Oct 2012 16:59:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 05 Oct 2012 16:59:14 +0200
changeset 52
34f50d0bada4
parent 51
1c78cd19fb6b
child 53
e533c170bfb8
permissions
-rw-r--r--

added ucx_map_copy and fixed ucx_map_rehash

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

mercurial