Fri, 09 Aug 2013 18:46:07 +0200
changed parameter order of ucx_map_new_a
olaf@2 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@103 | 4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@2 | 27 | */ |
olaf@2 | 28 | |
universe@136 | 29 | /** |
universe@136 | 30 | * @file map.h |
universe@136 | 31 | * |
universe@136 | 32 | * Hash map implementation. |
universe@136 | 33 | * |
universe@136 | 34 | * This implementation uses murmur hash 2 and separate chaining with linked |
universe@136 | 35 | * lists. |
universe@136 | 36 | * |
universe@136 | 37 | * @author Mike Becker |
universe@136 | 38 | * @author Olaf Wintermann |
universe@136 | 39 | */ |
universe@136 | 40 | |
olaf@120 | 41 | #ifndef UCX_MAP_H |
olaf@120 | 42 | #define UCX_MAP_H |
olaf@2 | 43 | |
olaf@20 | 44 | #include "ucx.h" |
olaf@20 | 45 | #include "string.h" |
universe@114 | 46 | #include "allocator.h" |
universe@41 | 47 | #include <stdio.h> |
olaf@20 | 48 | |
olaf@2 | 49 | #ifdef __cplusplus |
olaf@2 | 50 | extern "C" { |
olaf@2 | 51 | #endif |
olaf@2 | 52 | |
olaf@111 | 53 | #define UCX_MAP_FOREACH(key,elm,iter) \ |
olaf@111 | 54 | for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;) |
olaf@31 | 55 | |
olaf@31 | 56 | typedef struct UcxMap UcxMap; |
olaf@31 | 57 | typedef struct UcxKey UcxKey; |
olaf@31 | 58 | typedef struct UcxMapElement UcxMapElement; |
olaf@31 | 59 | typedef struct UcxMapIterator UcxMapIterator; |
olaf@2 | 60 | |
olaf@20 | 61 | struct UcxMap { |
olaf@107 | 62 | UcxAllocator *allocator; |
universe@29 | 63 | UcxMapElement **map; |
olaf@20 | 64 | size_t size; |
olaf@45 | 65 | size_t count; |
olaf@20 | 66 | }; |
olaf@2 | 67 | |
olaf@20 | 68 | struct UcxKey { |
olaf@20 | 69 | void *data; |
olaf@20 | 70 | size_t len; |
olaf@20 | 71 | int hash; |
olaf@20 | 72 | }; |
olaf@20 | 73 | |
olaf@20 | 74 | struct UcxMapElement { |
olaf@20 | 75 | void *data; |
olaf@20 | 76 | UcxMapElement *next; |
olaf@20 | 77 | UcxKey key; |
olaf@20 | 78 | }; |
olaf@20 | 79 | |
olaf@31 | 80 | struct UcxMapIterator { |
olaf@31 | 81 | UcxMap *map; |
olaf@31 | 82 | UcxMapElement *cur; |
universe@95 | 83 | size_t index; |
olaf@31 | 84 | }; |
olaf@31 | 85 | |
universe@136 | 86 | /** |
universe@136 | 87 | * Creates a new hash map with the specified size. |
universe@136 | 88 | * @param size the size of the hash map |
universe@136 | 89 | * @return a pointer to the new hash map |
universe@136 | 90 | */ |
universe@136 | 91 | UcxMap *ucx_map_new(size_t size); |
olaf@20 | 92 | |
universe@136 | 93 | /** |
universe@136 | 94 | * Creates a new hash map with the specified size using an UcxAllocator. |
olaf@137 | 95 | * @param allocator the allocator to use |
universe@136 | 96 | * @param size the size of the hash map |
universe@136 | 97 | * @return a pointer to the new hash map |
universe@136 | 98 | */ |
olaf@137 | 99 | UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size); |
universe@136 | 100 | |
universe@136 | 101 | /** |
universe@136 | 102 | * Frees a hash map. |
universe@136 | 103 | * |
universe@136 | 104 | * <b>Note:</b> the contents are <b>not</b> freed, use an UcxMempool for that |
universe@136 | 105 | * purpose. |
universe@136 | 106 | * |
universe@136 | 107 | * @param map the map to be freed |
universe@136 | 108 | */ |
universe@29 | 109 | void ucx_map_free(UcxMap *map); |
universe@136 | 110 | |
universe@51 | 111 | /* you cannot clone maps with more than 390 mio entries */ |
universe@67 | 112 | int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
universe@67 | 113 | copy_func fnc, void *data); |
olaf@44 | 114 | UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); |
olaf@52 | 115 | int ucx_map_rehash(UcxMap *map); |
olaf@20 | 116 | |
olaf@20 | 117 | int ucx_map_put(UcxMap *map, UcxKey key, void *data); |
olaf@20 | 118 | void* ucx_map_get(UcxMap *map, UcxKey key); |
universe@53 | 119 | void* ucx_map_remove(UcxMap *map, UcxKey key); |
olaf@20 | 120 | |
universe@136 | 121 | /** |
universe@136 | 122 | * Shorthand for putting data with a sstr_t key into the map. |
universe@136 | 123 | * @param map the map |
universe@136 | 124 | * @param key the key |
universe@136 | 125 | * @param value the value |
universe@136 | 126 | * @see ucx_map_put() |
universe@136 | 127 | */ |
universe@136 | 128 | #define ucx_map_sstr_put(map, key, value) \ |
universe@136 | 129 | ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value) |
universe@136 | 130 | /** |
universe@136 | 131 | * Shorthand for putting data with a C string key into the map. |
universe@136 | 132 | * @param map the map |
universe@136 | 133 | * @param key the key |
universe@136 | 134 | * @param value the value |
universe@136 | 135 | * @see ucx_map_put() |
universe@136 | 136 | */ |
universe@136 | 137 | #define ucx_map_cstr_put(map, key, value) \ |
universe@136 | 138 | ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value) |
universe@136 | 139 | /** |
universe@136 | 140 | * Shorthand for putting data with an integer key into the map. |
universe@136 | 141 | * @param map the map |
universe@136 | 142 | * @param key the key |
universe@136 | 143 | * @param value the value |
universe@136 | 144 | * @see ucx_map_put() |
universe@136 | 145 | */ |
universe@136 | 146 | #define ucx_map_int_put(map, key, value) \ |
universe@136 | 147 | ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value) |
olaf@78 | 148 | |
olaf@78 | 149 | |
universe@136 | 150 | /** |
universe@136 | 151 | * Shorthand for getting data from the map with a sstr_t key. |
universe@136 | 152 | * @param map the map |
universe@136 | 153 | * @param key the key |
universe@136 | 154 | * @see ucx_map_get() |
universe@136 | 155 | */ |
universe@136 | 156 | #define ucx_map_sstr_get(map, key) \ |
universe@136 | 157 | ucx_map_get(map, ucx_key(key.ptr, key.length)) |
universe@136 | 158 | /** |
universe@136 | 159 | * Shorthand for getting data from the map with a C string key. |
universe@136 | 160 | * @see ucx_map_get() |
universe@136 | 161 | */ |
universe@136 | 162 | #define ucx_map_cstr_get(map, key) \ |
universe@136 | 163 | ucx_map_get(map, ucx_key((void*)key, strlen(key))) |
universe@136 | 164 | /** |
universe@136 | 165 | * Shorthand for getting data from the map with an integer key. |
universe@136 | 166 | * @param map the map |
universe@136 | 167 | * @param key the key |
universe@136 | 168 | * @see ucx_map_get() |
universe@136 | 169 | */ |
universe@136 | 170 | #define ucx_map_int_get(map, key) \ |
universe@136 | 171 | ucx_map_get(map, ucx_key((void*)&key, sizeof(int))) |
universe@136 | 172 | /** |
universe@136 | 173 | * Shorthand for removing data from the map with a sstr_t key. |
universe@136 | 174 | * @param map the map |
universe@136 | 175 | * @param key the key |
universe@136 | 176 | * @see ucx_map_remove() |
universe@136 | 177 | */ |
universe@136 | 178 | #define ucx_map_sstr_remove(map, key) \ |
universe@136 | 179 | ucx_map_remove(map, ucx_key(key.ptr, key.length)) |
universe@136 | 180 | /** |
universe@136 | 181 | * Shorthand for removing data from the map with a C string key. |
universe@136 | 182 | * @param map the map |
universe@136 | 183 | * @param key the key |
universe@136 | 184 | * @see ucx_map_remove() |
universe@136 | 185 | */ |
universe@136 | 186 | #define ucx_map_cstr_remove(map, key) \ |
universe@136 | 187 | ucx_map_remove(map, ucx_key((void*)key, strlen(key))) |
universe@136 | 188 | /** |
universe@136 | 189 | * Shorthand for removing data from the map with an integer key. |
universe@136 | 190 | * @param map the map |
universe@136 | 191 | * @param key the key |
universe@136 | 192 | * @see ucx_map_remove() |
universe@136 | 193 | */ |
universe@136 | 194 | #define ucx_map_int_remove(map, key) \ |
universe@136 | 195 | ucx_map_remove(map, ucx_key((void*)&key, sizeof(key))) |
olaf@20 | 196 | |
olaf@20 | 197 | UcxKey ucx_key(void *data, size_t len); |
olaf@20 | 198 | |
universe@67 | 199 | int ucx_hash(const char *data, size_t len); |
olaf@2 | 200 | |
olaf@31 | 201 | UcxMapIterator ucx_map_iterator(UcxMap *map); |
olaf@31 | 202 | |
olaf@111 | 203 | int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm); |
olaf@31 | 204 | |
universe@42 | 205 | |
olaf@2 | 206 | #ifdef __cplusplus |
olaf@2 | 207 | } |
olaf@2 | 208 | #endif |
olaf@2 | 209 | |
olaf@120 | 210 | #endif /* UCX_MAP_H */ |
olaf@2 | 211 |