ucx/map.h

Fri, 25 May 2012 17:39:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 25 May 2012 17:39:27 +0200
changeset 31
91ac86557290
parent 30
23bb65cbf7a4
child 38
35f67a8ef875
permissions
-rw-r--r--

added map iterator

     1 /*
     2  * 
     3  */
     5 #ifndef MAP_H
     6 #define	MAP_H
     8 #include "ucx.h"
     9 #include "string.h"
    11 #ifdef	__cplusplus
    12 extern "C" {
    13 #endif
    15 #define UCX_MAP_FOREACH(type,elm,map,iter) \
    16         for(type elm;ucx_map_iter_next(&iter,(void*)&elm)==0;)
    18 typedef struct UcxMap          UcxMap;
    19 typedef struct UcxKey          UcxKey;
    20 typedef struct UcxMapElement   UcxMapElement;
    21 typedef struct UcxMapIterator  UcxMapIterator;
    23 struct UcxMap {
    24     UcxMapElement **map;
    25     size_t        size;
    26 };
    28 struct UcxKey {
    29     void   *data;
    30     size_t len;
    31     int    hash;
    32 };
    34 struct UcxMapElement {
    35     void          *data;
    36     UcxMapElement *next;
    37     UcxKey        key;
    38 };
    40 struct UcxMapIterator {
    41     UcxMap        *map;
    42     UcxMapElement *cur;
    43     int           index;
    44 };
    47 UcxMap *ucx_map_new(size_t size);
    48 void ucx_map_free(UcxMap *map);
    50 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
    51 void* ucx_map_get(UcxMap *map, UcxKey key);
    53 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
    54 #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
    55 #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
    56 #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
    58 UcxKey ucx_key(void *data, size_t len);
    60 int ucx_hash(char *data, size_t len);
    62 UcxMapIterator ucx_map_iterator(UcxMap *map);
    64 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
    66 #ifdef	__cplusplus
    67 }
    68 #endif
    70 #endif	/* MAP_H */

mercurial