| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tkPort.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 23 |
# undef Tcl_FindHashEntry |
| 24 |
# undef Tcl_CreateHashEntry |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#define REBUILD_MULTIPLIER 3 |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define RANDOM_INDEX(tablePtr, i) \ |
| 43 |
(((((long) (i))*1103515245) >> (tablePtr)->downShift) & (tablePtr)->mask) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static Tcl_HashEntry * AllocArrayEntry _ANSI_ARGS_(( |
| 50 |
Tcl_HashTable *tablePtr, |
| 51 |
VOID *keyPtr)); |
| 52 |
static int CompareArrayKeys _ANSI_ARGS_(( |
| 53 |
VOID *keyPtr, Tcl_HashEntry *hPtr)); |
| 54 |
static unsigned int HashArrayKey _ANSI_ARGS_(( |
| 55 |
Tcl_HashTable *tablePtr, |
| 56 |
VOID *keyPtr)); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#if 0 |
| 63 |
static Tcl_HashEntry * AllocOneWordEntry _ANSI_ARGS_(( |
| 64 |
Tcl_HashTable *tablePtr, |
| 65 |
VOID *keyPtr)); |
| 66 |
static int CompareOneWordKeys _ANSI_ARGS_(( |
| 67 |
VOID *keyPtr, Tcl_HashEntry *hPtr)); |
| 68 |
static unsigned int HashOneWordKey _ANSI_ARGS_(( |
| 69 |
Tcl_HashTable *tablePtr, |
| 70 |
VOID *keyPtr)); |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static Tcl_HashEntry * AllocStringEntry _ANSI_ARGS_(( |
| 78 |
Tcl_HashTable *tablePtr, |
| 79 |
VOID *keyPtr)); |
| 80 |
static int CompareStringKeys _ANSI_ARGS_(( |
| 81 |
VOID *keyPtr, Tcl_HashEntry *hPtr)); |
| 82 |
static unsigned int HashStringKey _ANSI_ARGS_(( |
| 83 |
Tcl_HashTable *tablePtr, |
| 84 |
VOID *keyPtr)); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 91 |
static Tcl_HashEntry * BogusFind _ANSI_ARGS_((Tcl_HashTable *tablePtr, |
| 92 |
CONST char *key)); |
| 93 |
static Tcl_HashEntry * BogusCreate _ANSI_ARGS_((Tcl_HashTable *tablePtr, |
| 94 |
CONST char *key, int *newPtr)); |
| 95 |
#endif |
| 96 |
|
| 97 |
static void RebuildTable _ANSI_ARGS_((Tcl_HashTable *tablePtr)); |
| 98 |
|
| 99 |
Tcl_HashKeyType tclArrayHashKeyType = { |
| 100 |
TCL_HASH_KEY_TYPE_VERSION, |
| 101 |
TCL_HASH_KEY_RANDOMIZE_HASH, |
| 102 |
HashArrayKey, |
| 103 |
CompareArrayKeys, |
| 104 |
AllocArrayEntry, |
| 105 |
NULL |
| 106 |
}; |
| 107 |
|
| 108 |
Tcl_HashKeyType tclOneWordHashKeyType = { |
| 109 |
TCL_HASH_KEY_TYPE_VERSION, |
| 110 |
0, |
| 111 |
NULL, |
| 112 |
NULL, |
| 113 |
NULL, |
| 114 |
NULL |
| 115 |
}; |
| 116 |
|
| 117 |
Tcl_HashKeyType tclStringHashKeyType = { |
| 118 |
TCL_HASH_KEY_TYPE_VERSION, |
| 119 |
0, |
| 120 |
HashStringKey, |
| 121 |
CompareStringKeys, |
| 122 |
AllocStringEntry, |
| 123 |
NULL |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#undef Tcl_InitHashTable |
| 146 |
void |
| 147 |
Tcl_InitHashTable(tablePtr, keyType) |
| 148 |
register Tcl_HashTable *tablePtr; |
| 149 |
|
| 150 |
int keyType; |
| 151 |
|
| 152 |
|
| 153 |
{ |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
Tcl_InitCustomHashTable(tablePtr, keyType, (Tcl_HashKeyType *) -1); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
void |
| 183 |
Tcl_InitCustomHashTable(tablePtr, keyType, typePtr) |
| 184 |
register Tcl_HashTable *tablePtr; |
| 185 |
|
| 186 |
int keyType; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
Tcl_HashKeyType *typePtr; |
| 192 |
|
| 193 |
{ |
| 194 |
#if (TCL_SMALL_HASH_TABLE != 4) |
| 195 |
panic("Tcl_InitCustomHashTable: TCL_SMALL_HASH_TABLE is %d, not 4\n", |
| 196 |
TCL_SMALL_HASH_TABLE); |
| 197 |
#endif |
| 198 |
|
| 199 |
tablePtr->buckets = tablePtr->staticBuckets; |
| 200 |
tablePtr->staticBuckets[0] = tablePtr->staticBuckets[1] = 0; |
| 201 |
tablePtr->staticBuckets[2] = tablePtr->staticBuckets[3] = 0; |
| 202 |
tablePtr->numBuckets = TCL_SMALL_HASH_TABLE; |
| 203 |
tablePtr->numEntries = 0; |
| 204 |
tablePtr->rebuildSize = TCL_SMALL_HASH_TABLE*REBUILD_MULTIPLIER; |
| 205 |
tablePtr->downShift = 28; |
| 206 |
tablePtr->mask = 3; |
| 207 |
tablePtr->keyType = keyType; |
| 208 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 209 |
tablePtr->findProc = Tcl_FindHashEntry; |
| 210 |
tablePtr->createProc = Tcl_CreateHashEntry; |
| 211 |
|
| 212 |
if (typePtr == NULL) { |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
} else if (typePtr != (Tcl_HashKeyType *) -1) { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
tablePtr->typePtr = typePtr; |
| 223 |
} else { |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
} |
| 229 |
#else |
| 230 |
if (typePtr == NULL) { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (keyType == TCL_STRING_KEYS) { |
| 235 |
typePtr = &tclStringHashKeyType; |
| 236 |
} else if (keyType == TCL_ONE_WORD_KEYS) { |
| 237 |
typePtr = &tclOneWordHashKeyType; |
| 238 |
} else if (keyType == TCL_CUSTOM_TYPE_KEYS) { |
| 239 |
Tcl_Panic ("No type structure specified for TCL_CUSTOM_TYPE_KEYS"); |
| 240 |
} else if (keyType == TCL_CUSTOM_PTR_KEYS) { |
| 241 |
Tcl_Panic ("No type structure specified for TCL_CUSTOM_PTR_KEYS"); |
| 242 |
} else { |
| 243 |
typePtr = &tclArrayHashKeyType; |
| 244 |
} |
| 245 |
} else if (typePtr == (Tcl_HashKeyType *) -1) { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
Tcl_Panic ("Hash table is not compatible"); |
| 251 |
} |
| 252 |
tablePtr->typePtr = typePtr; |
| 253 |
#endif |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
Tcl_HashEntry * |
| 274 |
Tcl_FindHashEntry(tablePtr, key) |
| 275 |
Tcl_HashTable *tablePtr; |
| 276 |
CONST char *key; |
| 277 |
{ |
| 278 |
register Tcl_HashEntry *hPtr; |
| 279 |
Tcl_HashKeyType *typePtr; |
| 280 |
unsigned int hash; |
| 281 |
int index; |
| 282 |
|
| 283 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 284 |
if (tablePtr->keyType == TCL_STRING_KEYS) { |
| 285 |
typePtr = &tclStringHashKeyType; |
| 286 |
} else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) { |
| 287 |
typePtr = &tclOneWordHashKeyType; |
| 288 |
} else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS |
| 289 |
|| tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) { |
| 290 |
typePtr = tablePtr->typePtr; |
| 291 |
} else { |
| 292 |
typePtr = &tclArrayHashKeyType; |
| 293 |
} |
| 294 |
#else |
| 295 |
typePtr = tablePtr->typePtr; |
| 296 |
if (typePtr == NULL) { |
| 297 |
Tcl_Panic("called Tcl_FindHashEntry on deleted table"); |
| 298 |
return NULL; |
| 299 |
} |
| 300 |
#endif |
| 301 |
|
| 302 |
if (typePtr->hashKeyProc) { |
| 303 |
hash = typePtr->hashKeyProc (tablePtr, (VOID *) key); |
| 304 |
if (typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) { |
| 305 |
index = RANDOM_INDEX (tablePtr, hash); |
| 306 |
} else { |
| 307 |
index = hash & tablePtr->mask; |
| 308 |
} |
| 309 |
} else { |
| 310 |
hash = (unsigned int) key; |
| 311 |
index = RANDOM_INDEX (tablePtr, hash); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if (typePtr->compareKeysProc) { |
| 319 |
for (hPtr = tablePtr->buckets[index]; hPtr != NULL; |
| 320 |
hPtr = hPtr->nextPtr) { |
| 321 |
#if TCL_HASH_KEY_STORE_HASH |
| 322 |
if (hash != (unsigned int) hPtr->hash) { |
| 323 |
continue; |
| 324 |
} |
| 325 |
#endif |
| 326 |
if (typePtr->compareKeysProc ((VOID *) key, hPtr)) { |
| 327 |
return hPtr; |
| 328 |
} |
| 329 |
} |
| 330 |
} else { |
| 331 |
for (hPtr = tablePtr->buckets[index]; hPtr != NULL; |
| 332 |
hPtr = hPtr->nextPtr) { |
| 333 |
#if TCL_HASH_KEY_STORE_HASH |
| 334 |
if (hash != (unsigned int) hPtr->hash) { |
| 335 |
continue; |
| 336 |
} |
| 337 |
#endif |
| 338 |
if (key == hPtr->key.oneWordValue) { |
| 339 |
return hPtr; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return NULL; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
Tcl_HashEntry * |
| 369 |
Tcl_CreateHashEntry(tablePtr, key, newPtr) |
| 370 |
Tcl_HashTable *tablePtr; |
| 371 |
CONST char *key; |
| 372 |
|
| 373 |
int *newPtr; |
| 374 |
|
| 375 |
{ |
| 376 |
register Tcl_HashEntry *hPtr; |
| 377 |
Tcl_HashKeyType *typePtr; |
| 378 |
unsigned int hash; |
| 379 |
int index; |
| 380 |
|
| 381 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 382 |
if (tablePtr->keyType == TCL_STRING_KEYS) { |
| 383 |
typePtr = &tclStringHashKeyType; |
| 384 |
} else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) { |
| 385 |
typePtr = &tclOneWordHashKeyType; |
| 386 |
} else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS |
| 387 |
|| tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) { |
| 388 |
typePtr = tablePtr->typePtr; |
| 389 |
} else { |
| 390 |
typePtr = &tclArrayHashKeyType; |
| 391 |
} |
| 392 |
#else |
| 393 |
typePtr = tablePtr->typePtr; |
| 394 |
if (typePtr == NULL) { |
| 395 |
Tcl_Panic("called Tcl_CreateHashEntry on deleted table"); |
| 396 |
return NULL; |
| 397 |
} |
| 398 |
#endif |
| 399 |
|
| 400 |
if (typePtr->hashKeyProc) { |
| 401 |
hash = typePtr->hashKeyProc (tablePtr, (VOID *) key); |
| 402 |
if (typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) { |
| 403 |
index = RANDOM_INDEX (tablePtr, hash); |
| 404 |
} else { |
| 405 |
index = hash & tablePtr->mask; |
| 406 |
} |
| 407 |
} else { |
| 408 |
hash = (unsigned int) key; |
| 409 |
index = RANDOM_INDEX (tablePtr, hash); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
if (typePtr->compareKeysProc) { |
| 417 |
for (hPtr = tablePtr->buckets[index]; hPtr != NULL; |
| 418 |
hPtr = hPtr->nextPtr) { |
| 419 |
#if TCL_HASH_KEY_STORE_HASH |
| 420 |
if (hash != (unsigned int) hPtr->hash) { |
| 421 |
continue; |
| 422 |
} |
| 423 |
#endif |
| 424 |
if (typePtr->compareKeysProc ((VOID *) key, hPtr)) { |
| 425 |
*newPtr = 0; |
| 426 |
return hPtr; |
| 427 |
} |
| 428 |
} |
| 429 |
} else { |
| 430 |
for (hPtr = tablePtr->buckets[index]; hPtr != NULL; |
| 431 |
hPtr = hPtr->nextPtr) { |
| 432 |
#if TCL_HASH_KEY_STORE_HASH |
| 433 |
if (hash != (unsigned int) hPtr->hash) { |
| 434 |
continue; |
| 435 |
} |
| 436 |
#endif |
| 437 |
if (key == hPtr->key.oneWordValue) { |
| 438 |
*newPtr = 0; |
| 439 |
return hPtr; |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
*newPtr = 1; |
| 449 |
if (typePtr->allocEntryProc) { |
| 450 |
hPtr = typePtr->allocEntryProc (tablePtr, (VOID *) key); |
| 451 |
} else { |
| 452 |
hPtr = (Tcl_HashEntry *) ckalloc((unsigned) sizeof(Tcl_HashEntry)); |
| 453 |
hPtr->key.oneWordValue = (char *) key; |
| 454 |
} |
| 455 |
|
| 456 |
hPtr->tablePtr = tablePtr; |
| 457 |
#if TCL_HASH_KEY_STORE_HASH |
| 458 |
# if TCL_PRESERVE_BINARY_COMPATABILITY |
| 459 |
hPtr->hash = (VOID *) hash; |
| 460 |
# else |
| 461 |
hPtr->hash = hash; |
| 462 |
# endif |
| 463 |
hPtr->nextPtr = tablePtr->buckets[index]; |
| 464 |
tablePtr->buckets[index] = hPtr; |
| 465 |
#else |
| 466 |
hPtr->bucketPtr = &(tablePtr->buckets[index]); |
| 467 |
hPtr->nextPtr = *hPtr->bucketPtr; |
| 468 |
*hPtr->bucketPtr = hPtr; |
| 469 |
#endif |
| 470 |
hPtr->clientData = 0; |
| 471 |
tablePtr->numEntries++; |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if (tablePtr->numEntries >= tablePtr->rebuildSize) { |
| 479 |
RebuildTable(tablePtr); |
| 480 |
} |
| 481 |
return hPtr; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
void |
| 504 |
Tcl_DeleteHashEntry(entryPtr) |
| 505 |
Tcl_HashEntry *entryPtr; |
| 506 |
{ |
| 507 |
register Tcl_HashEntry *prevPtr; |
| 508 |
Tcl_HashKeyType *typePtr; |
| 509 |
Tcl_HashTable *tablePtr; |
| 510 |
Tcl_HashEntry **bucketPtr; |
| 511 |
#if TCL_HASH_KEY_STORE_HASH |
| 512 |
int index; |
| 513 |
#endif |
| 514 |
|
| 515 |
tablePtr = entryPtr->tablePtr; |
| 516 |
|
| 517 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 518 |
if (tablePtr->keyType == TCL_STRING_KEYS) { |
| 519 |
typePtr = &tclStringHashKeyType; |
| 520 |
} else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) { |
| 521 |
typePtr = &tclOneWordHashKeyType; |
| 522 |
} else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS |
| 523 |
|| tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) { |
| 524 |
typePtr = tablePtr->typePtr; |
| 525 |
} else { |
| 526 |
typePtr = &tclArrayHashKeyType; |
| 527 |
} |
| 528 |
#else |
| 529 |
typePtr = tablePtr->typePtr; |
| 530 |
#endif |
| 531 |
|
| 532 |
#if TCL_HASH_KEY_STORE_HASH |
| 533 |
if (typePtr->hashKeyProc == NULL |
| 534 |
|| typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) { |
| 535 |
index = RANDOM_INDEX (tablePtr, entryPtr->hash); |
| 536 |
} else { |
| 537 |
index = ((unsigned int) entryPtr->hash) & tablePtr->mask; |
| 538 |
} |
| 539 |
|
| 540 |
bucketPtr = &(tablePtr->buckets[index]); |
| 541 |
#else |
| 542 |
bucketPtr = entryPtr->bucketPtr; |
| 543 |
#endif |
| 544 |
|
| 545 |
if (*bucketPtr == entryPtr) { |
| 546 |
*bucketPtr = entryPtr->nextPtr; |
| 547 |
} else { |
| 548 |
for (prevPtr = *bucketPtr; ; prevPtr = prevPtr->nextPtr) { |
| 549 |
if (prevPtr == NULL) { |
| 550 |
panic("malformed bucket chain in Tcl_DeleteHashEntry"); |
| 551 |
} |
| 552 |
if (prevPtr->nextPtr == entryPtr) { |
| 553 |
prevPtr->nextPtr = entryPtr->nextPtr; |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
tablePtr->numEntries--; |
| 560 |
if (typePtr->freeEntryProc) { |
| 561 |
typePtr->freeEntryProc (entryPtr); |
| 562 |
} else { |
| 563 |
ckfree((char *) entryPtr); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
void |
| 585 |
Tcl_DeleteHashTable(tablePtr) |
| 586 |
register Tcl_HashTable *tablePtr; |
| 587 |
{ |
| 588 |
register Tcl_HashEntry *hPtr, *nextPtr; |
| 589 |
Tcl_HashKeyType *typePtr; |
| 590 |
int i; |
| 591 |
|
| 592 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 593 |
if (tablePtr->keyType == TCL_STRING_KEYS) { |
| 594 |
typePtr = &tclStringHashKeyType; |
| 595 |
} else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) { |
| 596 |
typePtr = &tclOneWordHashKeyType; |
| 597 |
} else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS |
| 598 |
|| tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) { |
| 599 |
typePtr = tablePtr->typePtr; |
| 600 |
} else { |
| 601 |
typePtr = &tclArrayHashKeyType; |
| 602 |
} |
| 603 |
#else |
| 604 |
typePtr = tablePtr->typePtr; |
| 605 |
#endif |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
for (i = 0; i < tablePtr->numBuckets; i++) { |
| 612 |
hPtr = tablePtr->buckets[i]; |
| 613 |
while (hPtr != NULL) { |
| 614 |
nextPtr = hPtr->nextPtr; |
| 615 |
if (typePtr->freeEntryProc) { |
| 616 |
typePtr->freeEntryProc (hPtr); |
| 617 |
} else { |
| 618 |
ckfree((char *) hPtr); |
| 619 |
} |
| 620 |
hPtr = nextPtr; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
if (tablePtr->buckets != tablePtr->staticBuckets) { |
| 629 |
ckfree((char *) tablePtr->buckets); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 638 |
tablePtr->findProc = BogusFind; |
| 639 |
tablePtr->createProc = BogusCreate; |
| 640 |
#else |
| 641 |
tablePtr->typePtr = NULL; |
| 642 |
#endif |
| 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 |
Tcl_HashEntry * |
| 668 |
Tcl_FirstHashEntry(tablePtr, searchPtr) |
| 669 |
Tcl_HashTable *tablePtr; |
| 670 |
Tcl_HashSearch *searchPtr; |
| 671 |
|
| 672 |
{ |
| 673 |
searchPtr->tablePtr = tablePtr; |
| 674 |
searchPtr->nextIndex = 0; |
| 675 |
searchPtr->nextEntryPtr = NULL; |
| 676 |
return Tcl_NextHashEntry(searchPtr); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
Tcl_HashEntry * |
| 699 |
Tcl_NextHashEntry(searchPtr) |
| 700 |
register Tcl_HashSearch *searchPtr; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
{ |
| 705 |
Tcl_HashEntry *hPtr; |
| 706 |
|
| 707 |
while (searchPtr->nextEntryPtr == NULL) { |
| 708 |
if (searchPtr->nextIndex >= searchPtr->tablePtr->numBuckets) { |
| 709 |
return NULL; |
| 710 |
} |
| 711 |
searchPtr->nextEntryPtr = |
| 712 |
searchPtr->tablePtr->buckets[searchPtr->nextIndex]; |
| 713 |
searchPtr->nextIndex++; |
| 714 |
} |
| 715 |
hPtr = searchPtr->nextEntryPtr; |
| 716 |
searchPtr->nextEntryPtr = hPtr->nextPtr; |
| 717 |
return hPtr; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
CONST char * |
| 740 |
Tcl_HashStats(tablePtr) |
| 741 |
Tcl_HashTable *tablePtr; |
| 742 |
{ |
| 743 |
#define NUM_COUNTERS 10 |
| 744 |
int count[NUM_COUNTERS], overflow, i, j; |
| 745 |
double average, tmp; |
| 746 |
register Tcl_HashEntry *hPtr; |
| 747 |
char *result, *p; |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
for (i = 0; i < NUM_COUNTERS; i++) { |
| 754 |
count[i] = 0; |
| 755 |
} |
| 756 |
overflow = 0; |
| 757 |
average = 0.0; |
| 758 |
for (i = 0; i < tablePtr->numBuckets; i++) { |
| 759 |
j = 0; |
| 760 |
for (hPtr = tablePtr->buckets[i]; hPtr != NULL; hPtr = hPtr->nextPtr) { |
| 761 |
j++; |
| 762 |
} |
| 763 |
if (j < NUM_COUNTERS) { |
| 764 |
count[j]++; |
| 765 |
} else { |
| 766 |
overflow++; |
| 767 |
} |
| 768 |
tmp = j; |
| 769 |
average += (tmp+1.0)*(tmp/tablePtr->numEntries)/2.0; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
result = (char *) ckalloc((unsigned) ((NUM_COUNTERS*60) + 300)); |
| 777 |
sprintf(result, "%d entries in table, %d buckets\n", |
| 778 |
tablePtr->numEntries, tablePtr->numBuckets); |
| 779 |
p = result + strlen(result); |
| 780 |
for (i = 0; i < NUM_COUNTERS; i++) { |
| 781 |
sprintf(p, "number of buckets with %d entries: %d\n", |
| 782 |
i, count[i]); |
| 783 |
p += strlen(p); |
| 784 |
} |
| 785 |
sprintf(p, "number of buckets with %d or more entries: %d\n", |
| 786 |
NUM_COUNTERS, overflow); |
| 787 |
p += strlen(p); |
| 788 |
sprintf(p, "average search distance for entry: %.1f", average); |
| 789 |
return result; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
static Tcl_HashEntry * |
| 809 |
AllocArrayEntry(tablePtr, keyPtr) |
| 810 |
Tcl_HashTable *tablePtr; |
| 811 |
VOID *keyPtr; |
| 812 |
{ |
| 813 |
int *array = (int *) keyPtr; |
| 814 |
register int *iPtr1, *iPtr2; |
| 815 |
Tcl_HashEntry *hPtr; |
| 816 |
int count; |
| 817 |
unsigned int size; |
| 818 |
|
| 819 |
count = tablePtr->keyType; |
| 820 |
|
| 821 |
size = sizeof(Tcl_HashEntry) + (count*sizeof(int)) - sizeof(hPtr->key); |
| 822 |
if (size < sizeof(Tcl_HashEntry)) |
| 823 |
size = sizeof(Tcl_HashEntry); |
| 824 |
hPtr = (Tcl_HashEntry *) ckalloc(size); |
| 825 |
|
| 826 |
for (iPtr1 = array, iPtr2 = hPtr->key.words; |
| 827 |
count > 0; count--, iPtr1++, iPtr2++) { |
| 828 |
*iPtr2 = *iPtr1; |
| 829 |
} |
| 830 |
|
| 831 |
return hPtr; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
static int |
| 852 |
CompareArrayKeys(keyPtr, hPtr) |
| 853 |
VOID *keyPtr; |
| 854 |
Tcl_HashEntry *hPtr; |
| 855 |
{ |
| 856 |
register CONST int *iPtr1 = (CONST int *) keyPtr; |
| 857 |
register CONST int *iPtr2 = (CONST int *) hPtr->key.words; |
| 858 |
Tcl_HashTable *tablePtr = hPtr->tablePtr; |
| 859 |
int count; |
| 860 |
|
| 861 |
for (count = tablePtr->keyType; ; count--, iPtr1++, iPtr2++) { |
| 862 |
if (count == 0) { |
| 863 |
return 1; |
| 864 |
} |
| 865 |
if (*iPtr1 != *iPtr2) { |
| 866 |
break; |
| 867 |
} |
| 868 |
} |
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
static unsigned int |
| 891 |
HashArrayKey(tablePtr, keyPtr) |
| 892 |
Tcl_HashTable *tablePtr; |
| 893 |
VOID *keyPtr; |
| 894 |
{ |
| 895 |
register CONST int *array = (CONST int *) keyPtr; |
| 896 |
register unsigned int result; |
| 897 |
int count; |
| 898 |
|
| 899 |
for (result = 0, count = tablePtr->keyType; count > 0; |
| 900 |
count--, array++) { |
| 901 |
result += *array; |
| 902 |
} |
| 903 |
return result; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
static Tcl_HashEntry * |
| 923 |
AllocStringEntry(tablePtr, keyPtr) |
| 924 |
Tcl_HashTable *tablePtr; |
| 925 |
VOID *keyPtr; |
| 926 |
{ |
| 927 |
CONST char *string = (CONST char *) keyPtr; |
| 928 |
Tcl_HashEntry *hPtr; |
| 929 |
unsigned int size; |
| 930 |
|
| 931 |
size = sizeof(Tcl_HashEntry) + strlen(string) + 1 - sizeof(hPtr->key); |
| 932 |
if (size < sizeof(Tcl_HashEntry)) |
| 933 |
size = sizeof(Tcl_HashEntry); |
| 934 |
hPtr = (Tcl_HashEntry *) ckalloc(size); |
| 935 |
memcpy(hPtr->key.string, string, strlen(string) + 1); |
| 936 |
return hPtr; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
static int |
| 957 |
CompareStringKeys(keyPtr, hPtr) |
| 958 |
VOID *keyPtr; |
| 959 |
Tcl_HashEntry *hPtr; |
| 960 |
{ |
| 961 |
register CONST char *p1 = (CONST char *) keyPtr; |
| 962 |
register CONST char *p2 = (CONST char *) hPtr->key.string; |
| 963 |
|
| 964 |
for (;; p1++, p2++) { |
| 965 |
if (*p1 != *p2) { |
| 966 |
break; |
| 967 |
} |
| 968 |
if (*p1 == '\0') { |
| 969 |
return 1; |
| 970 |
} |
| 971 |
} |
| 972 |
return 0; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
static unsigned int |
| 994 |
HashStringKey(tablePtr, keyPtr) |
| 995 |
Tcl_HashTable *tablePtr; |
| 996 |
VOID *keyPtr; |
| 997 |
{ |
| 998 |
register CONST char *string = (CONST char *) keyPtr; |
| 999 |
register unsigned int result; |
| 1000 |
register int c; |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
result = 0; |
| 1019 |
while (1) { |
| 1020 |
c = *string; |
| 1021 |
if (c == 0) { |
| 1022 |
break; |
| 1023 |
} |
| 1024 |
result += (result<<3) + c; |
| 1025 |
string++; |
| 1026 |
} |
| 1027 |
return result; |
| 1028 |
} |
| 1029 |
|
| 1030 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
static Tcl_HashEntry * |
| 1051 |
BogusFind(tablePtr, key) |
| 1052 |
Tcl_HashTable *tablePtr; |
| 1053 |
CONST char *key; |
| 1054 |
{ |
| 1055 |
panic("called Tcl_FindHashEntry on deleted table"); |
| 1056 |
return NULL; |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
static Tcl_HashEntry * |
| 1079 |
BogusCreate(tablePtr, key, newPtr) |
| 1080 |
Tcl_HashTable *tablePtr; |
| 1081 |
CONST char *key; |
| 1082 |
|
| 1083 |
int *newPtr; |
| 1084 |
|
| 1085 |
{ |
| 1086 |
panic("called Tcl_CreateHashEntry on deleted table"); |
| 1087 |
return NULL; |
| 1088 |
} |
| 1089 |
#endif |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
static void |
| 1112 |
RebuildTable(tablePtr) |
| 1113 |
register Tcl_HashTable *tablePtr; |
| 1114 |
{ |
| 1115 |
int oldSize, count, index; |
| 1116 |
Tcl_HashEntry **oldBuckets; |
| 1117 |
register Tcl_HashEntry **oldChainPtr, **newChainPtr; |
| 1118 |
register Tcl_HashEntry *hPtr; |
| 1119 |
Tcl_HashKeyType *typePtr; |
| 1120 |
VOID *key; |
| 1121 |
|
| 1122 |
oldSize = tablePtr->numBuckets; |
| 1123 |
oldBuckets = tablePtr->buckets; |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
tablePtr->numBuckets *= 4; |
| 1131 |
tablePtr->buckets = (Tcl_HashEntry **) ckalloc((unsigned) |
| 1132 |
(tablePtr->numBuckets * sizeof(Tcl_HashEntry *))); |
| 1133 |
for (count = tablePtr->numBuckets, newChainPtr = tablePtr->buckets; |
| 1134 |
count > 0; count--, newChainPtr++) { |
| 1135 |
*newChainPtr = NULL; |
| 1136 |
} |
| 1137 |
tablePtr->rebuildSize *= 4; |
| 1138 |
tablePtr->downShift -= 2; |
| 1139 |
tablePtr->mask = (tablePtr->mask << 2) + 3; |
| 1140 |
|
| 1141 |
#if TCL_PRESERVE_BINARY_COMPATABILITY |
| 1142 |
if (tablePtr->keyType == TCL_STRING_KEYS) { |
| 1143 |
typePtr = &tclStringHashKeyType; |
| 1144 |
} else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) { |
| 1145 |
typePtr = &tclOneWordHashKeyType; |
| 1146 |
} else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS |
| 1147 |
|| tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) { |
| 1148 |
typePtr = tablePtr->typePtr; |
| 1149 |
} else { |
| 1150 |
typePtr = &tclArrayHashKeyType; |
| 1151 |
} |
| 1152 |
#else |
| 1153 |
typePtr = tablePtr->typePtr; |
| 1154 |
#endif |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
for (oldChainPtr = oldBuckets; oldSize > 0; oldSize--, oldChainPtr++) { |
| 1161 |
for (hPtr = *oldChainPtr; hPtr != NULL; hPtr = *oldChainPtr) { |
| 1162 |
*oldChainPtr = hPtr->nextPtr; |
| 1163 |
|
| 1164 |
key = (VOID *) Tcl_GetHashKey (tablePtr, hPtr); |
| 1165 |
|
| 1166 |
#if TCL_HASH_KEY_STORE_HASH |
| 1167 |
if (typePtr->hashKeyProc == NULL |
| 1168 |
|| typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) { |
| 1169 |
index = RANDOM_INDEX (tablePtr, hPtr->hash); |
| 1170 |
} else { |
| 1171 |
index = ((unsigned int) hPtr->hash) & tablePtr->mask; |
| 1172 |
} |
| 1173 |
hPtr->nextPtr = tablePtr->buckets[index]; |
| 1174 |
tablePtr->buckets[index] = hPtr; |
| 1175 |
#else |
| 1176 |
if (typePtr->hashKeyProc) { |
| 1177 |
unsigned int hash; |
| 1178 |
hash = typePtr->hashKeyProc (tablePtr, (VOID *) key); |
| 1179 |
if (typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) { |
| 1180 |
index = RANDOM_INDEX (tablePtr, hash); |
| 1181 |
} else { |
| 1182 |
index = hash & tablePtr->mask; |
| 1183 |
} |
| 1184 |
} else { |
| 1185 |
index = RANDOM_INDEX (tablePtr, key); |
| 1186 |
} |
| 1187 |
|
| 1188 |
hPtr->bucketPtr = &(tablePtr->buckets[index]); |
| 1189 |
hPtr->nextPtr = *hPtr->bucketPtr; |
| 1190 |
*hPtr->bucketPtr = hPtr; |
| 1191 |
#endif |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
if (oldBuckets != tablePtr->staticBuckets) { |
| 1200 |
ckfree((char *) oldBuckets); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|