ucx/map.h

Fri, 05 Oct 2012 14:06:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 14:06:40 +0200
changeset 51
1c78cd19fb6b
parent 48
621a4430c404
child 52
34f50d0bada4
permissions
-rw-r--r--

added rehashing to maps by using clone function

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

mercurial