Changeset 609 for branches/GNU/src/binutils/libiberty/hashtab.c
- Timestamp:
- Aug 16, 2003, 6:59:22 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/binutils/libiberty/hashtab.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r608 r609 1 1 /* An expandable hash tables datatype. 2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. 3 3 Contributed by Vladimir Makarov (vmakarov@cygnus.com). 4 4 … … 81 81 /* These are primes that are near, but slightly smaller than, a 82 82 power of two. */ 83 static unsigned long primes[] = {84 2,85 7,86 13,87 31,88 61,89 127,90 251,91 509,92 1021,93 2039,94 4093,95 8191,96 16381,97 32749,98 65521,99 131071,100 262139,101 524287,102 1048573,103 2097143,104 4194301,105 8388593,106 16777213,107 33554393,108 67108859,109 134217689,110 268435399,111 536870909,112 1073741789,113 2147483647, 114 429496729183 static const unsigned long primes[] = { 84 (unsigned long) 7, 85 (unsigned long) 13, 86 (unsigned long) 31, 87 (unsigned long) 61, 88 (unsigned long) 127, 89 (unsigned long) 251, 90 (unsigned long) 509, 91 (unsigned long) 1021, 92 (unsigned long) 2039, 93 (unsigned long) 4093, 94 (unsigned long) 8191, 95 (unsigned long) 16381, 96 (unsigned long) 32749, 97 (unsigned long) 65521, 98 (unsigned long) 131071, 99 (unsigned long) 262139, 100 (unsigned long) 524287, 101 (unsigned long) 1048573, 102 (unsigned long) 2097143, 103 (unsigned long) 4194301, 104 (unsigned long) 8388593, 105 (unsigned long) 16777213, 106 (unsigned long) 33554393, 107 (unsigned long) 67108859, 108 (unsigned long) 134217689, 109 (unsigned long) 268435399, 110 (unsigned long) 536870909, 111 (unsigned long) 1073741789, 112 (unsigned long) 2147483647, 113 /* 4294967291L */ 114 ((unsigned long) 2147483647) + ((unsigned long) 2147483644), 115 115 }; 116 116 117 unsigned long*low = &primes[0];118 unsigned long*high = &primes[sizeof(primes) / sizeof(primes[0])];117 const unsigned long *low = &primes[0]; 118 const unsigned long *high = &primes[sizeof(primes) / sizeof(primes[0])]; 119 119 120 120 while (low != high) 121 121 { 122 unsigned long*mid = low + (high - low) / 2;122 const unsigned long *mid = low + (high - low) / 2; 123 123 if (n > *mid) 124 124 low = mid + 1; … … 159 159 source length. Created hash table is initiated as empty (all the 160 160 hash table entries are EMPTY_ENTRY). The function returns the 161 created hash table. Memory allocation must not fail. */ 162 161 created hash table, or NULL if memory allocation fails. */ 162 163 htab_t 164 htab_create_alloc (size, hash_f, eq_f, del_f, alloc_f, free_f) 165 size_t size; 166 htab_hash hash_f; 167 htab_eq eq_f; 168 htab_del del_f; 169 htab_alloc alloc_f; 170 htab_free free_f; 171 { 172 htab_t result; 173 174 size = higher_prime_number (size); 175 result = (htab_t) (*alloc_f) (1, sizeof (struct htab)); 176 if (result == NULL) 177 return NULL; 178 result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR)); 179 if (result->entries == NULL) 180 { 181 if (free_f != NULL) 182 (*free_f) (result); 183 return NULL; 184 } 185 result->size = size; 186 result->hash_f = hash_f; 187 result->eq_f = eq_f; 188 result->del_f = del_f; 189 result->alloc_f = alloc_f; 190 result->free_f = free_f; 191 return result; 192 } 193 194 /* As above, but use the variants of alloc_f and free_f which accept 195 an extra argument. */ 196 197 htab_t 198 htab_create_alloc_ex (size, hash_f, eq_f, del_f, alloc_arg, alloc_f, 199 free_f) 200 size_t size; 201 htab_hash hash_f; 202 htab_eq eq_f; 203 htab_del del_f; 204 PTR alloc_arg; 205 htab_alloc_with_arg alloc_f; 206 htab_free_with_arg free_f; 207 { 208 htab_t result; 209 210 size = higher_prime_number (size); 211 result = (htab_t) (*alloc_f) (alloc_arg, 1, sizeof (struct htab)); 212 if (result == NULL) 213 return NULL; 214 result->entries = (PTR *) (*alloc_f) (alloc_arg, size, sizeof (PTR)); 215 if (result->entries == NULL) 216 { 217 if (free_f != NULL) 218 (*free_f) (alloc_arg, result); 219 return NULL; 220 } 221 result->size = size; 222 result->hash_f = hash_f; 223 result->eq_f = eq_f; 224 result->del_f = del_f; 225 result->alloc_arg = alloc_arg; 226 result->alloc_with_arg_f = alloc_f; 227 result->free_with_arg_f = free_f; 228 return result; 229 } 230 231 /* Update the function pointers and allocation parameter in the htab_t. */ 232 233 void 234 htab_set_functions_ex (htab, hash_f, eq_f, del_f, alloc_arg, alloc_f, free_f) 235 htab_t htab; 236 htab_hash hash_f; 237 htab_eq eq_f; 238 htab_del del_f; 239 PTR alloc_arg; 240 htab_alloc_with_arg alloc_f; 241 htab_free_with_arg free_f; 242 { 243 htab->hash_f = hash_f; 244 htab->eq_f = eq_f; 245 htab->del_f = del_f; 246 htab->alloc_arg = alloc_arg; 247 htab->alloc_with_arg_f = alloc_f; 248 htab->free_with_arg_f = free_f; 249 } 250 251 /* These functions exist solely for backward compatibility. */ 252 253 #undef htab_create 163 254 htab_t 164 255 htab_create (size, hash_f, eq_f, del_f) … … 168 259 htab_del del_f; 169 260 { 170 htab_t result; 171 172 size = higher_prime_number (size); 173 result = (htab_t) xcalloc (1, sizeof (struct htab)); 174 result->entries = (PTR *) xcalloc (size, sizeof (PTR)); 175 result->size = size; 176 result->hash_f = hash_f; 177 result->eq_f = eq_f; 178 result->del_f = del_f; 179 result->return_allocation_failure = 0; 180 return result; 181 } 182 183 /* This function creates table with length slightly longer than given 184 source length. The created hash table is initiated as empty (all the 185 hash table entries are EMPTY_ENTRY). The function returns the created 186 hash table. Memory allocation may fail; it may return NULL. */ 261 return htab_create_alloc (size, hash_f, eq_f, del_f, xcalloc, free); 262 } 187 263 188 264 htab_t … … 193 269 htab_del del_f; 194 270 { 195 htab_t result; 196 197 size = higher_prime_number (size); 198 result = (htab_t) calloc (1, sizeof (struct htab)); 199 if (result == NULL) 200 return NULL; 201 202 result->entries = (PTR *) calloc (size, sizeof (PTR)); 203 if (result->entries == NULL) 204 { 205 free (result); 206 return NULL; 207 } 208 209 result->size = size; 210 result->hash_f = hash_f; 211 result->eq_f = eq_f; 212 result->del_f = del_f; 213 result->return_allocation_failure = 1; 214 return result; 271 return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free); 215 272 } 216 273 … … 230 287 (*htab->del_f) (htab->entries[i]); 231 288 232 free (htab->entries); 233 free (htab); 289 if (htab->free_f != NULL) 290 { 291 (*htab->free_f) (htab->entries); 292 (*htab->free_f) (htab); 293 } 294 else if (htab->free_with_arg_f != NULL) 295 { 296 (*htab->free_with_arg_f) (htab->alloc_arg, htab->entries); 297 (*htab->free_with_arg_f) (htab->alloc_arg, htab); 298 } 234 299 } 235 300 … … 264 329 { 265 330 size_t size = htab->size; 266 hashval_t hash2 = 1 + hash % (size - 2);267 331 unsigned int index = hash % size; 268 332 PTR *slot = htab->entries + index; 333 hashval_t hash2; 334 335 if (*slot == EMPTY_ENTRY) 336 return slot; 337 else if (*slot == DELETED_ENTRY) 338 abort (); 339 340 hash2 = 1 + hash % (size - 2); 269 341 for (;;) 270 342 { 271 PTR *slot = htab->entries + index; 272 343 index += hash2; 344 if (index >= size) 345 index -= size; 346 347 slot = htab->entries + index; 273 348 if (*slot == EMPTY_ENTRY) 274 349 return slot; 275 350 else if (*slot == DELETED_ENTRY) 276 351 abort (); 277 278 index += hash2;279 if (index >= size)280 index -= size;281 352 } 282 353 } … … 297 368 PTR *olimit; 298 369 PTR *p; 370 PTR *nentries; 371 size_t nsize; 299 372 300 373 oentries = htab->entries; 301 374 olimit = oentries + htab->size; 302 375 303 htab->size = higher_prime_number (htab->size * 2); 304 305 if (htab->return_allocation_failure) 306 { 307 PTR *nentries = (PTR *) calloc (htab->size, sizeof (PTR *)); 308 if (nentries == NULL) 309 return 0; 310 htab->entries = nentries; 311 } 376 /* Resize only when table after removal of unused elements is either 377 too full or too empty. */ 378 if ((htab->n_elements - htab->n_deleted) * 2 > htab->size 379 || ((htab->n_elements - htab->n_deleted) * 8 < htab->size 380 && htab->size > 32)) 381 nsize = higher_prime_number ((htab->n_elements - htab->n_deleted) * 2); 312 382 else 313 htab->entries = (PTR *) xcalloc (htab->size, sizeof (PTR *)); 383 nsize = htab->size; 384 385 if (htab->alloc_with_arg_f != NULL) 386 nentries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize, 387 sizeof (PTR *)); 388 else 389 nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *)); 390 if (nentries == NULL) 391 return 0; 392 htab->entries = nentries; 393 htab->size = nsize; 314 394 315 395 htab->n_elements -= htab->n_deleted; … … 332 412 while (p < olimit); 333 413 334 free (oentries); 414 if (htab->free_f != NULL) 415 (*htab->free_f) (oentries); 416 else if (htab->free_with_arg_f != NULL) 417 (*htab->free_with_arg_f) (htab->alloc_arg, oentries); 335 418 return 1; 336 419 } … … 405 488 hashval_t hash2; 406 489 size_t size; 490 PTR entry; 407 491 408 492 if (insert == INSERT && htab->size * 3 <= htab->n_elements * 4 … … 411 495 412 496 size = htab->size; 413 hash2 = 1 + hash % (size - 2);414 497 index = hash % size; 415 498 … … 417 500 first_deleted_slot = NULL; 418 501 502 entry = htab->entries[index]; 503 if (entry == EMPTY_ENTRY) 504 goto empty_entry; 505 else if (entry == DELETED_ENTRY) 506 first_deleted_slot = &htab->entries[index]; 507 else if ((*htab->eq_f) (entry, element)) 508 return &htab->entries[index]; 509 510 hash2 = 1 + hash % (size - 2); 419 511 for (;;) 420 512 { 421 PTR entry = htab->entries[index]; 513 htab->collisions++; 514 index += hash2; 515 if (index >= size) 516 index -= size; 517 518 entry = htab->entries[index]; 422 519 if (entry == EMPTY_ENTRY) 423 { 424 if (insert == NO_INSERT) 425 return NULL; 426 427 htab->n_elements++; 428 429 if (first_deleted_slot) 430 { 431 *first_deleted_slot = EMPTY_ENTRY; 432 return first_deleted_slot; 433 } 434 435 return &htab->entries[index]; 436 } 437 438 if (entry == DELETED_ENTRY) 520 goto empty_entry; 521 else if (entry == DELETED_ENTRY) 439 522 { 440 523 if (!first_deleted_slot) 441 524 first_deleted_slot = &htab->entries[index]; 442 525 } 443 else 526 else if ((*htab->eq_f) (entry, element)) 444 527 return &htab->entries[index]; 445 446 htab->collisions++; 447 index += hash2; 448 if (index >= size) 449 index -= size; 450 } 528 } 529 530 empty_entry: 531 if (insert == NO_INSERT) 532 return NULL; 533 534 htab->n_elements++; 535 536 if (first_deleted_slot) 537 { 538 *first_deleted_slot = EMPTY_ENTRY; 539 return first_deleted_slot; 540 } 541 542 return &htab->entries[index]; 451 543 } 452 544 … … 512 604 513 605 void 514 htab_traverse (htab, callback, info)606 htab_traverse_noresize (htab, callback, info) 515 607 htab_t htab; 516 608 htab_trav callback; 517 609 PTR info; 518 610 { 519 PTR *slot = htab->entries; 520 PTR *limit = slot + htab->size; 611 PTR *slot; 612 PTR *limit; 613 614 slot = htab->entries; 615 limit = slot + htab->size; 521 616 522 617 do … … 531 626 } 532 627 628 /* Like htab_traverse_noresize, but does resize the table when it is 629 too empty to improve effectivity of subsequent calls. */ 630 631 void 632 htab_traverse (htab, callback, info) 633 htab_t htab; 634 htab_trav callback; 635 PTR info; 636 { 637 if ((htab->n_elements - htab->n_deleted) * 8 < htab->size) 638 htab_expand (htab); 639 640 htab_traverse_noresize (htab, callback, info); 641 } 642 533 643 /* Return the current size of given hash table. */ 534 644 … … 561 671 return (double) htab->collisions / (double) htab->searches; 562 672 } 673 674 /* Hash P as a null-terminated string. 675 676 Copied from gcc/hashtable.c. Zack had the following to say with respect 677 to applicability, though note that unlike hashtable.c, this hash table 678 implementation re-hashes rather than chain buckets. 679 680 http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01021.html 681 From: Zack Weinberg <zackw@panix.com> 682 Date: Fri, 17 Aug 2001 02:15:56 -0400 683 684 I got it by extracting all the identifiers from all the source code 685 I had lying around in mid-1999, and testing many recurrences of 686 the form "H_n = H_{n-1} * K + c_n * L + M" where K, L, M were either 687 prime numbers or the appropriate identity. This was the best one. 688 I don't remember exactly what constituted "best", except I was 689 looking at bucket-length distributions mostly. 690 691 So it should be very good at hashing identifiers, but might not be 692 as good at arbitrary strings. 693 694 I'll add that it thoroughly trounces the hash functions recommended 695 for this use at http://burtleburtle.net/bob/hash/index.html, both 696 on speed and bucket distribution. I haven't tried it against the 697 function they just started using for Perl's hashes. */ 698 699 hashval_t 700 htab_hash_string (p) 701 const PTR p; 702 { 703 const unsigned char *str = (const unsigned char *) p; 704 hashval_t r = 0; 705 unsigned char c; 706 707 while ((c = *str++) != 0) 708 r = r * 67 + c - 113; 709 710 return r; 711 } -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.