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

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

mercurial