ucx/map.h

Wed, 27 Feb 2013 13:30:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 13:30:21 +0100
changeset 95
ecfdc1c4a552
parent 79
cf3757c60c8f
child 103
08018864fb91
permissions
-rw-r--r--

added gnu++11 support

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@69 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;
universe@95 54 size_t 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
universe@71 70 #define ucx_map_sstr_put(m, s, d) \
olaf@78 71 ucx_map_put(m, ucx_key(s.ptr, s.length), d)
universe@71 72 #define ucx_map_cstr_put(m, s, d) \
olaf@78 73 ucx_map_put(m, ucx_key((void*)s, strlen(s)), d)
olaf@78 74 #define ucx_map_int_put(m, i, d) \
olaf@79 75 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), d)
olaf@78 76
universe@71 77 #define ucx_map_sstr_get(m, s) \
olaf@78 78 ucx_map_get(m, ucx_key(s.ptr, s.length))
universe@71 79 #define ucx_map_cstr_get(m, s) \
olaf@78 80 ucx_map_get(m, ucx_key((void*)s, strlen(s)))
olaf@78 81 #define ucx_map_int_get(m, i) \
olaf@78 82 ucx_map_get(m, ucx_key((void*)&i, sizeof(int)))
olaf@78 83
universe@71 84 #define ucx_map_sstr_remove(m, s) \
olaf@78 85 ucx_map_remove(m, ucx_key(s.ptr, s.length))
universe@71 86 #define ucx_map_cstr_remove(m, s) \
olaf@78 87 ucx_map_remove(m, ucx_key((void*)s, strlen(s)))
olaf@78 88 #define ucx_map_int_remove(m, i) \
olaf@79 89 ucx_map_remove(m, ucx_key((void*)&i, sizeof(i)))
olaf@20 90
olaf@20 91 UcxKey ucx_key(void *data, size_t len);
olaf@20 92
universe@67 93 int ucx_hash(const char *data, size_t len);
olaf@2 94
olaf@31 95 UcxMapIterator ucx_map_iterator(UcxMap *map);
olaf@31 96
olaf@31 97 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
olaf@31 98
universe@46 99 /* use macros for string maps only, values are not encoded */
universe@48 100 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
universe@46 101 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
universe@42 102
universe@48 103 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 104 ucx_map_coder decoder, void* decdata);
universe@46 105 /* encoders shall provide null terminated strings*/
universe@48 106 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 107 ucx_map_coder encoder, void* encdata);
universe@42 108
olaf@2 109 #ifdef __cplusplus
olaf@2 110 }
olaf@2 111 #endif
olaf@2 112
olaf@2 113 #endif /* MAP_H */
olaf@2 114

mercurial