ucx/map.h

Thu, 04 Oct 2012 16:03:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 16:03:18 +0200
changeset 42
ff3dd1ee7dee
parent 41
7f90a03e186e
child 44
46356d74e873
permissions
-rw-r--r--

(broken-commit) - added load and store functions, tests failing

some evil crash happens when executing the test - remove the strcmp calls in the test case for the store and load function and everything "works"

the error must be somewhere else - maybe something that should not be freed is freed during the test

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@41 10 #include <stdio.h>
olaf@20 11
olaf@2 12 #ifdef __cplusplus
olaf@2 13 extern "C" {
olaf@2 14 #endif
olaf@2 15
universe@41 16 #define UCX_MAP_FOREACH(elm,iter) \
universe@42 17 for(;ucx_map_iter_next(&iter,(void*)&elm)==0;)
olaf@31 18
olaf@31 19 typedef struct UcxMap UcxMap;
olaf@31 20 typedef struct UcxKey UcxKey;
olaf@31 21 typedef struct UcxMapElement UcxMapElement;
olaf@31 22 typedef struct UcxMapIterator UcxMapIterator;
olaf@2 23
olaf@20 24 struct UcxMap {
universe@29 25 UcxMapElement **map;
olaf@20 26 size_t size;
olaf@20 27 };
olaf@2 28
olaf@20 29 struct UcxKey {
olaf@20 30 void *data;
olaf@20 31 size_t len;
olaf@20 32 int hash;
olaf@20 33 };
olaf@20 34
olaf@20 35 struct UcxMapElement {
olaf@20 36 void *data;
olaf@20 37 UcxMapElement *next;
olaf@20 38 UcxKey key;
olaf@20 39 };
olaf@20 40
olaf@31 41 struct UcxMapIterator {
olaf@31 42 UcxMap *map;
olaf@31 43 UcxMapElement *cur;
olaf@31 44 int index;
olaf@31 45 };
olaf@31 46
olaf@20 47
olaf@20 48 UcxMap *ucx_map_new(size_t size);
universe@29 49 void ucx_map_free(UcxMap *map);
olaf@20 50
olaf@20 51 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
olaf@20 52 void* ucx_map_get(UcxMap *map, UcxKey key);
olaf@20 53
olaf@30 54 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
universe@29 55 #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
olaf@30 56 #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
universe@29 57 #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
olaf@20 58
olaf@20 59 UcxKey ucx_key(void *data, size_t len);
olaf@20 60
olaf@20 61 int ucx_hash(char *data, size_t len);
olaf@2 62
olaf@31 63 UcxMapIterator ucx_map_iterator(UcxMap *map);
olaf@31 64
olaf@31 65 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
olaf@31 66
universe@42 67 /* use for string maps only, values are not encoded */
universe@42 68 int ucx_map_load(UcxMap *map, FILE *f);
universe@42 69 int ucx_map_store(UcxMap *map, FILE *f);
universe@42 70
universe@42 71 /* TODO:
universe@42 72 int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata);
universe@42 73 int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void* encdata);
universe@42 74 */
universe@42 75
olaf@2 76 #ifdef __cplusplus
olaf@2 77 }
olaf@2 78 #endif
olaf@2 79
olaf@2 80 #endif /* MAP_H */
olaf@2 81

mercurial