| 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 |
#include <bltInt.h> |
| 44 |
|
| 45 |
#include <stdio.h> |
| 46 |
#include <string.h> |
| 47 |
|
| 48 |
#include <stdlib.h> |
| 49 |
|
| 50 |
#include "bltHash.h" |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#define REBUILD_MULTIPLIER 3 |
| 58 |
|
| 59 |
#if (SIZEOF_VOID_P == 8) |
| 60 |
#define RANDOM_INDEX HashOneWord |
| 61 |
#define DOWNSHIFT_START 62 |
| 62 |
#else |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#define RANDOM_INDEX(tablePtr, i) \ |
| 72 |
(((((long) (i))*1103515245) >> (tablePtr)->downShift) & (tablePtr)->mask) |
| 73 |
#define DOWNSHIFT_START 28 |
| 74 |
#endif |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static Blt_Hash HashArray _ANSI_ARGS_((CONST void *key, size_t length)); |
| 80 |
static Blt_HashEntry *ArrayFind _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 81 |
CONST void *key)); |
| 82 |
static Blt_HashEntry *ArrayCreate _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 83 |
CONST void *key, int *newPtr)); |
| 84 |
static Blt_HashEntry *BogusFind _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 85 |
CONST void *key)); |
| 86 |
static Blt_HashEntry *BogusCreate _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 87 |
CONST void *key, int *newPtr)); |
| 88 |
static Blt_Hash HashString _ANSI_ARGS_((CONST char *string)); |
| 89 |
static void RebuildTable _ANSI_ARGS_((Blt_HashTable *tablePtr)); |
| 90 |
static Blt_HashEntry *StringFind _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 91 |
CONST void *key)); |
| 92 |
static Blt_HashEntry *StringCreate _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 93 |
CONST void *key, int *newPtr)); |
| 94 |
static Blt_HashEntry *OneWordFind _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 95 |
CONST void *key)); |
| 96 |
static Blt_HashEntry *OneWordCreate _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 97 |
CONST void *key, int *newPtr)); |
| 98 |
|
| 99 |
#if (SIZEOF_VOID_P == 8) |
| 100 |
static Blt_Hash HashOneWord _ANSI_ARGS_((Blt_HashTable *tablePtr, |
| 101 |
CONST void *key)); |
| 102 |
|
| 103 |
#endif |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
static Blt_Hash |
| 123 |
HashString(register CONST char *string) |
| 124 |
|
| 125 |
{ |
| 126 |
register Blt_Hash result; |
| 127 |
register Blt_Hash c; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
result = 0; |
| 146 |
while ((c = *string++) != 0) { |
| 147 |
result += (result << 3) + c; |
| 148 |
} |
| 149 |
return (Blt_Hash)result; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static Blt_HashEntry * |
| 170 |
StringFind( |
| 171 |
Blt_HashTable *tablePtr, |
| 172 |
CONST void *key) |
| 173 |
{ |
| 174 |
Blt_Hash hval; |
| 175 |
register Blt_HashEntry *hPtr; |
| 176 |
size_t hindex; |
| 177 |
|
| 178 |
hval = HashString((char *)key); |
| 179 |
hindex = hval & tablePtr->mask; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
for (hPtr = tablePtr->buckets[hindex]; hPtr != NULL; |
| 186 |
hPtr = hPtr->nextPtr) { |
| 187 |
if (hPtr->hval == hval) { |
| 188 |
register CONST char *p1, *p2; |
| 189 |
|
| 190 |
for (p1 = key, p2 = hPtr->key.string; ; p1++, p2++) { |
| 191 |
if (*p1 != *p2) { |
| 192 |
break; |
| 193 |
} |
| 194 |
if (*p1 == '\0') { |
| 195 |
return hPtr; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static Blt_HashEntry * |
| 224 |
StringCreate( |
| 225 |
Blt_HashTable *tablePtr, |
| 226 |
CONST void *key, |
| 227 |
|
| 228 |
int *newPtr) |
| 229 |
|
| 230 |
{ |
| 231 |
Blt_Hash hval; |
| 232 |
Blt_HashEntry **bucketPtr; |
| 233 |
register Blt_HashEntry *hPtr; |
| 234 |
size_t size, hindex; |
| 235 |
|
| 236 |
hval = HashString(key); |
| 237 |
hindex = hval & tablePtr->mask; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
for (hPtr = tablePtr->buckets[hindex]; hPtr != NULL; |
| 244 |
hPtr = hPtr->nextPtr) { |
| 245 |
if (hPtr->hval == hval) { |
| 246 |
register CONST char *p1, *p2; |
| 247 |
|
| 248 |
for (p1 = key, p2 = hPtr->key.string; ; p1++, p2++) { |
| 249 |
if (*p1 != *p2) { |
| 250 |
break; |
| 251 |
} |
| 252 |
if (*p1 == '\0') { |
| 253 |
*newPtr = FALSE; |
| 254 |
return hPtr; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
*newPtr = TRUE; |
| 265 |
size = sizeof(Blt_HashEntry) + strlen(key) - sizeof(Blt_HashKey) + 1; |
| 266 |
if (tablePtr->hPool != NULL) { |
| 267 |
hPtr = Blt_PoolAllocItem(tablePtr->hPool, size); |
| 268 |
} else { |
| 269 |
hPtr = Blt_Malloc(size); |
| 270 |
} |
| 271 |
bucketPtr = tablePtr->buckets + hindex; |
| 272 |
hPtr->nextPtr = *bucketPtr; |
| 273 |
hPtr->hval = hval; |
| 274 |
hPtr->clientData = 0; |
| 275 |
strcpy(hPtr->key.string, key); |
| 276 |
*bucketPtr = hPtr; |
| 277 |
tablePtr->numEntries++; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
if (tablePtr->numEntries >= tablePtr->rebuildSize) { |
| 285 |
RebuildTable(tablePtr); |
| 286 |
} |
| 287 |
return hPtr; |
| 288 |
} |
| 289 |
|
| 290 |
#if (SIZEOF_VOID_P == 8) |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
static Blt_Hash |
| 314 |
HashOneWord( |
| 315 |
Blt_HashTable *tablePtr, |
| 316 |
CONST void *key) |
| 317 |
{ |
| 318 |
uint64_t a0, a1; |
| 319 |
uint64_t y0, y1; |
| 320 |
uint64_t y2, y3; |
| 321 |
uint64_t p1, p2; |
| 322 |
uint64_t result; |
| 323 |
|
| 324 |
a0 = (uint64_t)key & 0x00000000FFFFFFFF; |
| 325 |
a1 = (uint64_t)key >> 32; |
| 326 |
|
| 327 |
y0 = a0 * 0x000000007f4a7c13; |
| 328 |
y1 = a0 * 0x000000009e3779b9; |
| 329 |
y2 = a1 * 0x000000007f4a7c13; |
| 330 |
y3 = a1 * 0x000000009e3779b9; |
| 331 |
y1 += y0 >> 32; |
| 332 |
y1 += y2; |
| 333 |
if (y1 < y2) { |
| 334 |
y3 += (1LL << 32); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
p1 = ((y1 & 0x00000000FFFFFFFF) << 32) + (y0 & 0x00000000FFFFFFFF); |
| 339 |
p2 = y3 + (y1 >> 32); |
| 340 |
|
| 341 |
|
| 342 |
if (tablePtr->downShift > 0) { |
| 343 |
if (tablePtr->downShift < 64) { |
| 344 |
result = ((p2 << (64 - tablePtr->downShift)) | |
| 345 |
(p1 >> (tablePtr->downShift & 63))); |
| 346 |
} else { |
| 347 |
result = p2 >> (tablePtr->downShift & 63); |
| 348 |
} |
| 349 |
} else { |
| 350 |
result = p1; |
| 351 |
} |
| 352 |
|
| 353 |
return (Blt_Hash)(result & tablePtr->mask); |
| 354 |
} |
| 355 |
|
| 356 |
#endif |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
static Blt_HashEntry * |
| 376 |
OneWordFind( |
| 377 |
Blt_HashTable *tablePtr, |
| 378 |
register CONST void *key) |
| 379 |
{ |
| 380 |
register Blt_HashEntry *hPtr; |
| 381 |
size_t hindex; |
| 382 |
|
| 383 |
hindex = RANDOM_INDEX(tablePtr, key); |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
for (hPtr = tablePtr->buckets[hindex]; hPtr != NULL; |
| 389 |
hPtr = hPtr->nextPtr) { |
| 390 |
if (hPtr->key.oneWordValue == key) { |
| 391 |
return hPtr; |
| 392 |
} |
| 393 |
} |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static Blt_HashEntry * |
| 418 |
OneWordCreate( |
| 419 |
Blt_HashTable *tablePtr, |
| 420 |
CONST void *key, |
| 421 |
|
| 422 |
int *newPtr) |
| 423 |
|
| 424 |
{ |
| 425 |
Blt_HashEntry **bucketPtr; |
| 426 |
register Blt_HashEntry *hPtr; |
| 427 |
size_t hindex; |
| 428 |
|
| 429 |
hindex = RANDOM_INDEX(tablePtr, key); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
for (hPtr = tablePtr->buckets[hindex]; hPtr != NULL; |
| 435 |
hPtr = hPtr->nextPtr) { |
| 436 |
if (hPtr->key.oneWordValue == key) { |
| 437 |
*newPtr = FALSE; |
| 438 |
return hPtr; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
*newPtr = TRUE; |
| 447 |
if (tablePtr->hPool != NULL) { |
| 448 |
hPtr = Blt_PoolAllocItem(tablePtr->hPool, sizeof(Blt_HashEntry)); |
| 449 |
} else { |
| 450 |
hPtr = Blt_Malloc(sizeof(Blt_HashEntry)); |
| 451 |
} |
| 452 |
bucketPtr = tablePtr->buckets + hindex; |
| 453 |
hPtr->nextPtr = *bucketPtr; |
| 454 |
hPtr->hval = (Blt_Hash)key; |
| 455 |
hPtr->clientData = 0; |
| 456 |
hPtr->key.oneWordValue = (void *)key; |
| 457 |
*bucketPtr = hPtr; |
| 458 |
tablePtr->numEntries++; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
if (tablePtr->numEntries >= tablePtr->rebuildSize) { |
| 466 |
RebuildTable(tablePtr); |
| 467 |
} |
| 468 |
return hPtr; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
#if (SIZEOF_VOID_P == 4) |
| 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 |
#define MIX32(a,b,c) \ |
| 509 |
a -= b, a -= c, a ^= (c >> 13), \ |
| 510 |
b -= c, b -= a, b ^= (a << 8), \ |
| 511 |
c -= a, c -= b, c ^= (b >> 13), \ |
| 512 |
a -= b, a -= c, a ^= (c >> 12), \ |
| 513 |
b -= c, b -= a, b ^= (a << 16), \ |
| 514 |
c -= a, c -= b, c ^= (b >> 5), \ |
| 515 |
a -= b, a -= c, a ^= (c >> 3), \ |
| 516 |
b -= c, b -= a, b ^= (a << 10), \ |
| 517 |
c -= a, c -= b, c ^= (b >> 15) |
| 518 |
|
| 519 |
#define GOLDEN_RATIO32 0x9e3779b9 |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
static Blt_Hash |
| 538 |
HashArray( |
| 539 |
CONST void *key, |
| 540 |
size_t length) |
| 541 |
{ |
| 542 |
register uint32_t a, b, c, len; |
| 543 |
register uint32_t *arrayPtr = (uint32_t *)key; |
| 544 |
|
| 545 |
len = length; |
| 546 |
a = b = GOLDEN_RATIO32; |
| 547 |
c = 0; |
| 548 |
|
| 549 |
while (len >= 3) { |
| 550 |
a += arrayPtr[0]; |
| 551 |
b += arrayPtr[1]; |
| 552 |
c += arrayPtr[2]; |
| 553 |
MIX32(a, b, c); |
| 554 |
arrayPtr += 3; len -= 3; |
| 555 |
} |
| 556 |
c += length; |
| 557 |
|
| 558 |
|
| 559 |
switch(len) { |
| 560 |
|
| 561 |
case 2 : b += arrayPtr[1]; |
| 562 |
case 1 : a += arrayPtr[0]; |
| 563 |
|
| 564 |
} |
| 565 |
MIX32(a, b, c); |
| 566 |
return (Blt_Hash)c; |
| 567 |
} |
| 568 |
#endif |
| 569 |
|
| 570 |
#if (SIZEOF_VOID_P == 8) |
| 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 |
#define MIX64(a,b,c) \ |
| 602 |
a -= b, a -= c, a ^= (c >> 43), \ |
| 603 |
b -= c, b -= a, b ^= (a << 9), \ |
| 604 |
c -= a, c -= b, c ^= (b >> 8), \ |
| 605 |
a -= b, a -= c, a ^= (c >> 38), \ |
| 606 |
b -= c, b -= a, b ^= (a << 23), \ |
| 607 |
c -= a, c -= b, c ^= (b >> 5), \ |
| 608 |
a -= b, a -= c, a ^= (c >> 35), \ |
| 609 |
b -= c, b -= a, b ^= (a << 49), \ |
| 610 |
c -= a, c -= b, c ^= (b >> 11), \ |
| 611 |
a -= b, a -= c, a ^= (c >> 12), \ |
| 612 |
b -= c, b -= a, b ^= (a << 18), \ |
| 613 |
c -= a, c -= b, c ^= (b >> 22) |
| 614 |
|
| 615 |
#define GOLDEN_RATIO64 0x9e3779b97f4a7c13LL |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
static Blt_Hash |
| 635 |
HashArray( |
| 636 |
CONST void *key, |
| 637 |
size_t length) |
| 638 |
{ |
| 639 |
register uint64_t a, b, c, len; |
| 640 |
register uint32_t *iPtr = (uint32_t *)key; |
| 641 |
|
| 642 |
#ifdef WORDS_BIGENDIAN |
| 643 |
#define PACK(a,b) ((uint64_t)(b) | ((uint64_t)(a) << 32)) |
| 644 |
#else |
| 645 |
#define PACK(a,b) ((uint64_t)(a) | ((uint64_t)(b) << 32)) |
| 646 |
#endif |
| 647 |
|
| 648 |
len = length; |
| 649 |
a = b = GOLDEN_RATIO64; |
| 650 |
c = 0; |
| 651 |
|
| 652 |
while (len >= 6) { |
| 653 |
a += PACK(iPtr[0], iPtr[1]); |
| 654 |
b += PACK(iPtr[2], iPtr[3]); |
| 655 |
c += PACK(iPtr[4], iPtr[5]); |
| 656 |
MIX64(a,b,c); |
| 657 |
iPtr += 6; len -= 6; |
| 658 |
} |
| 659 |
c += length; |
| 660 |
|
| 661 |
|
| 662 |
switch(len) { |
| 663 |
|
| 664 |
case 5 : |
| 665 |
case 4 : |
| 666 |
a += PACK(iPtr[0], iPtr[1]); |
| 667 |
b += PACK(iPtr[2], iPtr[3]); |
| 668 |
iPtr += 4; len -= 4; |
| 669 |
break; |
| 670 |
case 3 : |
| 671 |
case 2 : |
| 672 |
a += PACK(iPtr[0], iPtr[1]); |
| 673 |
iPtr += 2; len -= 2; |
| 674 |
|
| 675 |
} |
| 676 |
if (len > 0) { |
| 677 |
b += iPtr[0]; |
| 678 |
} |
| 679 |
MIX64(a,b,c); |
| 680 |
return (Blt_Hash)c; |
| 681 |
} |
| 682 |
#endif |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
static Blt_HashEntry * |
| 702 |
ArrayFind( |
| 703 |
Blt_HashTable *tablePtr, |
| 704 |
CONST void *key) |
| 705 |
{ |
| 706 |
Blt_Hash hval; |
| 707 |
register Blt_HashEntry *hPtr; |
| 708 |
size_t hindex; |
| 709 |
|
| 710 |
hval = HashArray(key, tablePtr->keyType); |
| 711 |
hindex = hval & tablePtr->mask; |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
for (hPtr = tablePtr->buckets[hindex]; hPtr != NULL; |
| 717 |
hPtr = hPtr->nextPtr) { |
| 718 |
if (hPtr->hval == hval) { |
| 719 |
register unsigned int *iPtr1, *iPtr2; |
| 720 |
unsigned int count; |
| 721 |
|
| 722 |
for (iPtr1 = (uint32_t *)key, iPtr2 = (uint32_t *)hPtr->key.words, |
| 723 |
count = tablePtr->keyType; ; count--, iPtr1++, iPtr2++) { |
| 724 |
if (count == 0) { |
| 725 |
return hPtr; |
| 726 |
} |
| 727 |
if (*iPtr1 != *iPtr2) { |
| 728 |
break; |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
return NULL; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
static Blt_HashEntry * |
| 757 |
ArrayCreate( |
| 758 |
Blt_HashTable *tablePtr, |
| 759 |
register CONST void *key, |
| 760 |
|
| 761 |
int *newPtr) |
| 762 |
|
| 763 |
{ |
| 764 |
Blt_Hash hval; |
| 765 |
Blt_HashEntry **bucketPtr; |
| 766 |
int count; |
| 767 |
register Blt_HashEntry *hPtr; |
| 768 |
register uint32_t *iPtr1, *iPtr2; |
| 769 |
size_t size, hindex; |
| 770 |
|
| 771 |
hval = HashArray(key, tablePtr->keyType); |
| 772 |
hindex = hval & tablePtr->mask; |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
for (hPtr = tablePtr->buckets[hindex]; hPtr != NULL; |
| 778 |
hPtr = hPtr->nextPtr) { |
| 779 |
if (hPtr->hval == hval) { |
| 780 |
for (iPtr1 = (uint32_t *)key, iPtr2 = (uint32_t *)hPtr->key.words, |
| 781 |
count = tablePtr->keyType; ; count--, iPtr1++, iPtr2++) { |
| 782 |
if (count == 0) { |
| 783 |
*newPtr = FALSE; |
| 784 |
return hPtr; |
| 785 |
} |
| 786 |
if (*iPtr1 != *iPtr2) { |
| 787 |
break; |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
*newPtr = TRUE; |
| 797 |
|
| 798 |
size = sizeof(Blt_HashEntry) + tablePtr->keyType * sizeof(uint32_t) - |
| 799 |
sizeof(Blt_HashKey); |
| 800 |
if (tablePtr->hPool != NULL) { |
| 801 |
hPtr = Blt_PoolAllocItem(tablePtr->hPool, size); |
| 802 |
} else { |
| 803 |
hPtr = Blt_Malloc(size); |
| 804 |
} |
| 805 |
bucketPtr = tablePtr->buckets + hindex; |
| 806 |
hPtr->nextPtr = *bucketPtr; |
| 807 |
hPtr->hval = hval; |
| 808 |
hPtr->clientData = 0; |
| 809 |
count = tablePtr->keyType; |
| 810 |
for (iPtr1 = (uint32_t *)key, iPtr2 = (uint32_t *)hPtr->key.words; |
| 811 |
count > 0; count--, iPtr1++, iPtr2++) { |
| 812 |
*iPtr2 = *iPtr1; |
| 813 |
} |
| 814 |
*bucketPtr = hPtr; |
| 815 |
tablePtr->numEntries++; |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
if (tablePtr->numEntries >= tablePtr->rebuildSize) { |
| 822 |
RebuildTable(tablePtr); |
| 823 |
} |
| 824 |
return hPtr; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
static Blt_HashEntry * |
| 846 |
BogusFind( |
| 847 |
Blt_HashTable *tablePtr, |
| 848 |
CONST void *key) |
| 849 |
{ |
| 850 |
Blt_Panic("called Blt_FindHashEntry on deleted table"); |
| 851 |
return NULL; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
static Blt_HashEntry * |
| 873 |
BogusCreate( |
| 874 |
Blt_HashTable *tablePtr, |
| 875 |
CONST void *key, |
| 876 |
|
| 877 |
int *newPtr) |
| 878 |
|
| 879 |
{ |
| 880 |
Blt_Panic("called Blt_CreateHashEntry on deleted table"); |
| 881 |
return NULL; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
static void |
| 904 |
RebuildTable(Blt_HashTable *tablePtr) |
| 905 |
{ |
| 906 |
Blt_HashEntry **bucketPtr, **oldBuckets; |
| 907 |
register Blt_HashEntry **oldChainPtr, **endPtr; |
| 908 |
register Blt_HashEntry *hPtr, *nextPtr; |
| 909 |
size_t hindex; |
| 910 |
|
| 911 |
oldBuckets = tablePtr->buckets; |
| 912 |
endPtr = tablePtr->buckets + tablePtr->numBuckets; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
tablePtr->numBuckets <<= 2; |
| 918 |
tablePtr->buckets = Blt_Calloc(tablePtr->numBuckets, |
| 919 |
sizeof(Blt_HashEntry *)); |
| 920 |
tablePtr->rebuildSize <<= 2; |
| 921 |
tablePtr->downShift -= 2; |
| 922 |
tablePtr->mask = tablePtr->numBuckets - 1; |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
if (tablePtr->keyType == BLT_ONE_WORD_KEYS) { |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
for (oldChainPtr = oldBuckets; oldChainPtr < endPtr; oldChainPtr++) { |
| 935 |
for (hPtr = *oldChainPtr; hPtr != NULL; hPtr = nextPtr) { |
| 936 |
nextPtr = hPtr->nextPtr; |
| 937 |
hindex = RANDOM_INDEX(tablePtr, hPtr->key.oneWordValue); |
| 938 |
bucketPtr = tablePtr->buckets + hindex; |
| 939 |
hPtr->nextPtr = *bucketPtr; |
| 940 |
*bucketPtr = hPtr; |
| 941 |
} |
| 942 |
} |
| 943 |
} else { |
| 944 |
for (oldChainPtr = oldBuckets; oldChainPtr < endPtr; oldChainPtr++) { |
| 945 |
for (hPtr = *oldChainPtr; hPtr != NULL; hPtr = nextPtr) { |
| 946 |
nextPtr = hPtr->nextPtr; |
| 947 |
hindex = hPtr->hval & tablePtr->mask; |
| 948 |
bucketPtr = tablePtr->buckets + hindex; |
| 949 |
hPtr->nextPtr = *bucketPtr; |
| 950 |
*bucketPtr = hPtr; |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
if (oldBuckets != tablePtr->staticBuckets) { |
| 959 |
Blt_Free(oldBuckets); |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
void |
| 984 |
Blt_InitHashTable( |
| 985 |
register Blt_HashTable *tablePtr, |
| 986 |
|
| 987 |
size_t keyType) |
| 988 |
{ |
| 989 |
#if (BLT_SMALL_HASH_TABLE != 4) |
| 990 |
Blt_Panic("Blt_InitHashTable: BLT_SMALL_HASH_TABLE is %d, not 4\n", |
| 991 |
BLT_SMALL_HASH_TABLE); |
| 992 |
#endif |
| 993 |
tablePtr->buckets = tablePtr->staticBuckets; |
| 994 |
tablePtr->numBuckets = BLT_SMALL_HASH_TABLE; |
| 995 |
tablePtr->staticBuckets[0] = tablePtr->staticBuckets[1] = 0; |
| 996 |
tablePtr->staticBuckets[2] = tablePtr->staticBuckets[3] = 0; |
| 997 |
tablePtr->numEntries = 0; |
| 998 |
tablePtr->rebuildSize = BLT_SMALL_HASH_TABLE * REBUILD_MULTIPLIER; |
| 999 |
tablePtr->downShift = DOWNSHIFT_START; |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
tablePtr->mask = (Blt_Hash)(tablePtr->numBuckets - 1); |
| 1005 |
tablePtr->keyType = keyType; |
| 1006 |
|
| 1007 |
switch (keyType) { |
| 1008 |
case BLT_STRING_KEYS: |
| 1009 |
tablePtr->findProc = StringFind; |
| 1010 |
tablePtr->createProc = StringCreate; |
| 1011 |
break; |
| 1012 |
|
| 1013 |
case BLT_ONE_WORD_KEYS: |
| 1014 |
tablePtr->findProc = OneWordFind; |
| 1015 |
tablePtr->createProc = OneWordCreate; |
| 1016 |
break; |
| 1017 |
|
| 1018 |
default: |
| 1019 |
if (keyType == 0) { |
| 1020 |
Blt_Panic("Blt_InitHashTable: Key size can't be %d, must be > 0\n", |
| 1021 |
keyType); |
| 1022 |
} |
| 1023 |
tablePtr->findProc = ArrayFind; |
| 1024 |
tablePtr->createProc = ArrayCreate; |
| 1025 |
break; |
| 1026 |
} |
| 1027 |
tablePtr->hPool = NULL; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
void |
| 1051 |
Blt_InitHashTableWithPool( |
| 1052 |
register Blt_HashTable *tablePtr, |
| 1053 |
|
| 1054 |
size_t keyType) |
| 1055 |
{ |
| 1056 |
Blt_InitHashTable(tablePtr, keyType); |
| 1057 |
if (keyType == BLT_STRING_KEYS) { |
| 1058 |
tablePtr->hPool = Blt_PoolCreate(BLT_VARIABLE_SIZE_ITEMS); |
| 1059 |
} else { |
| 1060 |
tablePtr->hPool = Blt_PoolCreate(BLT_FIXED_SIZE_ITEMS); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
void |
| 1083 |
Blt_DeleteHashEntry( |
| 1084 |
Blt_HashTable *tablePtr, |
| 1085 |
Blt_HashEntry *entryPtr) |
| 1086 |
{ |
| 1087 |
register Blt_HashEntry *prevPtr; |
| 1088 |
Blt_HashEntry **bucketPtr; |
| 1089 |
size_t hindex; |
| 1090 |
|
| 1091 |
if (tablePtr->keyType == BLT_ONE_WORD_KEYS) { |
| 1092 |
hindex = RANDOM_INDEX(tablePtr, (CONST void *)entryPtr->hval); |
| 1093 |
} else { |
| 1094 |
hindex = (entryPtr->hval & tablePtr->mask); |
| 1095 |
} |
| 1096 |
bucketPtr = tablePtr->buckets + hindex; |
| 1097 |
if (*bucketPtr == entryPtr) { |
| 1098 |
*bucketPtr = entryPtr->nextPtr; |
| 1099 |
} else { |
| 1100 |
for (prevPtr = *bucketPtr; ; prevPtr = prevPtr->nextPtr) { |
| 1101 |
if (prevPtr == NULL) { |
| 1102 |
Blt_Panic("malformed bucket chain in Blt_DeleteHashEntry"); |
| 1103 |
} |
| 1104 |
if (prevPtr->nextPtr == entryPtr) { |
| 1105 |
prevPtr->nextPtr = entryPtr->nextPtr; |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
tablePtr->numEntries--; |
| 1111 |
if (tablePtr->hPool != NULL) { |
| 1112 |
Blt_PoolFreeItem(tablePtr->hPool, (char *)entryPtr); |
| 1113 |
} else { |
| 1114 |
Blt_Free(entryPtr); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
void |
| 1135 |
Blt_DeleteHashTable(Blt_HashTable *tablePtr) |
| 1136 |
{ |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
if (tablePtr->hPool != NULL) { |
| 1141 |
Blt_PoolDestroy(tablePtr->hPool); |
| 1142 |
tablePtr->hPool = NULL; |
| 1143 |
} else { |
| 1144 |
register Blt_HashEntry *hPtr, *nextPtr; |
| 1145 |
size_t i; |
| 1146 |
|
| 1147 |
for (i = 0; i < tablePtr->numBuckets; i++) { |
| 1148 |
hPtr = tablePtr->buckets[i]; |
| 1149 |
while (hPtr != NULL) { |
| 1150 |
nextPtr = hPtr->nextPtr; |
| 1151 |
Blt_Free(hPtr); |
| 1152 |
hPtr = nextPtr; |
| 1153 |
} |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
if (tablePtr->buckets != tablePtr->staticBuckets) { |
| 1161 |
Blt_Free(tablePtr->buckets); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
tablePtr->findProc = BogusFind; |
| 1170 |
tablePtr->createProc = BogusCreate; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
Blt_HashEntry * |
| 1195 |
Blt_FirstHashEntry( |
| 1196 |
Blt_HashTable *tablePtr, |
| 1197 |
Blt_HashSearch *searchPtr) |
| 1198 |
|
| 1199 |
{ |
| 1200 |
searchPtr->tablePtr = tablePtr; |
| 1201 |
searchPtr->nextIndex = 0; |
| 1202 |
searchPtr->nextEntryPtr = NULL; |
| 1203 |
return Blt_NextHashEntry(searchPtr); |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
Blt_HashEntry * |
| 1225 |
Blt_NextHashEntry(Blt_HashSearch *searchPtr) |
| 1226 |
{ |
| 1227 |
Blt_HashEntry *hPtr; |
| 1228 |
|
| 1229 |
while (searchPtr->nextEntryPtr == NULL) { |
| 1230 |
if (searchPtr->nextIndex >= searchPtr->tablePtr->numBuckets) { |
| 1231 |
return NULL; |
| 1232 |
} |
| 1233 |
searchPtr->nextEntryPtr = |
| 1234 |
searchPtr->tablePtr->buckets[searchPtr->nextIndex]; |
| 1235 |
searchPtr->nextIndex++; |
| 1236 |
} |
| 1237 |
hPtr = searchPtr->nextEntryPtr; |
| 1238 |
searchPtr->nextEntryPtr = hPtr->nextPtr; |
| 1239 |
return hPtr; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
char * |
| 1261 |
Blt_HashStats(Blt_HashTable *tablePtr) |
| 1262 |
{ |
| 1263 |
#define NUM_COUNTERS 10 |
| 1264 |
size_t count[NUM_COUNTERS], overflow, i, j, max; |
| 1265 |
double average, tmp; |
| 1266 |
register Blt_HashEntry *hPtr; |
| 1267 |
Blt_HashEntry **bucketPtr, **endPtr; |
| 1268 |
char *result, *p; |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
for (i = 0; i < NUM_COUNTERS; i++) { |
| 1274 |
count[i] = 0; |
| 1275 |
} |
| 1276 |
overflow = 0; |
| 1277 |
average = 0.0; |
| 1278 |
max = 0; |
| 1279 |
endPtr = tablePtr->buckets + tablePtr->numBuckets; |
| 1280 |
for (bucketPtr = tablePtr->buckets; bucketPtr < endPtr; bucketPtr++) { |
| 1281 |
j = 0; |
| 1282 |
for (hPtr = *bucketPtr; hPtr != NULL; hPtr = hPtr->nextPtr) { |
| 1283 |
j++; |
| 1284 |
} |
| 1285 |
if (j > max) { |
| 1286 |
max = j; |
| 1287 |
} |
| 1288 |
if (j < NUM_COUNTERS) { |
| 1289 |
count[j]++; |
| 1290 |
} else { |
| 1291 |
overflow++; |
| 1292 |
} |
| 1293 |
tmp = j; |
| 1294 |
average += (tmp+1.0)*(tmp/tablePtr->numEntries)/2.0; |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
result = Blt_Malloc((unsigned) ((NUM_COUNTERS*60) + 300)); |
| 1301 |
#if SIZEOF_VOID_P == 8 |
| 1302 |
sprintf(result, "%ld entries in table, %ld buckets\n", |
| 1303 |
tablePtr->numEntries, tablePtr->numBuckets); |
| 1304 |
#else |
| 1305 |
sprintf(result, "%d entries in table, %d buckets\n", |
| 1306 |
tablePtr->numEntries, tablePtr->numBuckets); |
| 1307 |
#endif |
| 1308 |
p = result + strlen(result); |
| 1309 |
for (i = 0; i < NUM_COUNTERS; i++) { |
| 1310 |
#if SIZEOF_VOID_P == 8 |
| 1311 |
sprintf(p, "number of buckets with %ld entries: %ld\n", |
| 1312 |
i, count[i]); |
| 1313 |
#else |
| 1314 |
sprintf(p, "number of buckets with %d entries: %d\n", |
| 1315 |
i, count[i]); |
| 1316 |
#endif |
| 1317 |
p += strlen(p); |
| 1318 |
} |
| 1319 |
#if SIZEOF_VOID_P == 8 |
| 1320 |
sprintf(p, "number of buckets with %d or more entries: %ld\n", |
| 1321 |
NUM_COUNTERS, overflow); |
| 1322 |
#else |
| 1323 |
sprintf(p, "number of buckets with %d or more entries: %d\n", |
| 1324 |
NUM_COUNTERS, overflow); |
| 1325 |
#endif |
| 1326 |
p += strlen(p); |
| 1327 |
sprintf(p, "average search distance for entry: %.2f\n", average); |
| 1328 |
p += strlen(p); |
| 1329 |
#if SIZEOF_VOID_P == 8 |
| 1330 |
sprintf(p, "maximum search distance for entry: %ld", max); |
| 1331 |
#else |
| 1332 |
sprintf(p, "maximum search distance for entry: %d", max); |
| 1333 |
#endif |
| 1334 |
return result; |
| 1335 |
} |