1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
#ifndef __BL_MAP_H__
#define __BL_MAP_H__
#include <string.h> /* memset */
#include "bl_types.h" /* size_t */
#include "bl_debug.h"
#include "bl_mem.h"
#define DEFAULT_MAP_SIZE 16
#define MAP_MARGIN_SIZE 2
#define BL_PAIR(name) __##name##_pair_t
#define BL_PAIR_TYPEDEF(name, key_type, val_type) \
typedef struct __##name##_pair { \
int is_filled; \
key_type key; \
val_type value; \
\
} * __##name##_pair_t
#define BL_MAP(name) __##name##_map_t
#define BL_MAP_TYPEDEF(name, key_type, val_type) \
BL_PAIR_TYPEDEF(name, key_type, val_type); \
typedef struct __##name##_map { \
BL_PAIR(name) pairs; \
BL_PAIR(name) * pairs_array; \
u_int map_size; \
u_int filled_size; \
int (*hash_func)(key_type, u_int); \
int (*compare_func)(key_type, key_type); \
\
} * __##name##_map_t
#define bl_map_new_with_size(key_type, val_type, map, __hash_func, __compare_func, size) \
{ \
if ((map = malloc(sizeof(*(map)))) == NULL || \
((map)->pairs = calloc(size, sizeof(*(map)->pairs))) == NULL) { \
bl_error_printf("malloc() failed in bl_map_new().\n"); \
abort(); \
} \
\
(map)->pairs_array = NULL; \
(map)->map_size = size; \
(map)->filled_size = 0; \
if ((int (*)(int, u_int))__hash_func == bl_map_hash_int) { \
if (size & (size - 1)) { \
(map)->hash_func = (int (*)(key_type, u_int))bl_map_hash_int; \
} else { \
/* new_size == 2^n */ \
(map)->hash_func = (int (*)(key_type, u_int))bl_map_hash_int_fast; \
} \
} else { \
(map)->hash_func = (int (*)(key_type, u_int))__hash_func; \
} \
(map)->compare_func = (int (*)(key_type, key_type))__compare_func; \
}
#define bl_map_new(key_type, val_type, map, __hash_func, __compare_func) \
bl_map_new_with_size(key_type, val_type, map, __hash_func, __compare_func, DEFAULT_MAP_SIZE)
/*
* the deletion of pair->key/pair->value should be done by users of bl_map.
*/
#define bl_map_destroy(map) \
{ \
free((map)->pairs); \
free((map)->pairs_array); \
free(map); \
}
#define bl_map_get(map, __key, __pair_p) \
{ \
u_int filled_size; \
\
__pair_p = NULL; \
\
if ((filled_size = (map)->filled_size) > 0) { \
int __hash_key; \
\
__hash_key = (*(map)->hash_func)(__key, (map)->map_size); \
do { \
if ((map)->pairs[__hash_key].is_filled) { \
if ((*(map)->compare_func)(__key, (map)->pairs[__hash_key].key)) { \
__pair_p = &(map)->pairs[__hash_key]; \
\
break; \
} \
\
filled_size--; \
} \
\
__hash_key = bl_map_rehash(__hash_key, (map)->map_size); \
} while (filled_size > 0); \
} \
\
if (0 && __pair_p) { \
bl_msg_printf("Found key in %d times\n", (map)->filled_size - filled_size + 1); \
} \
}
#if 0
#define bl_map_dump_size(map_size, new_size) \
bl_debug_printf("reallocating map size from %d to %d.\n", map_size, new_size)
#else
#define bl_map_dump_size(map_size, new_size)
#endif
#ifdef HAVE_TYPEOF
#define bl_hash_func_t(map) typeof((map)->hash_func)
#else
/* XXX int (*)() should be int (*)(key_type, u_int) */
#define bl_hash_func_t(map) int (*)()
#endif
#define bl_map_set(result, map, __key, __value) \
{ \
int __hash_key; \
u_int __count; \
\
result = 0; \
\
if ((map)->map_size == (map)->filled_size + MAP_MARGIN_SIZE) { \
/* \
* Expanding map by DEFAULT_MAP_SIZE \
*/ \
\
u_int __new_size; \
void* __new; \
\
__new_size = (map)->map_size + DEFAULT_MAP_SIZE; \
\
bl_map_dump_size((map)->map_size, __new_size); \
\
if ((__new = calloc(__new_size, sizeof(*(map)->pairs)))) { \
void* __old; \
\
__old = (map)->pairs; \
\
if ((int (*)(int, u_int))(map)->hash_func == bl_map_hash_int || \
(int (*)(int, u_int))(map)->hash_func == bl_map_hash_int_fast) { \
if (__new_size & (__new_size - 1)) { \
(map)->hash_func = (bl_hash_func_t(map))bl_map_hash_int; \
} else { \
/* __new_size == 2^n */ \
(map)->hash_func = (bl_hash_func_t(map))bl_map_hash_int_fast; \
} \
} \
\
/* reconstruct (map)->pairs since map_size is changed. */ \
for (__count = 0; __count < (map)->map_size; __count++) { \
if ((map)->pairs[__count].is_filled) { \
void *dst; \
\
__hash_key = (*(map)->hash_func)((map)->pairs[__count].key, __new_size); \
\
(map)->pairs = __new; \
while ((map)->pairs[__hash_key].is_filled) { \
__hash_key = bl_map_rehash(__hash_key, __new_size); \
} \
\
dst = &(map)->pairs[__hash_key]; \
(map)->pairs = __old; \
memcpy(dst, &(map)->pairs[__count], sizeof(*(map)->pairs)); \
} \
} \
\
free(__old); \
(map)->pairs = __new; \
(map)->map_size = __new_size; \
} \
} \
\
__hash_key = (*(map)->hash_func)(__key, (map)->map_size); \
for (__count = 0; __count < (map)->map_size; __count++) { \
if (!(map)->pairs[__hash_key].is_filled) { \
(map)->pairs[__hash_key].key = __key; \
(map)->pairs[__hash_key].value = __value; \
(map)->pairs[__hash_key].is_filled = 1; \
(map)->filled_size++; \
\
free((map)->pairs_array); \
(map)->pairs_array = NULL; \
\
result = 1; \
\
break; \
} \
\
__hash_key = bl_map_rehash(__hash_key, (map)->map_size); \
} \
}
#define __bl_map_erase_simple(result, map, __key) \
int __hash_key; \
u_int __count; \
\
result = 0; \
\
__hash_key = (*(map)->hash_func)(__key, (map)->map_size); \
for (__count = 0; __count < (map)->map_size; __count++) { \
if ((map)->pairs[__hash_key].is_filled && \
(*(map)->compare_func)(__key, (map)->pairs[__hash_key].key)) { \
(map)->pairs[__hash_key].is_filled = 0; \
(map)->filled_size--; \
\
free((map)->pairs_array); \
(map)->pairs_array = NULL; \
\
result = 1; \
\
break; \
} \
\
__hash_key = bl_map_rehash(__hash_key, (map)->map_size); \
}
/*
* Not shrink map.
*/
#define bl_map_erase_simple(result, map, __key) \
{ __bl_map_erase_simple(result, map, __key); }
/*
* Shrink map.
*/
#define bl_map_erase(result, map, __key) \
{ \
__bl_map_erase_simple(result, map, __key); \
\
/* \
* __hash_key and __count are declared in __bl_map_erase_simple(). \
*/ \
\
if (result == 1 && /* \
* if (map)->filled_size is (DEFAULT_MAP_SIZE * 2) \
* smaller than the map size , \
* the map size is (DEFAULT_MAP_SIZE) shrinked. \
* the difference(DEFAULT_MAP_SIZE) is buffered to \
* reduce calling realloc(). \
*/ \
(map)->filled_size + (DEFAULT_MAP_SIZE * 2) < (map)->map_size) { \
/* \
* shrinking map by DEFAULT_MAP_SIZE \
*/ \
\
u_int __new_size; \
void* __old; \
void* __new; \
u_int __count; \
\
__new_size = (map)->map_size - DEFAULT_MAP_SIZE; \
\
bl_map_dump_size((map)->map_size, __new_size); \
\
if ((__new = calloc(__new_size, sizeof(*(map)->pairs)))) { \
__old = (map)->pairs; \
\
if ((int (*)(int, u_int))(map)->hash_func == bl_map_hash_int || \
(int (*)(int, u_int))(map)->hash_func == bl_map_hash_int_fast) { \
if (__new_size & (__new_size - 1)) { \
/* XXX int (*)() should be int (*)(key_type, u_int) */ \
(map)->hash_func = (int (*)())bl_map_hash_int; \
} else { \
/* __new_size == 2^n */ \
/* XXX int (*)() should be int (*)(key_type, u_int) */ \
(map)->hash_func = (int (*)())bl_map_hash_int_fast; \
} \
} \
\
/* reconstruct (map)->pairs since map_size is changed. */ \
for (__count = 0; __count < (map)->map_size; __count++) { \
if ((map)->pairs[__count].is_filled) { \
void *dst; \
\
__hash_key = (*(map)->hash_func)((map)->pairs[__count].key, __new_size); \
\
(map)->pairs = __new; \
while ((map)->pairs[__hash_key].is_filled) { \
__hash_key = bl_map_rehash(__hash_key, __new_size); \
} \
\
dst = &(map)->pairs[__hash_key]; \
(map)->pairs = __old; \
memcpy(dst, &(map)->pairs[__count], sizeof(*(map)->pairs)); \
} \
} \
\
free(__old); \
(map)->pairs = __new; \
(map)->map_size = __new_size; \
} \
} \
}
#define bl_map_get_pairs_array(map, array, size) \
{ \
size = (map)->filled_size; \
\
if ((array = (map)->pairs_array) == NULL) { \
if ((array = calloc(size, sizeof(void*))) == NULL) { \
size = 0; \
} else { \
int __array_count; \
u_int __count; \
\
__array_count = 0; \
for (__count = 0; __count < (map)->map_size; __count++) { \
if ((map)->pairs[__count].is_filled) { \
array[__array_count++] = &(map)->pairs[__count]; \
} \
} \
} \
\
(map)->pairs_array = array; \
} \
}
int bl_map_rehash(int hash_key, u_int size);
/*
* preparing useful hash functions.
*/
int bl_map_hash_str(char *key, u_int size);
int bl_map_hash_int(int key, u_int size);
int bl_map_hash_int_fast(int key, u_int size);
/*
* preparing useful compare functions.
*/
int bl_map_compare_str(char *key1, char *key2);
int bl_map_compare_str_nocase(char *key1, char *key2);
int bl_map_compare_int(int key1, int key2);
#endif
|