Fri, 25 May 2012 17:39:27 +0200
added map iterator
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" |
olaf@20 | 10 | |
olaf@2 | 11 | #ifdef __cplusplus |
olaf@2 | 12 | extern "C" { |
olaf@2 | 13 | #endif |
olaf@2 | 14 | |
olaf@31 | 15 | #define UCX_MAP_FOREACH(type,elm,map,iter) \ |
olaf@31 | 16 | for(type elm;ucx_map_iter_next(&iter,(void*)&elm)==0;) |
olaf@31 | 17 | |
olaf@31 | 18 | typedef struct UcxMap UcxMap; |
olaf@31 | 19 | typedef struct UcxKey UcxKey; |
olaf@31 | 20 | typedef struct UcxMapElement UcxMapElement; |
olaf@31 | 21 | typedef struct UcxMapIterator UcxMapIterator; |
olaf@2 | 22 | |
olaf@20 | 23 | struct UcxMap { |
universe@29 | 24 | UcxMapElement **map; |
olaf@20 | 25 | size_t size; |
olaf@20 | 26 | }; |
olaf@2 | 27 | |
olaf@20 | 28 | struct UcxKey { |
olaf@20 | 29 | void *data; |
olaf@20 | 30 | size_t len; |
olaf@20 | 31 | int hash; |
olaf@20 | 32 | }; |
olaf@20 | 33 | |
olaf@20 | 34 | struct UcxMapElement { |
olaf@20 | 35 | void *data; |
olaf@20 | 36 | UcxMapElement *next; |
olaf@20 | 37 | UcxKey key; |
olaf@20 | 38 | }; |
olaf@20 | 39 | |
olaf@31 | 40 | struct UcxMapIterator { |
olaf@31 | 41 | UcxMap *map; |
olaf@31 | 42 | UcxMapElement *cur; |
olaf@31 | 43 | int index; |
olaf@31 | 44 | }; |
olaf@31 | 45 | |
olaf@20 | 46 | |
olaf@20 | 47 | UcxMap *ucx_map_new(size_t size); |
universe@29 | 48 | void ucx_map_free(UcxMap *map); |
olaf@20 | 49 | |
olaf@20 | 50 | int ucx_map_put(UcxMap *map, UcxKey key, void *data); |
olaf@20 | 51 | void* ucx_map_get(UcxMap *map, UcxKey key); |
olaf@20 | 52 | |
olaf@30 | 53 | #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) |
universe@29 | 54 | #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d) |
olaf@30 | 55 | #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length)) |
universe@29 | 56 | #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s))) |
olaf@20 | 57 | |
olaf@20 | 58 | UcxKey ucx_key(void *data, size_t len); |
olaf@20 | 59 | |
olaf@20 | 60 | int ucx_hash(char *data, size_t len); |
olaf@2 | 61 | |
olaf@31 | 62 | UcxMapIterator ucx_map_iterator(UcxMap *map); |
olaf@31 | 63 | |
olaf@31 | 64 | int ucx_map_iter_next(UcxMapIterator *i, void **elm); |
olaf@31 | 65 | |
olaf@2 | 66 | #ifdef __cplusplus |
olaf@2 | 67 | } |
olaf@2 | 68 | #endif |
olaf@2 | 69 | |
olaf@2 | 70 | #endif /* MAP_H */ |
olaf@2 | 71 |