ucx/map.h

Thu, 04 Oct 2012 18:46:57 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Oct 2012 18:46:57 +0200
changeset 44
46356d74e873
parent 42
ff3dd1ee7dee
child 45
dd03226c1a6b
permissions
-rw-r--r--

added map clone

     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);
    50 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
    52 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
    53 void* ucx_map_get(UcxMap *map, UcxKey key);
    55 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
    56 #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
    57 #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
    58 #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
    60 UcxKey ucx_key(void *data, size_t len);
    62 int ucx_hash(char *data, size_t len);
    64 UcxMapIterator ucx_map_iterator(UcxMap *map);
    66 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
    68 /* use for string maps only, values are not encoded */
    69 int ucx_map_load(UcxMap *map, FILE *f);
    70 int ucx_map_store(UcxMap *map, FILE *f);
    72 /* TODO:
    73 int ucx_map_load_enc(UcxMap *map, FILE *f, copy_func decoder, void* decdata);
    74 int ucx_map_store_enc(UcxMap *map, FILE *f, copy_func encoder, void* encdata);
    75  */
    77 #ifdef	__cplusplus
    78 }
    79 #endif
    81 #endif	/* MAP_H */

mercurial