ucx/map.h

changeset 136
b798f2eed26a
parent 120
8170f658f017
child 137
81a02ad99388
child 138
7800811078b8
     1.1 --- a/ucx/map.h	Fri Aug 09 14:36:54 2013 +0200
     1.2 +++ b/ucx/map.h	Fri Aug 09 15:29:26 2013 +0200
     1.3 @@ -26,6 +26,18 @@
     1.4   * POSSIBILITY OF SUCH DAMAGE.
     1.5   */
     1.6  
     1.7 +/**
     1.8 + * @file map.h
     1.9 + * 
    1.10 + * Hash map implementation.
    1.11 + * 
    1.12 + * This implementation uses murmur hash 2 and separate chaining with linked
    1.13 + * lists.
    1.14 + * 
    1.15 + * @author Mike Becker
    1.16 + * @author Olaf Wintermann
    1.17 + */
    1.18 +
    1.19  #ifndef UCX_MAP_H
    1.20  #define	UCX_MAP_H
    1.21  
    1.22 @@ -46,14 +58,6 @@
    1.23  typedef struct UcxMapElement   UcxMapElement;
    1.24  typedef struct UcxMapIterator  UcxMapIterator;
    1.25  
    1.26 -/*
    1.27 - * param 1: element
    1.28 - * param 2: additional data
    1.29 - * param 3: size of encoded data will be stored here
    1.30 - * returns encoded / decoded string or NULL on failure
    1.31 - */
    1.32 -typedef void*(*ucx_map_coder)(void*,void*,size_t*);
    1.33 -
    1.34  struct UcxMap {
    1.35      UcxAllocator  *allocator;
    1.36      UcxMapElement **map;
    1.37 @@ -79,10 +83,31 @@
    1.38      size_t        index;
    1.39  };
    1.40  
    1.41 +/**
    1.42 + * Creates a new hash map with the specified size.
    1.43 + * @param size the size of the hash map
    1.44 + * @return a pointer to the new hash map
    1.45 + */
    1.46 +UcxMap *ucx_map_new(size_t size);
    1.47  
    1.48 -UcxMap *ucx_map_new(size_t size);
    1.49 -UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator);
    1.50 +/**
    1.51 + * Creates a new hash map with the specified size using an UcxAllocator.
    1.52 + * @param size the size of the hash map
    1.53 + * @param allocator the allocator to use
    1.54 + * @return a pointer to the new hash map
    1.55 + */
    1.56 +UcxMap *ucx_map_new_a(size_t size, UcxAllocator *allocator);
    1.57 +
    1.58 +/**
    1.59 + * Frees a hash map.
    1.60 + * 
    1.61 + * <b>Note:</b> the contents are <b>not</b> freed, use an UcxMempool for that
    1.62 + * purpose.
    1.63 + * 
    1.64 + * @param map the map to be freed
    1.65 + */
    1.66  void ucx_map_free(UcxMap *map);
    1.67 +
    1.68  /* you cannot clone maps with more than 390 mio entries */
    1.69  int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
    1.70          copy_func fnc, void *data);
    1.71 @@ -93,26 +118,81 @@
    1.72  void* ucx_map_get(UcxMap *map, UcxKey key);
    1.73  void* ucx_map_remove(UcxMap *map, UcxKey key);
    1.74  
    1.75 -#define ucx_map_sstr_put(m, s, d) \
    1.76 -    ucx_map_put(m, ucx_key(s.ptr, s.length), (void*)d)
    1.77 -#define ucx_map_cstr_put(m, s, d) \
    1.78 -    ucx_map_put(m, ucx_key((void*)s, strlen(s)), (void*)d)
    1.79 -#define ucx_map_int_put(m, i, d) \
    1.80 -    ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), (void*)d)
    1.81 +/**
    1.82 + * Shorthand for putting data with a sstr_t key into the map.
    1.83 + * @param map the map
    1.84 + * @param key the key
    1.85 + * @param value the value
    1.86 + * @see ucx_map_put()
    1.87 + */
    1.88 +#define ucx_map_sstr_put(map, key, value) \
    1.89 +    ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
    1.90 +/**
    1.91 + * Shorthand for putting data with a C string key into the map.
    1.92 + * @param map the map
    1.93 + * @param key the key
    1.94 + * @param value the value
    1.95 + * @see ucx_map_put()
    1.96 + */
    1.97 +#define ucx_map_cstr_put(map, key, value) \
    1.98 +    ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
    1.99 +/**
   1.100 + * Shorthand for putting data with an integer key into the map.
   1.101 + * @param map the map
   1.102 + * @param key the key
   1.103 + * @param value the value
   1.104 + * @see ucx_map_put()
   1.105 + */
   1.106 +#define ucx_map_int_put(map, key, value) \
   1.107 +    ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
   1.108  
   1.109 -#define ucx_map_sstr_get(m, s) \
   1.110 -    ucx_map_get(m, ucx_key(s.ptr, s.length))
   1.111 -#define ucx_map_cstr_get(m, s) \
   1.112 -    ucx_map_get(m, ucx_key((void*)s, strlen(s)))
   1.113 -#define ucx_map_int_get(m, i) \
   1.114 -    ucx_map_get(m, ucx_key((void*)&i, sizeof(int)))
   1.115  
   1.116 -#define ucx_map_sstr_remove(m, s) \
   1.117 -    ucx_map_remove(m, ucx_key(s.ptr, s.length))
   1.118 -#define ucx_map_cstr_remove(m, s) \
   1.119 -    ucx_map_remove(m, ucx_key((void*)s, strlen(s)))
   1.120 -#define ucx_map_int_remove(m, i) \
   1.121 -    ucx_map_remove(m, ucx_key((void*)&i, sizeof(i)))
   1.122 +/**
   1.123 + * Shorthand for getting data from the map with a sstr_t key.
   1.124 + * @param map the map
   1.125 + * @param key the key
   1.126 + * @see ucx_map_get()
   1.127 + */
   1.128 +#define ucx_map_sstr_get(map, key) \
   1.129 +    ucx_map_get(map, ucx_key(key.ptr, key.length))
   1.130 +/**
   1.131 + * Shorthand for getting data from the map with a C string key.
   1.132 + * @see ucx_map_get()
   1.133 + */
   1.134 +#define ucx_map_cstr_get(map, key) \
   1.135 +    ucx_map_get(map, ucx_key((void*)key, strlen(key)))
   1.136 +/**
   1.137 + * Shorthand for getting data from the map with an integer key.
   1.138 + * @param map the map
   1.139 + * @param key the key
   1.140 + * @see ucx_map_get()
   1.141 + */
   1.142 +#define ucx_map_int_get(map, key) \
   1.143 +    ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
   1.144 +/**
   1.145 + * Shorthand for removing data from the map with a sstr_t key.
   1.146 + * @param map the map
   1.147 + * @param key the key
   1.148 + * @see ucx_map_remove()
   1.149 + */
   1.150 +#define ucx_map_sstr_remove(map, key) \
   1.151 +    ucx_map_remove(map, ucx_key(key.ptr, key.length))
   1.152 +/**
   1.153 + * Shorthand for removing data from the map with a C string key.
   1.154 + * @param map the map
   1.155 + * @param key the key
   1.156 + * @see ucx_map_remove()
   1.157 + */
   1.158 +#define ucx_map_cstr_remove(map, key) \
   1.159 +    ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
   1.160 +/**
   1.161 + * Shorthand for removing data from the map with an integer key.
   1.162 + * @param map the map
   1.163 + * @param key the key
   1.164 + * @see ucx_map_remove()
   1.165 + */
   1.166 +#define ucx_map_int_remove(map, key) \
   1.167 +    ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
   1.168  
   1.169  UcxKey ucx_key(void *data, size_t len);
   1.170  

mercurial