| 1 |
/* hash.c - hash table lookup strings - |
| 2 |
Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GAS, the GNU Assembler. |
| 5 |
|
| 6 |
GAS is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GAS is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GAS; see the file COPYING. If not, write to |
| 18 |
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 |
|
| 20 |
/* |
| 21 |
* BUGS, GRIPES, APOLOGIA etc. |
| 22 |
* |
| 23 |
* A typical user doesn't need ALL this: I intend to make a library out |
| 24 |
* of it one day - Dean Elsner. |
| 25 |
* Also, I want to change the definition of a symbol to (address,length) |
| 26 |
* so I can put arbitrary binary in the names stored. [see hsh.c for that] |
| 27 |
* |
| 28 |
* This slime is common coupled inside the module. Com-coupling (and other |
| 29 |
* vandalism) was done to speed running time. The interfaces at the |
| 30 |
* module's edges are adequately clean. |
| 31 |
* |
| 32 |
* There is no way to (a) run a test script through this heap and (b) |
| 33 |
* compare results with previous scripts, to see if we have broken any |
| 34 |
* code. Use GNU (f)utilities to do this. A few commands assist test. |
| 35 |
* The testing is awkward: it tries to be both batch & interactive. |
| 36 |
* For now, interactive rules! |
| 37 |
*/ |
| 38 |
|
| 39 |
/* |
| 40 |
* The idea is to implement a symbol table. A test jig is here. |
| 41 |
* Symbols are arbitrary strings; they can't contain '\0'. |
| 42 |
* [See hsh.c for a more general symbol flavour.] |
| 43 |
* Each symbol is associated with a char*, which can point to anything |
| 44 |
* you want, allowing an arbitrary property list for each symbol. |
| 45 |
* |
| 46 |
* The basic operations are: |
| 47 |
* |
| 48 |
* new creates symbol table, returns handle |
| 49 |
* find (symbol) returns char* |
| 50 |
* insert (symbol,char*) error if symbol already in table |
| 51 |
* delete (symbol) returns char* if symbol was in table |
| 52 |
* apply so you can delete all symbols before die() |
| 53 |
* die destroy symbol table (free up memory) |
| 54 |
* |
| 55 |
* Supplementary functions include |
| 56 |
* |
| 57 |
* say how big? what % full? |
| 58 |
* replace (symbol,newval) report previous value |
| 59 |
* jam (symbol,value) assert symbol:=value |
| 60 |
* |
| 61 |
* You, the caller, have control over errors: this just reports them. |
| 62 |
* |
| 63 |
* This package requires malloc(), free(). |
| 64 |
* Malloc(size) returns NULL or address of char[size]. |
| 65 |
* Free(address) frees same. |
| 66 |
*/ |
| 67 |
|
| 68 |
/* |
| 69 |
* The code and its structures are re-enterent. |
| 70 |
* |
| 71 |
* Before you do anything else, you must call hash_new() which will |
| 72 |
* return the address of a hash-table-control-block. You then use |
| 73 |
* this address as a handle of the symbol table by passing it to all |
| 74 |
* the other hash_...() functions. The only approved way to recover |
| 75 |
* the memory used by the symbol table is to call hash_die() with the |
| 76 |
* handle of the symbol table. |
| 77 |
* |
| 78 |
* Before you call hash_die() you normally delete anything pointed to |
| 79 |
* by individual symbols. After hash_die() you can't use that symbol |
| 80 |
* table again. |
| 81 |
* |
| 82 |
* The char* you associate with a symbol may not be NULL (0) because |
| 83 |
* NULL is returned whenever a symbol is not in the table. Any other |
| 84 |
* value is OK, except DELETED, #defined below. |
| 85 |
* |
| 86 |
* When you supply a symbol string for insertion, YOU MUST PRESERVE THE |
| 87 |
* STRING until that symbol is deleted from the table. The reason is that |
| 88 |
* only the address you supply, NOT the symbol string itself, is stored |
| 89 |
* in the symbol table. |
| 90 |
* |
| 91 |
* You may delete and add symbols arbitrarily. |
| 92 |
* Any or all symbols may have the same 'value' (char *). In fact, these |
| 93 |
* routines don't do anything with your symbol values. |
| 94 |
* |
| 95 |
* You have no right to know where the symbol:char* mapping is stored, |
| 96 |
* because it moves around in memory; also because we may change how it |
| 97 |
* works and we don't want to break your code do we? However the handle |
| 98 |
* (address of struct hash_control) is never changed in |
| 99 |
* the life of the symbol table. |
| 100 |
* |
| 101 |
* What you CAN find out about a symbol table is: |
| 102 |
* how many slots are in the hash table? |
| 103 |
* how many slots are filled with symbols? |
| 104 |
* (total hashes,collisions) for (reads,writes) (*) |
| 105 |
* All of the above values vary in time. |
| 106 |
* (*) some of these numbers will not be meaningful if we change the |
| 107 |
* internals. */ |
| 108 |
|
| 109 |
/* |
| 110 |
* I N T E R N A L |
| 111 |
* |
| 112 |
* Hash table is an array of hash_entries; each entry is a pointer to a |
| 113 |
* a string and a user-supplied value 1 char* wide. |
| 114 |
* |
| 115 |
* The array always has 2 ** n elements, n>0, n integer. |
| 116 |
* There is also a 'wall' entry after the array, which is always empty |
| 117 |
* and acts as a sentinel to stop running off the end of the array. |
| 118 |
* When the array gets too full, we create a new array twice as large |
| 119 |
* and re-hash the symbols into the new array, then forget the old array. |
| 120 |
* (Of course, we copy the values into the new array before we junk the |
| 121 |
* old array!) |
| 122 |
* |
| 123 |
*/ |
| 124 |
|
| 125 |
#include <stdio.h> |
| 126 |
#include <rplay.h> |
| 127 |
|
| 128 |
#ifndef FALSE |
| 129 |
#define FALSE (0) |
| 130 |
#define TRUE (!FALSE) |
| 131 |
#endif /* no FALSE yet */ |
| 132 |
|
| 133 |
#include <ctype.h> |
| 134 |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
| 135 |
|
| 136 |
#if 0 |
| 137 |
#include "as.h" |
| 138 |
#else |
| 139 |
#include "hash.h" |
| 140 |
#endif |
| 141 |
|
| 142 |
#define error as_fatal |
| 143 |
|
| 144 |
#define DELETED ((PTR)1) /* guarenteed invalid address */ |
| 145 |
#define START_POWER (11) /* power of two: size of new hash table */ |
| 146 |
|
| 147 |
/* TRUE if a symbol is in entry @ ptr. */ |
| 148 |
#define islive(ptr) (ptr->hash_string && ptr->hash_string!=DELETED) |
| 149 |
|
| 150 |
/* Number of slots in hash table. The wall does not count here. |
| 151 |
We expect this is always a power of 2. */ |
| 152 |
#define STAT_SIZE (0) |
| 153 |
#define STAT_ACCESS (1) /* number of hash_ask()s */ |
| 154 |
#define STAT__READ (0) /* reading */ |
| 155 |
#define STAT__WRITE (1) /* writing */ |
| 156 |
/* Number of collisions (total). This may exceed STAT_ACCESS if we |
| 157 |
have lots of collisions/access. */ |
| 158 |
#define STAT_COLLIDE (3) |
| 159 |
#define STAT_USED (5) /* slots used right now */ |
| 160 |
#define STATLENGTH (6) /* size of statistics block */ |
| 161 |
#if STATLENGTH != HASH_STATLENGTH |
| 162 |
Panic! Please make #include "stat.h" agree with previous definitions! |
| 163 |
#endif |
| 164 |
|
| 165 |
/* #define SUSPECT to do runtime checks */ |
| 166 |
/* #define TEST to be a test jig for hash...() */ |
| 167 |
|
| 168 |
#ifdef TEST |
| 169 |
/* TEST: use smaller hash table */ |
| 170 |
#undef START_POWER |
| 171 |
#define START_POWER (3) |
| 172 |
#undef START_SIZE |
| 173 |
#define START_SIZE (8) |
| 174 |
#undef START_FULL |
| 175 |
#define START_FULL (4) |
| 176 |
#endif |
| 177 |
|
| 178 |
/*------------------ plan ---------------------------------- i = internal |
| 179 |
|
| 180 |
struct hash_control * c; |
| 181 |
struct hash_entry * e; i |
| 182 |
int b[z]; buffer for statistics |
| 183 |
z size of b |
| 184 |
char * s; symbol string (address) [ key ] |
| 185 |
char * v; value string (address) [datum] |
| 186 |
boolean f; TRUE if we found s in hash table i |
| 187 |
char * t; error string; 0 means OK |
| 188 |
int a; access type [0...n) i |
| 189 |
|
| 190 |
c=hash_new () create new hash_control |
| 191 |
|
| 192 |
hash_die (c) destroy hash_control (and hash table) |
| 193 |
table should be empty. |
| 194 |
doesn't check if table is empty. |
| 195 |
c has no meaning after this. |
| 196 |
|
| 197 |
hash_say (c,b,z) report statistics of hash_control. |
| 198 |
also report number of available statistics. |
| 199 |
|
| 200 |
v=hash_delete (c,s) delete symbol, return old value if any. |
| 201 |
ask() NULL means no old value. |
| 202 |
f |
| 203 |
|
| 204 |
v=hash_replace (c,s,v) replace old value of s with v. |
| 205 |
ask() NULL means no old value: no table change. |
| 206 |
f |
| 207 |
|
| 208 |
t=hash_insert (c,s,v) insert (s,v) in c. |
| 209 |
ask() return error string. |
| 210 |
f it is an error to insert if s is already |
| 211 |
in table. |
| 212 |
if any error, c is unchanged. |
| 213 |
|
| 214 |
t=hash_jam (c,s,v) assert that new value of s will be v. i |
| 215 |
ask() it may decide to GROW the table. i |
| 216 |
f i |
| 217 |
grow() i |
| 218 |
t=hash_grow (c) grow the hash table. i |
| 219 |
jam() will invoke JAM. i |
| 220 |
|
| 221 |
?=hash_apply (c,y) apply y() to every symbol in c. |
| 222 |
y evtries visited in 'unspecified' order. |
| 223 |
|
| 224 |
v=hash_find (c,s) return value of s, or NULL if s not in c. |
| 225 |
ask() |
| 226 |
f |
| 227 |
|
| 228 |
f,e=hash_ask() (c,s,a) return slot where s SHOULD live. i |
| 229 |
code() maintain collision stats in c. i |
| 230 |
|
| 231 |
.=hash_code (c,s) compute hash-code for s, i |
| 232 |
from parameters of c. i |
| 233 |
|
| 234 |
*/ |
| 235 |
|
| 236 |
/* Returned by hash_ask() to stop extra testing. hash_ask() wants to |
| 237 |
return both a slot and a status. This is the status. TRUE: found |
| 238 |
symbol FALSE: absent: empty or deleted slot Also returned by |
| 239 |
hash_jam(). TRUE: we replaced a value FALSE: we inserted a value. */ |
| 240 |
static char hash_found; |
| 241 |
|
| 242 |
static struct hash_entry *hash_ask PARAMS ((struct hash_control *, |
| 243 |
const char *, int)); |
| 244 |
static int hash_code PARAMS ((struct hash_control *, const char *)); |
| 245 |
static const char *hash_grow PARAMS ((struct hash_control *)); |
| 246 |
|
| 247 |
/* Create a new hash table. Return NULL if failed; otherwise return handle |
| 248 |
(address of struct hash). */ |
| 249 |
struct hash_control * |
| 250 |
hash_new () |
| 251 |
{ |
| 252 |
struct hash_control *retval; |
| 253 |
struct hash_entry *room; /* points to hash table */ |
| 254 |
struct hash_entry *wall; |
| 255 |
struct hash_entry *entry; |
| 256 |
int *ip; /* scan stats block of struct hash_control */ |
| 257 |
int *nd; /* limit of stats block */ |
| 258 |
|
| 259 |
room = (struct hash_entry *) xmalloc (sizeof (struct hash_entry) |
| 260 |
/* +1 for the wall entry */ |
| 261 |
* ((1 << START_POWER) + 1)); |
| 262 |
retval = (struct hash_control *) xmalloc (sizeof (struct hash_control)); |
| 263 |
|
| 264 |
nd = retval->hash_stat + STATLENGTH; |
| 265 |
for (ip = retval->hash_stat; ip < nd; ip++) |
| 266 |
*ip = 0; |
| 267 |
|
| 268 |
retval->hash_stat[STAT_SIZE] = 1 << START_POWER; |
| 269 |
retval->hash_mask = (1 << START_POWER) - 1; |
| 270 |
retval->hash_sizelog = START_POWER; |
| 271 |
/* works for 1's compl ok */ |
| 272 |
retval->hash_where = room; |
| 273 |
retval->hash_wall = |
| 274 |
wall = room + (1 << START_POWER); |
| 275 |
retval->hash_full = (1 << START_POWER) / 2; |
| 276 |
for (entry = room; entry <= wall; entry++) |
| 277 |
entry->hash_string = NULL; |
| 278 |
return retval; |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* h a s h _ d i e ( ) |
| 283 |
* |
| 284 |
* Table should be empty, but this is not checked. |
| 285 |
* To empty the table, try hash_apply()ing a symbol deleter. |
| 286 |
* Return to free memory both the hash table and it's control |
| 287 |
* block. |
| 288 |
* 'handle' has no meaning after this function. |
| 289 |
* No errors are recoverable. |
| 290 |
*/ |
| 291 |
void |
| 292 |
hash_die (handle) |
| 293 |
struct hash_control *handle; |
| 294 |
{ |
| 295 |
free ((char *) handle->hash_where); |
| 296 |
free ((char *) handle); |
| 297 |
} |
| 298 |
|
| 299 |
/* |
| 300 |
* h a s h _ s a y ( ) |
| 301 |
* |
| 302 |
* Return the size of the statistics table, and as many statistics as |
| 303 |
* we can until either (a) we have run out of statistics or (b) caller |
| 304 |
* has run out of buffer. |
| 305 |
* NOTE: hash_say treats all statistics alike. |
| 306 |
* These numbers may change with time, due to insertions, deletions |
| 307 |
* and expansions of the table. |
| 308 |
* The first "statistic" returned is the length of hash_stat[]. |
| 309 |
* Then contents of hash_stat[] are read out (in ascending order) |
| 310 |
* until your buffer or hash_stat[] is exausted. |
| 311 |
*/ |
| 312 |
void |
| 313 |
hash_say (handle, buffer, bufsiz) |
| 314 |
struct hash_control *handle; |
| 315 |
int buffer[ /*bufsiz*/ ]; |
| 316 |
int bufsiz; |
| 317 |
{ |
| 318 |
int *nd; /* limit of statistics block */ |
| 319 |
int *ip; /* scan statistics */ |
| 320 |
|
| 321 |
ip = handle->hash_stat; |
| 322 |
nd = ip + min (bufsiz - 1, STATLENGTH); |
| 323 |
if (bufsiz > 0) /* trust nothing! bufsiz<=0 is dangerous */ |
| 324 |
{ |
| 325 |
*buffer++ = STATLENGTH; |
| 326 |
for (; ip < nd; ip++, buffer++) |
| 327 |
{ |
| 328 |
*buffer = *ip; |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/* |
| 334 |
* h a s h _ d e l e t e ( ) |
| 335 |
* |
| 336 |
* Try to delete a symbol from the table. |
| 337 |
* If it was there, return its value (and adjust STAT_USED). |
| 338 |
* Otherwise, return NULL. |
| 339 |
* Anyway, the symbol is not present after this function. |
| 340 |
* |
| 341 |
*/ |
| 342 |
PTR /* NULL if string not in table, else */ |
| 343 |
/* returns value of deleted symbol */ |
| 344 |
hash_delete (handle, string) |
| 345 |
struct hash_control *handle; |
| 346 |
const char *string; |
| 347 |
{ |
| 348 |
PTR retval; |
| 349 |
struct hash_entry *entry; |
| 350 |
|
| 351 |
entry = hash_ask (handle, string, STAT__WRITE); |
| 352 |
if (hash_found) |
| 353 |
{ |
| 354 |
retval = entry->hash_value; |
| 355 |
entry->hash_string = DELETED; |
| 356 |
handle->hash_stat[STAT_USED] -= 1; |
| 357 |
#ifdef SUSPECT |
| 358 |
if (handle->hash_stat[STAT_USED] < 0) |
| 359 |
{ |
| 360 |
error ("hash_delete"); |
| 361 |
} |
| 362 |
#endif /* def SUSPECT */ |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
retval = NULL; |
| 367 |
} |
| 368 |
return (retval); |
| 369 |
} |
| 370 |
|
| 371 |
/* |
| 372 |
* h a s h _ r e p l a c e ( ) |
| 373 |
* |
| 374 |
* Try to replace the old value of a symbol with a new value. |
| 375 |
* Normally return the old value. |
| 376 |
* Return NULL and don't change the table if the symbol is not already |
| 377 |
* in the table. |
| 378 |
*/ |
| 379 |
PTR |
| 380 |
hash_replace (handle, string, value) |
| 381 |
struct hash_control *handle; |
| 382 |
const char *string; |
| 383 |
PTR value; |
| 384 |
{ |
| 385 |
struct hash_entry *entry; |
| 386 |
char *retval; |
| 387 |
|
| 388 |
entry = hash_ask (handle, string, STAT__WRITE); |
| 389 |
if (hash_found) |
| 390 |
{ |
| 391 |
retval = entry->hash_value; |
| 392 |
entry->hash_value = value; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
retval = NULL; |
| 397 |
} |
| 398 |
; |
| 399 |
return retval; |
| 400 |
} |
| 401 |
|
| 402 |
/* |
| 403 |
* h a s h _ i n s e r t ( ) |
| 404 |
* |
| 405 |
* Insert a (symbol-string, value) into the hash table. |
| 406 |
* Return an error string, 0 means OK. |
| 407 |
* It is an 'error' to insert an existing symbol. |
| 408 |
*/ |
| 409 |
|
| 410 |
const char * /* return error string */ |
| 411 |
hash_insert (handle, string, value) |
| 412 |
struct hash_control *handle; |
| 413 |
const char *string; |
| 414 |
PTR value; |
| 415 |
{ |
| 416 |
struct hash_entry *entry; |
| 417 |
const char *retval; |
| 418 |
|
| 419 |
retval = 0; |
| 420 |
if (handle->hash_stat[STAT_USED] > handle->hash_full) |
| 421 |
{ |
| 422 |
retval = hash_grow (handle); |
| 423 |
} |
| 424 |
if (!retval) |
| 425 |
{ |
| 426 |
entry = hash_ask (handle, string, STAT__WRITE); |
| 427 |
if (hash_found) |
| 428 |
{ |
| 429 |
retval = "exists"; |
| 430 |
} |
| 431 |
else |
| 432 |
{ |
| 433 |
entry->hash_value = value; |
| 434 |
entry->hash_string = string; |
| 435 |
handle->hash_stat[STAT_USED] += 1; |
| 436 |
} |
| 437 |
} |
| 438 |
return retval; |
| 439 |
} |
| 440 |
|
| 441 |
/* |
| 442 |
* h a s h _ j a m ( ) |
| 443 |
* |
| 444 |
* Regardless of what was in the symbol table before, after hash_jam() |
| 445 |
* the named symbol has the given value. The symbol is either inserted or |
| 446 |
* (its value is) relpaced. |
| 447 |
* An error message string is returned, 0 means OK. |
| 448 |
* |
| 449 |
* WARNING: this may decide to grow the hashed symbol table. |
| 450 |
* To do this, we call hash_grow(), WHICH WILL recursively CALL US. |
| 451 |
* |
| 452 |
* We report status internally: hash_found is TRUE if we replaced, but |
| 453 |
* false if we inserted. |
| 454 |
*/ |
| 455 |
const char * |
| 456 |
hash_jam (handle, string, value) |
| 457 |
struct hash_control *handle; |
| 458 |
const char *string; |
| 459 |
PTR value; |
| 460 |
{ |
| 461 |
const char *retval; |
| 462 |
struct hash_entry *entry; |
| 463 |
|
| 464 |
retval = 0; |
| 465 |
if (handle->hash_stat[STAT_USED] > handle->hash_full) |
| 466 |
{ |
| 467 |
retval = hash_grow (handle); |
| 468 |
} |
| 469 |
if (!retval) |
| 470 |
{ |
| 471 |
entry = hash_ask (handle, string, STAT__WRITE); |
| 472 |
if (!hash_found) |
| 473 |
{ |
| 474 |
entry->hash_string = string; |
| 475 |
handle->hash_stat[STAT_USED] += 1; |
| 476 |
} |
| 477 |
entry->hash_value = value; |
| 478 |
} |
| 479 |
return retval; |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* h a s h _ g r o w ( ) |
| 484 |
* |
| 485 |
* Grow a new (bigger) hash table from the old one. |
| 486 |
* We choose to double the hash table's size. |
| 487 |
* Return a human-scrutible error string: 0 if OK. |
| 488 |
* Warning! This uses hash_jam(), which had better not recurse |
| 489 |
* back here! Hash_jam() conditionally calls us, but we ALWAYS |
| 490 |
* call hash_jam()! |
| 491 |
* Internal. |
| 492 |
*/ |
| 493 |
static const char * |
| 494 |
hash_grow (handle) /* make a hash table grow */ |
| 495 |
struct hash_control *handle; |
| 496 |
{ |
| 497 |
struct hash_entry *newwall; |
| 498 |
struct hash_entry *newwhere; |
| 499 |
struct hash_entry *newtrack; |
| 500 |
struct hash_entry *oldtrack; |
| 501 |
struct hash_entry *oldwhere; |
| 502 |
struct hash_entry *oldwall; |
| 503 |
int temp; |
| 504 |
int newsize; |
| 505 |
const char *string; |
| 506 |
const char *retval; |
| 507 |
#ifdef SUSPECT |
| 508 |
int oldused; |
| 509 |
#endif |
| 510 |
|
| 511 |
/* |
| 512 |
* capture info about old hash table |
| 513 |
*/ |
| 514 |
oldwhere = handle->hash_where; |
| 515 |
oldwall = handle->hash_wall; |
| 516 |
#ifdef SUSPECT |
| 517 |
oldused = handle->hash_stat[STAT_USED]; |
| 518 |
#endif |
| 519 |
/* |
| 520 |
* attempt to get enough room for a hash table twice as big |
| 521 |
*/ |
| 522 |
temp = handle->hash_stat[STAT_SIZE]; |
| 523 |
if ((newwhere = ((struct hash_entry *) |
| 524 |
xmalloc ((unsigned long) ((temp + temp + 1) |
| 525 |
* sizeof (struct hash_entry))))) |
| 526 |
!= NULL) |
| 527 |
/* +1 for wall slot */ |
| 528 |
{ |
| 529 |
retval = 0; /* assume success until proven otherwise */ |
| 530 |
/* |
| 531 |
* have enough room: now we do all the work. |
| 532 |
* double the size of everything in handle, |
| 533 |
* note: hash_mask frob works for 1's & for 2's complement machines |
| 534 |
*/ |
| 535 |
handle->hash_mask = handle->hash_mask + handle->hash_mask + 1; |
| 536 |
handle->hash_stat[STAT_SIZE] <<= 1; |
| 537 |
newsize = handle->hash_stat[STAT_SIZE]; |
| 538 |
handle->hash_where = newwhere; |
| 539 |
handle->hash_full <<= 1; |
| 540 |
handle->hash_sizelog += 1; |
| 541 |
handle->hash_stat[STAT_USED] = 0; |
| 542 |
handle->hash_wall = |
| 543 |
newwall = newwhere + newsize; |
| 544 |
/* |
| 545 |
* set all those pesky new slots to vacant. |
| 546 |
*/ |
| 547 |
for (newtrack = newwhere; newtrack <= newwall; newtrack++) |
| 548 |
{ |
| 549 |
newtrack->hash_string = NULL; |
| 550 |
} |
| 551 |
/* |
| 552 |
* we will do a scan of the old table, the hard way, using the |
| 553 |
* new control block to re-insert the data into new hash table. |
| 554 |
*/ |
| 555 |
handle->hash_stat[STAT_USED] = 0; /* inserts will bump it up to correct */ |
| 556 |
for (oldtrack = oldwhere; oldtrack < oldwall; oldtrack++) |
| 557 |
if (((string = oldtrack->hash_string) != NULL) && string != DELETED) |
| 558 |
if ((retval = hash_jam (handle, string, oldtrack->hash_value))) |
| 559 |
break; |
| 560 |
|
| 561 |
#ifdef SUSPECT |
| 562 |
if (!retval && handle->hash_stat[STAT_USED] != oldused) |
| 563 |
{ |
| 564 |
retval = "hash_used"; |
| 565 |
} |
| 566 |
#endif |
| 567 |
if (!retval) |
| 568 |
{ |
| 569 |
/* |
| 570 |
* we have a completely faked up control block. |
| 571 |
* return the old hash table. |
| 572 |
*/ |
| 573 |
free ((char *) oldwhere); |
| 574 |
/* |
| 575 |
* Here with success. retval is already 0. |
| 576 |
*/ |
| 577 |
} |
| 578 |
} |
| 579 |
else |
| 580 |
{ |
| 581 |
retval = "no room"; |
| 582 |
} |
| 583 |
return retval; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* h a s h _ a p p l y ( ) |
| 588 |
* |
| 589 |
* Use this to scan each entry in symbol table. |
| 590 |
* For each symbol, this calls (applys) a nominated function supplying the |
| 591 |
* symbol's value (and the symbol's name). |
| 592 |
* The idea is you use this to destroy whatever is associted with |
| 593 |
* any values in the table BEFORE you destroy the table with hash_die. |
| 594 |
* Of course, you can use it for other jobs; whenever you need to |
| 595 |
* visit all extant symbols in the table. |
| 596 |
* |
| 597 |
* We choose to have a call-you-back idea for two reasons: |
| 598 |
* asthetic: it is a neater idea to use apply than an explicit loop |
| 599 |
* sensible: if we ever had to grow the symbol table (due to insertions) |
| 600 |
* then we would lose our place in the table when we re-hashed |
| 601 |
* symbols into the new table in a different order. |
| 602 |
* |
| 603 |
* The order symbols are visited depends entirely on the hashing function. |
| 604 |
* Whenever you insert a (symbol, value) you risk expanding the table. If |
| 605 |
* you do expand the table, then the hashing function WILL change, so you |
| 606 |
* MIGHT get a different order of symbols visited. In other words, if you |
| 607 |
* want the same order of visiting symbols as the last time you used |
| 608 |
* hash_apply() then you better not have done any hash_insert()s or |
| 609 |
* hash_jam()s since the last time you used hash_apply(). |
| 610 |
* |
| 611 |
* In future we may use the value returned by your nominated function. |
| 612 |
* One idea is to abort the scan if, after applying the function to a |
| 613 |
* certain node, the function returns a certain code. |
| 614 |
* To be safe, please make your functions of type char *. If you always |
| 615 |
* return NULL, then the scan will complete, visiting every symbol in |
| 616 |
* the table exactly once. ALL OTHER RETURNED VALUES have no meaning yet! |
| 617 |
* Caveat Actor! |
| 618 |
* |
| 619 |
* The function you supply should be of the form: |
| 620 |
* char * myfunct(string,value) |
| 621 |
* char * string; |* the symbol's name *| |
| 622 |
* char * value; |* the symbol's value *| |
| 623 |
* { |
| 624 |
* |* ... *| |
| 625 |
* return(NULL); |
| 626 |
* } |
| 627 |
* |
| 628 |
* The returned value of hash_apply() is (char*)NULL. In future it may return |
| 629 |
* other values. NULL means "completed scan OK". Other values have no meaning |
| 630 |
* yet. (The function has no graceful failures.) |
| 631 |
*/ |
| 632 |
char * |
| 633 |
hash_apply (handle, function) |
| 634 |
struct hash_control *handle; |
| 635 |
char *(*function) (); |
| 636 |
{ |
| 637 |
struct hash_entry *entry; |
| 638 |
struct hash_entry *wall; |
| 639 |
|
| 640 |
wall = handle->hash_wall; |
| 641 |
for (entry = handle->hash_where; entry < wall; entry++) |
| 642 |
{ |
| 643 |
if (islive (entry)) /* silly code: tests entry->string twice! */ |
| 644 |
{ |
| 645 |
(*function) (entry->hash_string, entry->hash_value); |
| 646 |
} |
| 647 |
} |
| 648 |
return (NULL); |
| 649 |
} |
| 650 |
|
| 651 |
/* |
| 652 |
* h a s h _ f i n d ( ) |
| 653 |
* |
| 654 |
* Given symbol string, find value (if any). |
| 655 |
* Return found value or NULL. |
| 656 |
*/ |
| 657 |
PTR |
| 658 |
hash_find (handle, string) |
| 659 |
struct hash_control *handle; |
| 660 |
const char *string; |
| 661 |
{ |
| 662 |
struct hash_entry *entry; |
| 663 |
|
| 664 |
entry = hash_ask (handle, string, STAT__READ); |
| 665 |
if (hash_found) |
| 666 |
return entry->hash_value; |
| 667 |
else |
| 668 |
return NULL; |
| 669 |
} |
| 670 |
|
| 671 |
/* |
| 672 |
* h a s h _ a s k ( ) |
| 673 |
* |
| 674 |
* Searches for given symbol string. |
| 675 |
* Return the slot where it OUGHT to live. It may be there. |
| 676 |
* Return hash_found: TRUE only if symbol is in that slot. |
| 677 |
* Access argument is to help keep statistics in control block. |
| 678 |
* Internal. |
| 679 |
*/ |
| 680 |
static struct hash_entry * /* string slot, may be empty or deleted */ |
| 681 |
hash_ask (handle, string, access) |
| 682 |
struct hash_control *handle; |
| 683 |
const char *string; |
| 684 |
int access; /* access type */ |
| 685 |
{ |
| 686 |
const char *string1; /* JF avoid strcmp calls */ |
| 687 |
const char *s; |
| 688 |
int c; |
| 689 |
struct hash_entry *slot; |
| 690 |
int collision; /* count collisions */ |
| 691 |
|
| 692 |
/* start looking here */ |
| 693 |
slot = handle->hash_where + hash_code (handle, string); |
| 694 |
|
| 695 |
handle->hash_stat[STAT_ACCESS + access] += 1; |
| 696 |
collision = 0; |
| 697 |
hash_found = FALSE; |
| 698 |
while (((s = slot->hash_string) != NULL) && s != DELETED) |
| 699 |
{ |
| 700 |
for (string1 = string;;) |
| 701 |
{ |
| 702 |
if ((c = *s++) == 0) |
| 703 |
{ |
| 704 |
if (!*string1) |
| 705 |
hash_found = TRUE; |
| 706 |
break; |
| 707 |
} |
| 708 |
if (*string1++ != c) |
| 709 |
break; |
| 710 |
} |
| 711 |
if (hash_found) |
| 712 |
break; |
| 713 |
collision++; |
| 714 |
slot++; |
| 715 |
} |
| 716 |
/* |
| 717 |
* slot: return: |
| 718 |
* in use: we found string slot |
| 719 |
* at empty: |
| 720 |
* at wall: we fell off: wrap round ???? |
| 721 |
* in table: dig here slot |
| 722 |
* at DELETED: dig here slot |
| 723 |
*/ |
| 724 |
if (slot == handle->hash_wall) |
| 725 |
{ |
| 726 |
slot = handle->hash_where;/* now look again */ |
| 727 |
while (((s = slot->hash_string) != NULL) && s != DELETED) |
| 728 |
{ |
| 729 |
for (string1 = string; *s; string1++, s++) |
| 730 |
{ |
| 731 |
if (*string1 != *s) |
| 732 |
break; |
| 733 |
} |
| 734 |
if (*s == *string1) |
| 735 |
{ |
| 736 |
hash_found = TRUE; |
| 737 |
break; |
| 738 |
} |
| 739 |
collision++; |
| 740 |
slot++; |
| 741 |
} |
| 742 |
/* |
| 743 |
* slot: return: |
| 744 |
* in use: we found it slot |
| 745 |
* empty: wall: ERROR IMPOSSIBLE !!!! |
| 746 |
* in table: dig here slot |
| 747 |
* DELETED:dig here slot |
| 748 |
*/ |
| 749 |
} |
| 750 |
handle->hash_stat[STAT_COLLIDE + access] += collision; |
| 751 |
return (slot); /* also return hash_found */ |
| 752 |
} |
| 753 |
|
| 754 |
/* |
| 755 |
* h a s h _ c o d e |
| 756 |
* |
| 757 |
* Does hashing of symbol string to hash number. |
| 758 |
* Internal. |
| 759 |
*/ |
| 760 |
static int |
| 761 |
hash_code (handle, string) |
| 762 |
struct hash_control *handle; |
| 763 |
const char *string; |
| 764 |
{ |
| 765 |
long h; /* hash code built here */ |
| 766 |
long c; /* each character lands here */ |
| 767 |
int n; /* Amount to shift h by */ |
| 768 |
|
| 769 |
n = (handle->hash_sizelog - 3); |
| 770 |
h = 0; |
| 771 |
while ((c = *string++) != 0) |
| 772 |
{ |
| 773 |
h += c; |
| 774 |
h = (h << 3) + (h >> n) + c; |
| 775 |
} |
| 776 |
return (h & handle->hash_mask); |
| 777 |
} |
| 778 |
|
| 779 |
/* |
| 780 |
* Here is a test program to exercise above. |
| 781 |
*/ |
| 782 |
#ifdef TEST |
| 783 |
|
| 784 |
#define TABLES (6) /* number of hash tables to maintain */ |
| 785 |
/* (at once) in any testing */ |
| 786 |
#define STATBUFSIZE (12) /* we can have 12 statistics */ |
| 787 |
|
| 788 |
int statbuf[STATBUFSIZE]; /* display statistics here */ |
| 789 |
char answer[100]; /* human farts here */ |
| 790 |
char *hashtable[TABLES]; /* we test many hash tables at once */ |
| 791 |
char *h; /* points to curent hash_control */ |
| 792 |
char **pp; |
| 793 |
char *p; |
| 794 |
char *name; |
| 795 |
char *value; |
| 796 |
int size; |
| 797 |
int used; |
| 798 |
char command; |
| 799 |
int number; /* number 0:TABLES-1 of current hashed */ |
| 800 |
/* symbol table */ |
| 801 |
|
| 802 |
main () |
| 803 |
{ |
| 804 |
char (*applicatee ()); |
| 805 |
char *destroy (); |
| 806 |
char *what (); |
| 807 |
int *ip; |
| 808 |
|
| 809 |
number = 0; |
| 810 |
h = 0; |
| 811 |
printf ("type h <RETURN> for help\n"); |
| 812 |
for (;;) |
| 813 |
{ |
| 814 |
printf ("hash_test command: "); |
| 815 |
gets (answer); |
| 816 |
command = answer[0]; |
| 817 |
if (isupper (command)) |
| 818 |
command = tolower (command); /* ecch! */ |
| 819 |
switch (command) |
| 820 |
{ |
| 821 |
case '#': |
| 822 |
printf ("old hash table #=%d.\n", number); |
| 823 |
whattable (); |
| 824 |
break; |
| 825 |
case '?': |
| 826 |
for (pp = hashtable; pp < hashtable + TABLES; pp++) |
| 827 |
{ |
| 828 |
printf ("address of hash table #%d control block is %xx\n" |
| 829 |
,pp - hashtable, *pp); |
| 830 |
} |
| 831 |
break; |
| 832 |
case 'a': |
| 833 |
hash_apply (h, applicatee); |
| 834 |
break; |
| 835 |
case 'd': |
| 836 |
hash_apply (h, destroy); |
| 837 |
hash_die (h); |
| 838 |
break; |
| 839 |
case 'f': |
| 840 |
p = hash_find (h, name = what ("symbol")); |
| 841 |
printf ("value of \"%s\" is \"%s\"\n", name, p ? p : "NOT-PRESENT"); |
| 842 |
break; |
| 843 |
case 'h': |
| 844 |
printf ("# show old, select new default hash table number\n"); |
| 845 |
printf ("? display all hashtable control block addresses\n"); |
| 846 |
printf ("a apply a simple display-er to each symbol in table\n"); |
| 847 |
printf ("d die: destroy hashtable\n"); |
| 848 |
printf ("f find value of nominated symbol\n"); |
| 849 |
printf ("h this help\n"); |
| 850 |
printf ("i insert value into symbol\n"); |
| 851 |
printf ("j jam value into symbol\n"); |
| 852 |
printf ("n new hashtable\n"); |
| 853 |
printf ("r replace a value with another\n"); |
| 854 |
printf ("s say what %% of table is used\n"); |
| 855 |
printf ("q exit this program\n"); |
| 856 |
printf ("x delete a symbol from table, report its value\n"); |
| 857 |
break; |
| 858 |
case 'i': |
| 859 |
p = hash_insert (h, name = what ("symbol"), value = what ("value")); |
| 860 |
if (p) |
| 861 |
{ |
| 862 |
printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, |
| 863 |
p); |
| 864 |
} |
| 865 |
break; |
| 866 |
case 'j': |
| 867 |
p = hash_jam (h, name = what ("symbol"), value = what ("value")); |
| 868 |
if (p) |
| 869 |
{ |
| 870 |
printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, p); |
| 871 |
} |
| 872 |
break; |
| 873 |
case 'n': |
| 874 |
h = hashtable[number] = (char *) hash_new (); |
| 875 |
break; |
| 876 |
case 'q': |
| 877 |
exit (); |
| 878 |
case 'r': |
| 879 |
p = hash_replace (h, name = what ("symbol"), value = what ("value")); |
| 880 |
printf ("old value was \"%s\"\n", p ? p : "{}"); |
| 881 |
break; |
| 882 |
case 's': |
| 883 |
hash_say (h, statbuf, STATBUFSIZE); |
| 884 |
for (ip = statbuf; ip < statbuf + STATBUFSIZE; ip++) |
| 885 |
{ |
| 886 |
printf ("%d ", *ip); |
| 887 |
} |
| 888 |
printf ("\n"); |
| 889 |
break; |
| 890 |
case 'x': |
| 891 |
p = hash_delete (h, name = what ("symbol")); |
| 892 |
printf ("old value was \"%s\"\n", p ? p : "{}"); |
| 893 |
break; |
| 894 |
default: |
| 895 |
printf ("I can't understand command \"%c\"\n", command); |
| 896 |
break; |
| 897 |
} |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
char * |
| 902 |
what (description) |
| 903 |
char *description; |
| 904 |
{ |
| 905 |
char *retval; |
| 906 |
char *malloc (); |
| 907 |
|
| 908 |
printf (" %s : ", description); |
| 909 |
gets (answer); |
| 910 |
/* will one day clean up answer here */ |
| 911 |
retval = malloc (strlen (answer) + 1); |
| 912 |
if (!retval) |
| 913 |
{ |
| 914 |
error ("room"); |
| 915 |
} |
| 916 |
(void) strcpy (retval, answer); |
| 917 |
return (retval); |
| 918 |
} |
| 919 |
|
| 920 |
char * |
| 921 |
destroy (string, value) |
| 922 |
char *string; |
| 923 |
char *value; |
| 924 |
{ |
| 925 |
free (string); |
| 926 |
free (value); |
| 927 |
return (NULL); |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
char * |
| 932 |
applicatee (string, value) |
| 933 |
char *string; |
| 934 |
char *value; |
| 935 |
{ |
| 936 |
printf ("%.20s-%.20s\n", string, value); |
| 937 |
return (NULL); |
| 938 |
} |
| 939 |
|
| 940 |
whattable () /* determine number: what hash table to use */ |
| 941 |
/* also determine h: points to hash_control */ |
| 942 |
{ |
| 943 |
|
| 944 |
for (;;) |
| 945 |
{ |
| 946 |
printf (" what hash table (%d:%d) ? ", 0, TABLES - 1); |
| 947 |
gets (answer); |
| 948 |
sscanf (answer, "%d", &number); |
| 949 |
if (number >= 0 && number < TABLES) |
| 950 |
{ |
| 951 |
h = hashtable[number]; |
| 952 |
if (!h) |
| 953 |
{ |
| 954 |
printf ("warning: current hash-table-#%d. has no hash-control\n", number); |
| 955 |
} |
| 956 |
return; |
| 957 |
} |
| 958 |
else |
| 959 |
{ |
| 960 |
printf ("invalid hash table number: %d\n", number); |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
#endif /* #ifdef TEST */ |
| 968 |
|
| 969 |
/* $Id: hash.c,v 1.2 1998/08/13 06:13:29 boyns Exp $ */ |