Hash :
076487dd
Author :
Date :
1997-09-21T04:41:19
(hash_free_0): Remove prototype. Move function to precede first use.
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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
/* A generic hash table package. */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "hash.h"
#ifdef USE_OBSTACK
# define ZALLOC(Ht, N) obstack_alloc (&(ht->ht_obstack), (N))
#else
# define ZALLOC(Ht, N) malloc ((N))
#endif
#define BUCKET_HEAD(ht, idx) ((ht)->hash_table[(idx)])
static int
is_prime (candidate)
unsigned long candidate;
{
/* No even number and none less than 10 will be passed here. */
unsigned long divn = 3;
unsigned long sq = divn * divn;
while (sq < candidate && (candidate % divn))
{
divn++;
sq += 4 * divn;
divn++;
}
return (candidate % divn);
}
/* Round a given number up to the nearest prime. */
static unsigned long
next_prime (candidate)
unsigned long candidate;
{
/* Make it definitely odd. */
candidate |= 1;
while (!is_prime (candidate))
candidate += 2;
return candidate;
}
static void
hash_free_entry (HT *ht, HASH_ENT *e)
{
e->key = NULL;
e->next = ht->hash_free_entry_list;
ht->hash_free_entry_list = e;
}
static HASH_ENT *
hash_allocate_entry (HT *ht)
{
HASH_ENT *new;
if (ht->hash_free_entry_list)
{
new = ht->hash_free_entry_list;
ht->hash_free_entry_list = new->next;
}
else
{
new = (HASH_ENT *) ZALLOC (ht, sizeof (HASH_ENT));
}
return new;
}
unsigned int
hash_get_n_slots_used (const HT *ht)
{
return ht->hash_n_slots_used;
}
/* Free all storage associated with HT that functions in this package
have allocated. If a key_freer function has been supplied (when HT
was created), this function applies it to the key of each entry before
freeing that entry. */
static void
hash_free_0 (HT *ht, int free_user_data)
{
if (free_user_data && ht->hash_key_freer != NULL)
{
unsigned int i;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p;
HASH_ENT *next;
for (p = BUCKET_HEAD (ht, i); p; p = next)
{
next = p->next;
ht->hash_key_freer (p->key);
}
}
}
#ifdef USE_OBSTACK
obstack_free (&(ht->ht_obstack), NULL);
#else
{
unsigned int i;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p;
HASH_ENT *next;
for (p = BUCKET_HEAD (ht, i); p; p = next)
{
next = p->next;
free (p);
}
}
}
#endif
ht->hash_free_entry_list = NULL;
free (ht->hash_table);
}
/* FIXME-comment */
int
hash_rehash (HT *ht, unsigned int new_table_size)
{
HT *ht_new;
unsigned int i;
if (ht->hash_table_size <= 0 || new_table_size == 0)
return 1;
ht_new = hash_initialize (new_table_size, ht->hash_key_freer,
ht->hash_hash, ht->hash_key_comparator);
if (ht_new == NULL)
return 1;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p = BUCKET_HEAD (ht, i);
for ( /* empty */ ; p; p = p->next)
{
int failed;
const void *already_in_table;
already_in_table = hash_insert_if_absent (ht_new, p->key, &failed);
assert (failed == 0 && already_in_table == 0);
}
}
hash_free_0 (ht, 0);
#ifdef TESTING
assert (hash_table_ok (ht_new));
#endif
*ht = *ht_new;
free (ht_new);
/* FIXME: fill in ht_new->n_slots_used and other statistics fields. */
return 0;
}
/* FIXME-comment */
unsigned int
hash_get_max_chain_length (HT *ht)
{
unsigned int i;
unsigned int max_chain_length = 0;
if (!ht->hash_dirty_max_chain_length)
return ht->hash_max_chain_length;
for (i = 0; i < ht->hash_table_size; i++)
{
unsigned int chain_length = 0;
HASH_ENT *p = BUCKET_HEAD (ht, i);
for ( /* empty */ ; p; p = p->next)
++chain_length;
if (chain_length > max_chain_length)
max_chain_length = chain_length;
}
ht->hash_max_chain_length = max_chain_length;
ht->hash_dirty_max_chain_length = 0;
return ht->hash_max_chain_length;
}
unsigned int
hash_get_n_keys (const HT *ht)
{
return ht->hash_n_keys;
}
unsigned int
hash_get_table_size (const HT *ht)
{
return ht->hash_table_size;
}
/* CANDIDATE_TABLE_SIZE need not be prime. If WHEN_TO_REHASH (FIXME: add
this parameter) is positive, when that percentage of table entries have
been used, the table size is increased; then a new, larger table
(GROW_FACTOR (FIXME: maybe add this parameter) times larger than the previous
size) is allocated and all entries in the old table are rehashed into
the new, larger one. The old table is freed. If WHEN_TO_REHASH is zero
or negative, the table is never resized.
The function returns non-zero
- if CANDIDATE_TABLE_SIZE is zero or negative
- if KEY_COMPARATOR or HASH is null
- if it was unable to allocate sufficient storage for the hash table
- if WHEN_TO_REHASH is zero or negative
Otherwise it returns zero. */
HT *
hash_initialize (unsigned int candidate_table_size,
Hash_key_freer_type key_freer,
unsigned int (*hash) (const void *, unsigned int),
int (*key_comparator) (const void *, const void *))
{
HT *ht;
unsigned int i;
unsigned int table_size;
if (candidate_table_size <= 0)
return NULL;
if (hash == NULL || key_comparator == NULL)
return NULL;
ht = (HT *) malloc (sizeof (HT));
if (ht == NULL)
return NULL;
table_size = next_prime (candidate_table_size);
ht->hash_table = (HASH_ENT **) malloc (table_size * sizeof (HASH_ENT *));
if (ht->hash_table == NULL)
return NULL;
for (i = 0; i < table_size; i++)
{
BUCKET_HEAD (ht, i) = NULL;
}
ht->hash_free_entry_list = NULL;
ht->hash_table_size = table_size;
ht->hash_hash = hash;
ht->hash_key_comparator = key_comparator;
ht->hash_key_freer = key_freer;
ht->hash_n_slots_used = 0;
ht->hash_max_chain_length = 0;
ht->hash_n_keys = 0;
ht->hash_dirty_max_chain_length = 0;
#ifdef USE_OBSTACK
obstack_init (&(ht->ht_obstack));
#endif
return ht;
}
/* This private function is used to help with insertion and deletion.
If E does *not* compare equal to the key of any entry in the table,
return NULL.
When E matches an entry in the table, return a pointer to the matching
entry. When DELETE is non-zero and E matches an entry in the table,
unlink the matching entry. Set *CHAIN_LENGTH to the number of keys
that have hashed to the bucket E hashed to. */
static HASH_ENT *
hash_find_entry (HT *ht, const void *e, unsigned int *table_idx,
unsigned int *chain_length, int delete)
{
unsigned int idx;
int found;
HASH_ENT *p, *prev;
idx = ht->hash_hash (e, ht->hash_table_size);
assert (idx < ht->hash_table_size);
*table_idx = idx;
*chain_length = 0;
prev = ht->hash_table[idx];
if (prev == NULL)
return NULL;
*chain_length = 1;
if (ht->hash_key_comparator (e, prev->key) == 0)
{
if (delete)
ht->hash_table[idx] = prev->next;
return prev;
}
p = prev->next;
found = 0;
while (p)
{
++(*chain_length);
if (ht->hash_key_comparator (e, p->key) == 0)
{
found = 1;
break;
}
prev = p;
p = p->next;
}
if (!found)
return NULL;
assert (p != NULL);
if (delete)
prev->next = p->next;
return p;
}
/* Return non-zero if E is already in the table, zero otherwise. */
int
hash_query_in_table (const HT *ht, const void *e)
{
unsigned int idx;
HASH_ENT *p;
idx = ht->hash_hash (e, ht->hash_table_size);
assert (idx < ht->hash_table_size);
for (p = BUCKET_HEAD (ht, idx); p != NULL; p = p->next)
if (ht->hash_key_comparator (e, p->key) == 0)
return 1;
return 0;
}
void *
hash_lookup (const HT *ht, const void *e)
{
unsigned int idx;
HASH_ENT *p;
idx = ht->hash_hash (e, ht->hash_table_size);
assert (idx < ht->hash_table_size);
for (p = BUCKET_HEAD (ht, idx); p != NULL; p = p->next)
if (ht->hash_key_comparator (e, p->key) == 0)
return p->key;
return NULL;
}
/* If E matches an entry already in the hash table, don't modify the
table and return a pointer to the matched entry. If E does not
match any item in the table, insert E and return NULL.
If the storage required for insertion cannot be allocated
set *FAILED to non-zero and return NULL. */
void *
hash_insert_if_absent (HT *ht, const void *e, int *failed)
{
const HASH_ENT *ent;
HASH_ENT *new;
unsigned int idx;
unsigned int chain_length;
assert (e != NULL); /* Can't insert a NULL key. */
*failed = 0;
ent = hash_find_entry (ht, e, &idx, &chain_length, 0);
if (ent != NULL)
{
/* E matches a key from an entry already in the table. */
return ent->key;
}
new = hash_allocate_entry (ht);
if (new == NULL)
{
*failed = 1;
return NULL;
}
new->key = (void *) e;
new->next = BUCKET_HEAD (ht, idx);
BUCKET_HEAD (ht, idx) = new;
if (chain_length == 0)
++(ht->hash_n_slots_used);
/* The insertion has just increased chain_length by 1. */
++chain_length;
if (chain_length > ht->hash_max_chain_length)
ht->hash_max_chain_length = chain_length;
++(ht->hash_n_keys);
if ((double) ht->hash_n_keys / ht->hash_table_size > 0.80)
{
unsigned int new_size;
new_size = next_prime (2 * ht->hash_table_size + 1);
*failed = hash_rehash (ht, new_size);
}
#ifdef TESTING
assert (hash_table_ok (ht));
#endif
return NULL;
}
/* If E is already in the table, remove it and return a pointer to
the just-deleted key (the user may want to deallocate its storage).
If E is not in the table, don't modify the table and return NULL. */
void *
hash_delete_if_present (HT *ht, const void *e)
{
HASH_ENT *ent;
void *key;
unsigned int idx;
unsigned int chain_length;
ent = hash_find_entry (ht, e, &idx, &chain_length, 1);
if (ent == NULL)
return NULL;
if (ent->next == NULL && chain_length == 1)
--(ht->hash_n_slots_used);
key = ent->key;
--(ht->hash_n_keys);
ht->hash_dirty_max_chain_length = 1;
if (ent->next == NULL && chain_length < ht->hash_max_chain_length)
ht->hash_dirty_max_chain_length = 0;
hash_free_entry (ht, ent);
#ifdef TESTING
assert (hash_table_ok (ht));
#endif
return key;
}
void
hash_print_statistics (const HT *ht, FILE *stream)
{
unsigned int n_slots_used;
unsigned int n_keys;
unsigned int max_chain_length;
int err;
err = hash_get_statistics (ht, &n_slots_used, &n_keys, &max_chain_length);
assert (err == 0);
fprintf (stream, "table size: %d\n", ht->hash_table_size);
fprintf (stream, "# slots used: %u (%.2f%%)\n", n_slots_used,
(100.0 * n_slots_used) / ht->hash_table_size);
fprintf (stream, "# keys: %u\n", n_keys);
fprintf (stream, "max chain length: %u\n", max_chain_length);
}
/* If there is *NO* table (so, no meaningful stats) return non-zero
and don't reference the argument pointers. Otherwise compute the
performance statistics and return non-zero. */
int
hash_get_statistics (const HT *ht,
unsigned int *n_slots_used,
unsigned int *n_keys,
unsigned int *max_chain_length)
{
unsigned int i;
if (ht == NULL || ht->hash_table == NULL)
return 1;
*max_chain_length = 0;
*n_slots_used = 0;
*n_keys = 0;
for (i = 0; i < ht->hash_table_size; i++)
{
unsigned int chain_length = 0;
HASH_ENT *p;
p = BUCKET_HEAD (ht, i);
if (p != NULL)
++(*n_slots_used);
for (; p; p = p->next)
++chain_length;
*n_keys += chain_length;
if (chain_length > *max_chain_length)
*max_chain_length = chain_length;
}
return 0;
}
int
hash_table_ok (HT *ht)
{
int code;
unsigned int n_slots_used;
unsigned int n_keys;
unsigned int max_chain_length;
if (ht == NULL || ht->hash_table == NULL)
return 1;
code = hash_get_statistics (ht, &n_slots_used, &n_keys,
&max_chain_length);
if (code != 0
|| n_slots_used != ht->hash_n_slots_used
|| n_keys != ht->hash_n_keys
|| max_chain_length != hash_get_max_chain_length (ht))
return 0;
return 1;
}
/* See hash_do_for_each_2 (below) for a variant. */
void
hash_do_for_each (HT *ht, void (*f) (void *e, void *aux), void *aux)
{
unsigned int i;
#ifdef TESTING
assert (hash_table_ok (ht));
#endif
if (ht->hash_table == NULL)
return;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p;
for (p = BUCKET_HEAD (ht, i); p; p = p->next)
{
(*f) (p->key, aux);
}
}
}
/* Just like hash_do_for_each, except that function F returns an int
that can signal (when non-zero) we should return early. */
int
hash_do_for_each_2 (HT *ht, int (*f) (void *e, void *aux), void *aux)
{
unsigned int i;
#ifdef TESTING
assert (hash_table_ok (ht));
#endif
if (ht->hash_table == NULL)
return 0;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p;
for (p = BUCKET_HEAD (ht, i); p; p = p->next)
{
int return_code;
return_code = (*f) (p->key, aux);
if (return_code != 0)
return return_code;
}
}
return 0;
}
/* For each entry in the bucket addressed by BUCKET_KEY of the hash
table HT, invoke the function F. If F returns non-zero, stop
iterating and return that value. Otherwise, apply F to all entries
in the selected bucket and return zero. The AUX argument to this
function is passed as the last argument in each invocation of F.
The first argument to F is BUCKET_KEY, and the second is the key of
an entry in the selected bucket. */
int
hash_do_for_each_in_selected_bucket (HT *ht, const void *bucket_key,
int (*f) (const void *bucket_key,
void *e, void *aux),
void *aux)
{
int idx;
HASH_ENT *p;
#ifdef TESTING
assert (hash_table_ok (ht));
#endif
if (ht->hash_table == NULL)
return 0;
idx = ht->hash_hash (bucket_key, ht->hash_table_size);
for (p = BUCKET_HEAD (ht, idx); p != NULL; p = p->next)
{
int return_code;
return_code = (*f) (bucket_key, p->key, aux);
if (return_code != 0)
return return_code;
}
return 0;
}
/* Make all buckets empty, placing any chained entries on the free list.
As with hash_free, apply the user-specified function key_freer
(if it's not NULL) to the keys of any affected entries. */
void
hash_clear (HT *ht)
{
unsigned int i;
HASH_ENT *p;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *tail = NULL;
HASH_ENT *head = BUCKET_HEAD (ht, i);
/* Free any keys and get tail pointer to last entry in chain. */
for (p = head; p; p = p->next)
{
if (ht->hash_key_freer != NULL)
ht->hash_key_freer (p->key);
p->key = NULL; /* Make sure no one tries to use this key later. */
tail = p;
}
BUCKET_HEAD (ht, i) = NULL;
/* If there's a chain in this bucket, tack it onto the
beginning of the free list. */
if (head != NULL)
{
assert (tail != NULL && tail->next == NULL);
tail->next = ht->hash_free_entry_list;
ht->hash_free_entry_list = head;
}
}
ht->hash_n_slots_used = 0;
ht->hash_max_chain_length = 0;
ht->hash_n_keys = 0;
ht->hash_dirty_max_chain_length = 0;
}
void
hash_free (HT *ht)
{
hash_free_0 (ht, 1);
free (ht);
}
#ifdef TESTING
void
hash_print (const HT *ht)
{
int i;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p;
if (BUCKET_HEAD (ht, i) != NULL)
printf ("%d:\n", i);
for (p = BUCKET_HEAD (ht, i); p; p = p->next)
{
char *s = (char *) p->key;
/* FIXME */
printf (" %s\n", s);
}
}
}
#endif /* TESTING */
void
hash_get_key_list (const HT *ht, unsigned int bufsize, void **buf)
{
unsigned int i;
unsigned int c = 0;
for (i = 0; i < ht->hash_table_size; i++)
{
HASH_ENT *p;
for (p = BUCKET_HEAD (ht, i); p; p = p->next)
{
if (c >= bufsize)
return;
buf[c++] = p->key;
}
}
}
/* Return the first key in the table. If the table is empty, return NULL. */
void *
hash_get_first (const HT *ht)
{
unsigned int idx;
HASH_ENT *p;
if (ht->hash_n_keys == 0)
return NULL;
for (idx = 0; idx < ht->hash_table_size; idx++)
{
if ((p = BUCKET_HEAD (ht, idx)) != NULL)
return p->key;
}
abort ();
}
/* Return the key in the entry following the entry whose key matches E.
If there is the only one key in the table and that key matches E,
return the matching key. If E is not in the table, return NULL. */
void *
hash_get_next (const HT *ht, const void *e)
{
unsigned int idx;
HASH_ENT *p;
idx = ht->hash_hash (e, ht->hash_table_size);
assert (idx < ht->hash_table_size);
for (p = BUCKET_HEAD (ht, idx); p != NULL; p = p->next)
{
if (ht->hash_key_comparator (e, p->key) == 0)
{
if (p->next != NULL)
{
return p->next->key;
}
else
{
unsigned int bucket;
/* E is the last or only key in the bucket chain. */
if (ht->hash_n_keys == 1)
{
/* There is only one key in the table, and it matches E. */
return p->key;
}
bucket = idx;
do
{
idx = (idx + 1) % ht->hash_table_size;
if ((p = BUCKET_HEAD (ht, idx)) != NULL)
return p->key;
}
while (idx != bucket);
}
}
}
/* E is not in the table. */
return NULL;
}