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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
/*
*
* This is free software. You can redistribute it and/or modify under
* the terms of the GNU General Public License version 2.
*
* Copyright (C) 1998 by kra
*
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "hash.h"
static int __chose_table_size(int size)
{
static int ht_size[] = {
37, 47, 79, 97, 163, 197, 331, 397, 673, 797, 1361, 1597,
2729, 3203, 5471, 6421, 10949, 12853, 21911, 25717, 43853,
51437, 87719, 102877, 175447, 205759, 350899, 411527, 701819,
823117, 1403641, 1646237, 2807303, 3292489, 5614657, 6584983,
11229331, 13169977, 22458671, 26339969, 44917381, 52679969,
89834777, 105359939
};
int i;
if (size >= ht_size[sizeof(ht_size) / sizeof(ht_size[0]) - 1])
return ht_size[sizeof(ht_size) / sizeof(ht_size[0])];
for (i = 0; ht_size[i] < size; i++)
;
return ht_size[i];
}
static inline int __when_need_space(int max_items)
{
return (HASH_MAX_PCT_FULL * max_items) / 100;
}
static inline int __hash_need_space(struct hash *h)
{
return h->h_items >= h->h_space_after ? 1 : 0;
// return (h->h_items * 100) / h->h_max_items >= HASH_MAX_PCT_FULL ? 1 : 0;
}
int hash_init(struct hash *h, int max_items, hash_equal_func eqfunc)
{
if (max_items < HASH_MIN_ITEMS)
max_items = HASH_MIN_ITEMS;
max_items = __chose_table_size(max_items);
h->h_space_after = __when_need_space(max_items);
h->h_items = 0;
h->h_max_items = max_items;
h->h_eqfunc = eqfunc;
if (!(h->h_table = malloc(max_items * sizeof(struct hash_table_item))))
return -1;
memset(h->h_table, 0, max_items * sizeof(struct hash_table_item));
#ifdef _REENTRANT
h->h_locked = 0;
pthread_mutex_init(&h->h_mutex, NULL);
#endif
return 0;
}
void hash_free(struct hash *h)
{
if (h->h_table)
free(h->h_table);
memset(h, 0, sizeof(*h));
}
static unsigned int __hv_hash;
static unsigned int __hv_hash_save;
static unsigned int __hv_max_items;
#ifdef HASH_FUNC_2
static unsigned int __hv_d;
static unsigned int __hv_d_save;
#endif
static inline int __hv(unsigned int key, unsigned int max_items)
{
__hv_max_items = max_items;
#ifdef HASH_FUNC_2
__hv_d = 2;
#endif
return (int)(__hv_hash = key % max_items);
}
static inline int __hv_peek(void)
{
return __hv_hash;
}
static inline int __hv_next(void)
{
#ifdef HASH_FUNC_2
__hv_hash = (__hv_hash + __hv_d) % __hv_max_items;
__hv_d += 2;
return (int)__hv_hash;
#else
return __hv_hash = (__hv_hash + 1) % __hv_max_items;
#endif
}
static inline int __hv_peek_next(void)
{
#ifdef HASH_FUNC2
return (int)((__hv_hash + __hv_d) % __hv_max_items);
#else
return (__hv_hash + 1) % __hv_max_items;
#endif
}
static inline void __hv_save_position(void)
{
#ifdef HASH_FUNC_2
__hv_d_save = __hv_d;
#endif
__hv_hash_save = __hv_hash;
}
static inline int __hv_load_position(void)
{
#ifdef HASH_FUNC_2
__hv_d = __hv_d_save;
#endif
return (int)(__hv_hash = __hv_hash_save);
}
static int __hash_find(struct hash *h, unsigned int key, void *arg)
{
int retval;
int hv;
struct hash_table_item *h_table;
hv = __hv(key, h->h_max_items);
h_table = h->h_table;
retval = -1;
while (h_table[hv].ht_data) {
if (h_table[hv].ht_key == key) {
if (!h->h_eqfunc ||
h_table[__hv_peek_next()].ht_data == NULL) {
retval = hv;
break;
} else {
if (h->h_eqfunc(key, h_table[hv].ht_data, arg)) {
retval = hv;
break;
}
}
}
hv = __hv_next();
}
return retval;
}
static void __hash_put(struct hash *h, unsigned int key, void *data)
{
int hv;
struct hash_table_item *h_table;
hv = __hv(key, h->h_max_items);
h_table = h->h_table;
while (h_table[hv].ht_data) {
hv = __hv_next();
}
h_table[hv].ht_key = key;
h_table[hv].ht_data = data;
h->h_items++;
}
static int __hash_put_check(struct hash *h, unsigned int key,
void *data, void *arg)
{
int retval;
int hv;
struct hash_table_item *h_table = h->h_table;
retval = __hash_find(h, key, arg);
if (retval < 0) {
hv = __hv_peek();
h_table[hv].ht_key = key;
h_table[hv].ht_data = data;
return 0;
} else
return -1;
}
static void __hash_remap(struct hash *h, int hv)
{
int count, i;
struct hash_table_item *h_table, *ht_save;
__hv_save_position();
h_table = h->h_table;
count = 0;
while (h_table[hv].ht_data) {
count++;
hv = __hv_next();
}
if (count) {
ht_save = alloca(count * sizeof(struct hash_table_item));
hv = __hv_load_position();
i = 0;
while (h_table[hv].ht_data) {
ht_save[i].ht_key = h_table[hv].ht_key;
ht_save[i].ht_data = h_table[hv].ht_data;
h_table[hv].ht_key = 0;
h_table[hv].ht_data = NULL;
i++;
hv = __hv_next();
}
h->h_items -= count;
for (i = 0; i < count; i++)
__hash_put(h, ht_save[i].ht_key, ht_save[i].ht_data);
}
}
static void __hash_new_space(struct hash *h)
{
struct hash_table_item *old_h_table;
int old_items, old_max_items;
int i;
old_items = h->h_items;
old_max_items = h->h_max_items;
old_h_table = h->h_table;
h->h_max_items = __chose_table_size(h->h_max_items +
(HASH_SPACE_PCT_INC * h->h_max_items) / 100);
if (!(h->h_table = malloc(h->h_max_items * sizeof(struct hash_table_item)))) {
h->h_items = old_items;
h->h_max_items = old_max_items;
h->h_table = old_h_table;
return;
}
h->h_space_after = __when_need_space(h->h_max_items);
memset(h->h_table, 0, h->h_max_items * sizeof(struct hash_table_item));
h->h_items = 0;
for (i = 0; i < old_max_items; i++) {
if (old_h_table[i].ht_data)
__hash_put(h, old_h_table[i].ht_key, old_h_table[i].ht_data);
}
free(old_h_table);
}
static inline void __lock(struct hash *h)
{
#ifdef _REENTRANT
if (!h->h_locked || h->h_locked_thr != pthread_self())
pthread_mutex_lock(&h->h_mutex);
#endif
}
static inline void __unlock(struct hash *h)
{
#ifdef _REENTRANT
if (!h->h_locked || h->h_locked_thr != pthread_self())
pthread_mutex_unlock(&h->h_mutex);
#endif
}
void *hash_get(struct hash *h, unsigned int key, void *arg)
{
int hv;
void *retval;
__lock(h);
if ((hv = __hash_find(h, key, arg)) >= 0)
retval = h->h_table[hv].ht_data;
else
retval = NULL;
__unlock(h);
return retval;
}
void *hash_remove(struct hash *h, unsigned int key, void *arg)
{
void *retval;
int hv;
__lock(h);
if ((hv = __hash_find(h, key, arg)) >= 0) {
retval = h->h_table[hv].ht_data;
h->h_table[hv].ht_key = 0;
h->h_table[hv].ht_data = NULL;
h->h_items--;
__hash_remap(h, __hv_next());
} else
retval = NULL;
__unlock(h);
return retval;
}
int hash_put(struct hash *h, unsigned int key, void *data)
{
int retval;
__lock(h);
if (__hash_need_space(h))
__hash_new_space(h);
if (h->h_items == h->h_max_items || !data)
retval = -1;
else
retval = 0;
__hash_put(h, key, data);
__unlock(h);
return retval;
}
int hash_put_check(struct hash *h, unsigned int key, void *data, void *arg)
{
int retval;
__lock(h);
if (__hash_need_space(h))
__hash_new_space(h);
if (h->h_items == h->h_max_items || !data)
retval = -1;
else
retval = __hash_put_check(h, key, data, arg);
__unlock(h);
return retval;
}
int hash_count(struct hash *h)
{
int retval;
__lock(h);
retval = h->h_items;
__unlock(h);
return retval;
}
void hash_lock(struct hash *h)
{
#ifdef _REENTRANT
if (!h->h_locked || h->h_locked_thr != pthread_self()) {
pthread_mutex_lock(&h->h_mutex);
h->h_locked_thr = pthread_self();
h->h_locked = 1;
} else
h->h_locked++;
#endif
}
void hash_unlock(struct hash *h)
{
#ifdef _REENTRANT
if (--h->h_locked == 0)
pthread_mutex_unlock(&h->h_mutex);
#endif
}
/*
*
* hash_iterator
*
*/
void hash_iter_set(struct hash_iterator *i, struct hash *h)
{
i->i_hash = h;
i->i_pos = 0;
}
void hash_iter_lock(struct hash_iterator *i)
{
hash_lock(i->i_hash);
}
void hash_iter_unlock(struct hash_iterator *i)
{
hash_unlock(i->i_hash);
}
void *hash_iter_get(struct hash_iterator *i, unsigned int *keyptr)
{
struct hash *h;
void *data;
void *retval;
h = i->i_hash;
data = NULL;
__lock(h);
for ( ;i->i_pos < h->h_max_items; i->i_pos++) {
data = h->h_table[i->i_pos].ht_data;
if (data)
break;
}
if (i->i_pos < h->h_max_items && data) {
retval = data;
if (keyptr)
*keyptr = h->h_table[i->i_pos].ht_key;
} else
retval = NULL;
i->i_pos++;
__unlock(h);
return retval;
}
void hash_iter_end(struct hash_iterator *i)
{
i->i_hash = NULL;
i->i_pos = 0;
}
#ifdef TEST
/*
*
* Test
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <assert.h>
#include <alloca.h>
int MAX_ITEMS = 1000;
static int verbose = 0;
static int verbose_print(int level, char *format, ...)
{
va_list ap;
int retval;
if (verbose >= level) {
va_start(ap, format);
retval = vprintf(format, ap);
va_end(ap);
return retval;
}
return 0;
}
int main(int argc, char *argv[])
{
struct hash h;
struct hash_iterator hi;
int i;
int *t, *t_arr;
while ((i = getopt(argc, argv, "vi:")) != -1) {
switch (i) {
case 'v':
++verbose;
break;
case 'i':
MAX_ITEMS = atoi(optarg);
break;
default:
fprintf(stderr, "bad option\n");
exit(1);
}
}
assert(t_arr = malloc(MAX_ITEMS * sizeof(int)));
hash_init(&h, 100, NULL);
verbose_print(1, "start\n");
for (i = 0; i < MAX_ITEMS; i++) {
t = t_arr + i;
*t = i;
assert(hash_put(&h, i * i, t) == 0);
}
assert(MAX_ITEMS == hash_count(&h));
verbose_print(1, "\nhash get\n");
for (i = 0; i < MAX_ITEMS; i++) {
t = hash_get(&h, i * i, NULL);
assert(t);
assert(*t == i);
verbose_print(2, "%d ", *t);
}
verbose_print(1, "\niterator test\n");
hash_iter_set(&hi, &h);
while ((t = hash_iter_get(&hi, NULL)))
verbose_print(2, "%d ", *t);
hash_iter_end(&hi);
verbose_print(1, "\nremove test\n");
for (i = 0; i < MAX_ITEMS; i += 3) {
t = hash_remove(&h, i * i, NULL);
assert(t);
assert(*t == i);
verbose_print(2, "%d ", *t);
}
verbose_print(1, "\nhash_put_check test\n");
for (i = 0; i < MAX_ITEMS; i += 3) {
t = t_arr + i;
assert(hash_put_check(&h, i * i, t, NULL) == 0);
verbose_print(2, "%d ", *t);
}
for (i = 0; i < MAX_ITEMS; i += 3) {
t = t_arr + i;
assert(hash_put_check(&h, i * i, t, NULL) < 0);
verbose_print(2, "%d ", *t);
}
for (i = 0; i < MAX_ITEMS; i += 3) {
t = hash_remove(&h, i * i, NULL);
assert(t);
assert(*t == i);
verbose_print(2, "%d ", *t);
}
verbose_print(1, "\nhash get\n");
for (i = 0; i < MAX_ITEMS; i++) {
t = hash_get(&h, i * i, NULL);
if ((i % 3) == 0)
assert(t == NULL);
else {
assert(t);
assert(*t == i);
verbose_print(2, "%d ", *t);
}
}
hash_free(&h);
verbose_print(1, "\nOK\n");
return 0;
}
#endif
|