ucx/map.h

Fri, 12 Oct 2012 12:08:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 12:08:34 +0200
changeset 71
303dabadff1c
parent 69
fb59270b1de3
child 78
af355652f271
permissions
-rw-r--r--

made the code work with g++ without errors (but warnings)

     1 /*
     2  * 
     3  */
     5 #ifndef MAP_H
     6 #define	MAP_H
     8 #include "ucx.h"
     9 #include "string.h"
    10 #include "mempool.h"
    11 #include <stdio.h>
    13 #ifdef	__cplusplus
    14 extern "C" {
    15 #endif
    17 #define UCX_MAP_FOREACH(elm,iter) \
    18         for(;ucx_map_iter_next(&iter,(void**)&elm)==0;)
    20 typedef struct UcxMap          UcxMap;
    21 typedef struct UcxKey          UcxKey;
    22 typedef struct UcxMapElement   UcxMapElement;
    23 typedef struct UcxMapIterator  UcxMapIterator;
    25 /*
    26  * param 1: element
    27  * param 2: additional data
    28  * param 3: size of encoded data will be stored here
    29  * returns encoded / decoded string or NULL on failure
    30  */
    31 typedef void*(*ucx_map_coder)(void*,void*,size_t*);
    33 struct UcxMap {
    34     UcxMapElement **map;
    35     size_t        size;
    36     size_t        count;
    37 };
    39 struct UcxKey {
    40     void   *data;
    41     size_t len;
    42     int    hash;
    43 };
    45 struct UcxMapElement {
    46     void          *data;
    47     UcxMapElement *next;
    48     UcxKey        key;
    49 };
    51 struct UcxMapIterator {
    52     UcxMap        *map;
    53     UcxMapElement *cur;
    54     int           index;
    55 };
    58 UcxMap *ucx_map_new(size_t size);
    59 void ucx_map_free(UcxMap *map);
    60 /* you cannot clone maps with more than 390 mio entries */
    61 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
    62         copy_func fnc, void *data);
    63 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
    64 int ucx_map_rehash(UcxMap *map);
    66 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
    67 void* ucx_map_get(UcxMap *map, UcxKey key);
    68 void* ucx_map_remove(UcxMap *map, UcxKey key);
    70 #define ucx_map_sstr_put(m, s, d) \
    71     ucx_map_put(m, ucx_key((void*)s.ptr, s.length), d)
    72 #define ucx_map_cstr_put(m, s, d) \
    73     ucx_map_put(m, ucx_key((void*)s, 1+strlen(s)), d)
    74 #define ucx_map_sstr_get(m, s) \
    75     ucx_map_get(m, ucx_key((void*)s.ptr, s.length))
    76 #define ucx_map_cstr_get(m, s) \
    77     ucx_map_get(m, ucx_key((void*)s, 1+strlen(s)))
    78 #define ucx_map_sstr_remove(m, s) \
    79     ucx_map_remove(m, ucx_key((void*)s.ptr, s.length))
    80 #define ucx_map_cstr_remove(m, s) \
    81     ucx_map_remove(m, ucx_key((void*)s, 1+strlen(s)))
    83 UcxKey ucx_key(void *data, size_t len);
    85 int ucx_hash(const char *data, size_t len);
    87 UcxMapIterator ucx_map_iterator(UcxMap *map);
    89 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
    91 /* use macros for string maps only, values are not encoded */
    92 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
    93 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
    95 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
    96         ucx_map_coder decoder, void* decdata);
    97 /* encoders shall provide null terminated strings*/
    98 int ucx_map_store_enc(UcxMap *map, FILE *f,
    99         ucx_map_coder encoder, void* encdata);
   101 #ifdef	__cplusplus
   102 }
   103 #endif
   105 #endif	/* MAP_H */

mercurial