Thu, 11 Oct 2012 11:42:31 +0200
added some qualifiers + removed pointer alias in mergesort
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 */ |
universe@67 | 61 | int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
universe@67 | 62 | copy_func fnc, void *data); |
olaf@44 | 63 | UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); |
olaf@52 | 64 | int ucx_map_rehash(UcxMap *map); |
olaf@20 | 65 | |
olaf@20 | 66 | int ucx_map_put(UcxMap *map, UcxKey key, void *data); |
olaf@20 | 67 | void* ucx_map_get(UcxMap *map, UcxKey key); |
universe@53 | 68 | void* ucx_map_remove(UcxMap *map, UcxKey key); |
olaf@20 | 69 | |
olaf@30 | 70 | #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) |
universe@29 | 71 | #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d) |
olaf@30 | 72 | #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length)) |
universe@29 | 73 | #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s))) |
universe@53 | 74 | #define ucx_map_sstr_remove(m, s) ucx_map_remove(m, ucx_key(s.ptr, s.length)) |
universe@53 | 75 | #define ucx_map_cstr_remove(m, s) ucx_map_remove(m, ucx_key(s, 1+strlen(s))) |
olaf@20 | 76 | |
olaf@20 | 77 | UcxKey ucx_key(void *data, size_t len); |
olaf@20 | 78 | |
universe@67 | 79 | int ucx_hash(const char *data, size_t len); |
olaf@2 | 80 | |
olaf@31 | 81 | UcxMapIterator ucx_map_iterator(UcxMap *map); |
olaf@31 | 82 | |
olaf@31 | 83 | int ucx_map_iter_next(UcxMapIterator *i, void **elm); |
olaf@31 | 84 | |
universe@46 | 85 | /* use macros for string maps only, values are not encoded */ |
universe@48 | 86 | #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL) |
universe@46 | 87 | #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) |
universe@42 | 88 | |
universe@48 | 89 | int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, |
universe@48 | 90 | ucx_map_coder decoder, void* decdata); |
universe@46 | 91 | /* encoders shall provide null terminated strings*/ |
universe@48 | 92 | int ucx_map_store_enc(UcxMap *map, FILE *f, |
universe@48 | 93 | ucx_map_coder encoder, void* encdata); |
universe@42 | 94 | |
olaf@2 | 95 | #ifdef __cplusplus |
olaf@2 | 96 | } |
olaf@2 | 97 | #endif |
olaf@2 | 98 | |
olaf@2 | 99 | #endif /* MAP_H */ |
olaf@2 | 100 |