1 Debian patch touch this file
-
make it compile with gcc15
| 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 (struct hash_control *handle) |
| 293 | { |
| 294 | free ((char *) handle->hash_where); |
| 295 | free ((char *) handle); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * h a s h _ s a y ( ) |
| 300 | * |
| 301 | * Return the size of the statistics table, and as many statistics as |
| 302 | * we can until either (a) we have run out of statistics or (b) caller |
| 303 | * has run out of buffer. |
| 304 | * NOTE: hash_say treats all statistics alike. |
| 305 | * These numbers may change with time, due to insertions, deletions |
| 306 | * and expansions of the table. |
| 307 | * The first "statistic" returned is the length of hash_stat[]. |
| 308 | * Then contents of hash_stat[] are read out (in ascending order) |
| 309 | * until your buffer or hash_stat[] is exausted. |
| 310 | */ |
| 311 | void |
| •312 | hash_say (struct hash_control *handle, int buffer[ /*bufsiz*/ ], int bufsiz) |
| 313 | { |
| 314 | int *nd; /* limit of statistics block */ |
| 315 | int *ip; /* scan statistics */ |
| 316 | |
| 317 | ip = handle->hash_stat; |
| 318 | nd = ip + min (bufsiz - 1, STATLENGTH); |
| 319 | if (bufsiz > 0) /* trust nothing! bufsiz<=0 is dangerous */ |
| 320 | { |
| 321 | *buffer++ = STATLENGTH; |
| 322 | for (; ip < nd; ip++, buffer++) |
| 323 | { |
| 324 | *buffer = *ip; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * h a s h _ d e l e t e ( ) |
| 331 | * |
| 332 | * Try to delete a symbol from the table. |
| 333 | * If it was there, return its value (and adjust STAT_USED). |
| 334 | * Otherwise, return NULL. |
| 335 | * Anyway, the symbol is not present after this function. |
| 336 | * |
| 337 | */ |
| 338 | PTR /* NULL if string not in table, else */ |
| 339 | /* returns value of deleted symbol */ |
| •340 | hash_delete (struct hash_control *handle, const char *string) |
| 341 | { |
| 342 | PTR retval; |
| 343 | struct hash_entry *entry; |
| 344 | |
| 345 | entry = hash_ask (handle, string, STAT__WRITE); |
| 346 | if (hash_found) |
| 347 | { |
| 348 | retval = entry->hash_value; |
| 349 | entry->hash_string = DELETED; |
| 350 | handle->hash_stat[STAT_USED] -= 1; |
| 351 | #ifdef SUSPECT |
| 352 | if (handle->hash_stat[STAT_USED] < 0) |
| 353 | { |
| 354 | error ("hash_delete"); |
| 355 | } |
| 356 | #endif /* def SUSPECT */ |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | retval = NULL; |
| 361 | } |
| 362 | return (retval); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * h a s h _ r e p l a c e ( ) |
| 367 | * |
| 368 | * Try to replace the old value of a symbol with a new value. |
| 369 | * Normally return the old value. |
| 370 | * Return NULL and don't change the table if the symbol is not already |
| 371 | * in the table. |
| 372 | */ |
| 373 | PTR |
| •374 | hash_replace (struct hash_control *handle, const char *string, PTR value) |
| 375 | { |
| 376 | struct hash_entry *entry; |
| 377 | char *retval; |
| 378 | |
| 379 | entry = hash_ask (handle, string, STAT__WRITE); |
| 380 | if (hash_found) |
| 381 | { |
| 382 | retval = entry->hash_value; |
| 383 | entry->hash_value = value; |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | retval = NULL; |
| 388 | } |
| 389 | ; |
| 390 | return retval; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * h a s h _ i n s e r t ( ) |
| 395 | * |
| 396 | * Insert a (symbol-string, value) into the hash table. |
| 397 | * Return an error string, 0 means OK. |
| 398 | * It is an 'error' to insert an existing symbol. |
| 399 | */ |
| 400 | |
| 401 | const char * /* return error string */ |
| •402 | hash_insert (struct hash_control *handle, const char *string, PTR value) |
| 403 | { |
| 404 | struct hash_entry *entry; |
| 405 | const char *retval; |
| 406 | |
| 407 | retval = 0; |
| 408 | if (handle->hash_stat[STAT_USED] > handle->hash_full) |
| 409 | { |
| 410 | retval = hash_grow (handle); |
| 411 | } |
| 412 | if (!retval) |
| 413 | { |
| 414 | entry = hash_ask (handle, string, STAT__WRITE); |
| 415 | if (hash_found) |
| 416 | { |
| 417 | retval = "exists"; |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | entry->hash_value = value; |
| 422 | entry->hash_string = string; |
| 423 | handle->hash_stat[STAT_USED] += 1; |
| 424 | } |
| 425 | } |
| 426 | return retval; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * h a s h _ j a m ( ) |
| 431 | * |
| 432 | * Regardless of what was in the symbol table before, after hash_jam() |
| 433 | * the named symbol has the given value. The symbol is either inserted or |
| 434 | * (its value is) relpaced. |
| 435 | * An error message string is returned, 0 means OK. |
| 436 | * |
| 437 | * WARNING: this may decide to grow the hashed symbol table. |
| 438 | * To do this, we call hash_grow(), WHICH WILL recursively CALL US. |
| 439 | * |
| 440 | * We report status internally: hash_found is TRUE if we replaced, but |
| 441 | * false if we inserted. |
| 442 | */ |
| 443 | const char * |
| •444 | hash_jam (struct hash_control *handle, const char *string, PTR value) |
| 445 | { |
| 446 | const char *retval; |
| 447 | struct hash_entry *entry; |
| 448 | |
| 449 | retval = 0; |
| 450 | if (handle->hash_stat[STAT_USED] > handle->hash_full) |
| 451 | { |
| 452 | retval = hash_grow (handle); |
| 453 | } |
| 454 | if (!retval) |
| 455 | { |
| 456 | entry = hash_ask (handle, string, STAT__WRITE); |
| 457 | if (!hash_found) |
| 458 | { |
| 459 | entry->hash_string = string; |
| 460 | handle->hash_stat[STAT_USED] += 1; |
| 461 | } |
| 462 | entry->hash_value = value; |
| 463 | } |
| 464 | return retval; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * h a s h _ g r o w ( ) |
| 469 | * |
| 470 | * Grow a new (bigger) hash table from the old one. |
| 471 | * We choose to double the hash table's size. |
| 472 | * Return a human-scrutible error string: 0 if OK. |
| 473 | * Warning! This uses hash_jam(), which had better not recurse |
| 474 | * back here! Hash_jam() conditionally calls us, but we ALWAYS |
| 475 | * call hash_jam()! |
| 476 | * Internal. |
| 477 | */ |
| 478 | static const char * |
| •479 | hash_grow (struct hash_control *handle) /* make a hash table grow */ |
| 480 | { |
| 481 | struct hash_entry *newwall; |
| 482 | struct hash_entry *newwhere; |
| 483 | struct hash_entry *newtrack; |
| 484 | struct hash_entry *oldtrack; |
| 485 | struct hash_entry *oldwhere; |
| 486 | struct hash_entry *oldwall; |
| 487 | int temp; |
| 488 | int newsize; |
| 489 | const char *string; |
| 490 | const char *retval; |
| 491 | #ifdef SUSPECT |
| 492 | int oldused; |
| 493 | #endif |
| 494 | |
| 495 | /* |
| 496 | * capture info about old hash table |
| 497 | */ |
| 498 | oldwhere = handle->hash_where; |
| 499 | oldwall = handle->hash_wall; |
| 500 | #ifdef SUSPECT |
| 501 | oldused = handle->hash_stat[STAT_USED]; |
| 502 | #endif |
| 503 | /* |
| 504 | * attempt to get enough room for a hash table twice as big |
| 505 | */ |
| 506 | temp = handle->hash_stat[STAT_SIZE]; |
| 507 | if ((newwhere = ((struct hash_entry *) |
| 508 | xmalloc ((unsigned long) ((temp + temp + 1) |
| 509 | * sizeof (struct hash_entry))))) |
| 510 | != NULL) |
| 511 | /* +1 for wall slot */ |
| 512 | { |
| 513 | retval = 0; /* assume success until proven otherwise */ |
| 514 | /* |
| 515 | * have enough room: now we do all the work. |
| 516 | * double the size of everything in handle, |
| 517 | * note: hash_mask frob works for 1's & for 2's complement machines |
| 518 | */ |
| 519 | handle->hash_mask = handle->hash_mask + handle->hash_mask + 1; |
| 520 | handle->hash_stat[STAT_SIZE] <<= 1; |
| 521 | newsize = handle->hash_stat[STAT_SIZE]; |
| 522 | handle->hash_where = newwhere; |
| 523 | handle->hash_full <<= 1; |
| 524 | handle->hash_sizelog += 1; |
| 525 | handle->hash_stat[STAT_USED] = 0; |
| 526 | handle->hash_wall = |
| 527 | newwall = newwhere + newsize; |
| 528 | /* |
| 529 | * set all those pesky new slots to vacant. |
| 530 | */ |
| 531 | for (newtrack = newwhere; newtrack <= newwall; newtrack++) |
| 532 | { |
| 533 | newtrack->hash_string = NULL; |
| 534 | } |
| 535 | /* |
| 536 | * we will do a scan of the old table, the hard way, using the |
| 537 | * new control block to re-insert the data into new hash table. |
| 538 | */ |
| 539 | handle->hash_stat[STAT_USED] = 0; /* inserts will bump it up to correct */ |
| 540 | for (oldtrack = oldwhere; oldtrack < oldwall; oldtrack++) |
| 541 | if (((string = oldtrack->hash_string) != NULL) && string != DELETED) |
| 542 | if ((retval = hash_jam (handle, string, oldtrack->hash_value))) |
| 543 | break; |
| 544 | |
| 545 | #ifdef SUSPECT |
| 546 | if (!retval && handle->hash_stat[STAT_USED] != oldused) |
| 547 | { |
| 548 | retval = "hash_used"; |
| 549 | } |
| 550 | #endif |
| 551 | if (!retval) |
| 552 | { |
| 553 | /* |
| 554 | * we have a completely faked up control block. |
| 555 | * return the old hash table. |
| 556 | */ |
| 557 | free ((char *) oldwhere); |
| 558 | /* |
| 559 | * Here with success. retval is already 0. |
| 560 | */ |
| 561 | } |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | retval = "no room"; |
| 566 | } |
| 567 | return retval; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * h a s h _ a p p l y ( ) |
| 572 | * |
| 573 | * Use this to scan each entry in symbol table. |
| 574 | * For each symbol, this calls (applys) a nominated function supplying the |
| 575 | * symbol's value (and the symbol's name). |
| 576 | * The idea is you use this to destroy whatever is associted with |
| 577 | * any values in the table BEFORE you destroy the table with hash_die. |
| 578 | * Of course, you can use it for other jobs; whenever you need to |
| 579 | * visit all extant symbols in the table. |
| 580 | * |
| 581 | * We choose to have a call-you-back idea for two reasons: |
| 582 | * asthetic: it is a neater idea to use apply than an explicit loop |
| 583 | * sensible: if we ever had to grow the symbol table (due to insertions) |
| 584 | * then we would lose our place in the table when we re-hashed |
| 585 | * symbols into the new table in a different order. |
| 586 | * |
| 587 | * The order symbols are visited depends entirely on the hashing function. |
| 588 | * Whenever you insert a (symbol, value) you risk expanding the table. If |
| 589 | * you do expand the table, then the hashing function WILL change, so you |
| 590 | * MIGHT get a different order of symbols visited. In other words, if you |
| 591 | * want the same order of visiting symbols as the last time you used |
| 592 | * hash_apply() then you better not have done any hash_insert()s or |
| 593 | * hash_jam()s since the last time you used hash_apply(). |
| 594 | * |
| 595 | * In future we may use the value returned by your nominated function. |
| 596 | * One idea is to abort the scan if, after applying the function to a |
| 597 | * certain node, the function returns a certain code. |
| 598 | * To be safe, please make your functions of type char *. If you always |
| 599 | * return NULL, then the scan will complete, visiting every symbol in |
| 600 | * the table exactly once. ALL OTHER RETURNED VALUES have no meaning yet! |
| 601 | * Caveat Actor! |
| 602 | * |
| 603 | * The function you supply should be of the form: |
| 604 | * char * myfunct(string,value) |
| 605 | * char * string; |* the symbol's name *| |
| 606 | * char * value; |* the symbol's value *| |
| 607 | * { |
| 608 | * |* ... *| |
| 609 | * return(NULL); |
| 610 | * } |
| 611 | * |
| 612 | * The returned value of hash_apply() is (char*)NULL. In future it may return |
| 613 | * other values. NULL means "completed scan OK". Other values have no meaning |
| 614 | * yet. (The function has no graceful failures.) |
| 615 | */ |
| 616 | char * |
| •617 | hash_apply (struct hash_control *handle, char *(*function)(const char *, PTR )) |
| 618 | { |
| 619 | struct hash_entry *entry; |
| 620 | struct hash_entry *wall; |
| 621 | |
| 622 | wall = handle->hash_wall; |
| 623 | for (entry = handle->hash_where; entry < wall; entry++) |
| 624 | { |
| 625 | if (islive (entry)) /* silly code: tests entry->string twice! */ |
| 626 | { |
| 627 | (*function) (entry->hash_string, entry->hash_value); |
| 628 | } |
| 629 | } |
| 630 | return (NULL); |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * h a s h _ f i n d ( ) |
| 635 | * |
| 636 | * Given symbol string, find value (if any). |
| 637 | * Return found value or NULL. |
| 638 | */ |
| 639 | PTR |
| •640 | hash_find (struct hash_control *handle, const char *string) |
| 641 | { |
| 642 | struct hash_entry *entry; |
| 643 | |
| 644 | entry = hash_ask (handle, string, STAT__READ); |
| 645 | if (hash_found) |
| 646 | return entry->hash_value; |
| 647 | else |
| 648 | return NULL; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * h a s h _ a s k ( ) |
| 653 | * |
| 654 | * Searches for given symbol string. |
| 655 | * Return the slot where it OUGHT to live. It may be there. |
| 656 | * Return hash_found: TRUE only if symbol is in that slot. |
| 657 | * Access argument is to help keep statistics in control block. |
| 658 | * Internal. |
| 659 | */ |
| 660 | static struct hash_entry * /* string slot, may be empty or deleted */ |
| •661 | hash_ask (struct hash_control *handle, const char *string, int access) |
| 662 | // struct hash_control *handle; |
| 663 | // const char *string; |
| 664 | // int access; /* access type */ |
| 665 | { |
| 666 | const char *string1; /* JF avoid strcmp calls */ |
| 667 | const char *s; |
| 668 | int c; |
| 669 | struct hash_entry *slot; |
| 670 | int collision; /* count collisions */ |
| 671 | |
| 672 | /* start looking here */ |
| 673 | slot = handle->hash_where + hash_code (handle, string); |
| 674 | |
| 675 | handle->hash_stat[STAT_ACCESS + access] += 1; |
| 676 | collision = 0; |
| 677 | hash_found = FALSE; |
| 678 | while (((s = slot->hash_string) != NULL) && s != DELETED) |
| 679 | { |
| 680 | for (string1 = string;;) |
| 681 | { |
| 682 | if ((c = *s++) == 0) |
| 683 | { |
| 684 | if (!*string1) |
| 685 | hash_found = TRUE; |
| 686 | break; |
| 687 | } |
| 688 | if (*string1++ != c) |
| 689 | break; |
| 690 | } |
| 691 | if (hash_found) |
| 692 | break; |
| 693 | collision++; |
| 694 | slot++; |
| 695 | } |
| 696 | /* |
| 697 | * slot: return: |
| 698 | * in use: we found string slot |
| 699 | * at empty: |
| 700 | * at wall: we fell off: wrap round ???? |
| 701 | * in table: dig here slot |
| 702 | * at DELETED: dig here slot |
| 703 | */ |
| 704 | if (slot == handle->hash_wall) |
| 705 | { |
| 706 | slot = handle->hash_where;/* now look again */ |
| 707 | while (((s = slot->hash_string) != NULL) && s != DELETED) |
| 708 | { |
| 709 | for (string1 = string; *s; string1++, s++) |
| 710 | { |
| 711 | if (*string1 != *s) |
| 712 | break; |
| 713 | } |
| 714 | if (*s == *string1) |
| 715 | { |
| 716 | hash_found = TRUE; |
| 717 | break; |
| 718 | } |
| 719 | collision++; |
| 720 | slot++; |
| 721 | } |
| 722 | /* |
| 723 | * slot: return: |
| 724 | * in use: we found it slot |
| 725 | * empty: wall: ERROR IMPOSSIBLE !!!! |
| 726 | * in table: dig here slot |
| 727 | * DELETED:dig here slot |
| 728 | */ |
| 729 | } |
| 730 | handle->hash_stat[STAT_COLLIDE + access] += collision; |
| 731 | return (slot); /* also return hash_found */ |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * h a s h _ c o d e |
| 736 | * |
| 737 | * Does hashing of symbol string to hash number. |
| 738 | * Internal. |
| 739 | */ |
| 740 | static int |
| •741 | hash_code (struct hash_control *handle, const char *string) |
| 742 | { |
| 743 | long h; /* hash code built here */ |
| 744 | long c; /* each character lands here */ |
| 745 | int n; /* Amount to shift h by */ |
| 746 | |
| 747 | n = (handle->hash_sizelog - 3); |
| 748 | h = 0; |
| 749 | while ((c = *string++) != 0) |
| 750 | { |
| 751 | h += c; |
| 752 | h = (h << 3) + (h >> n) + c; |
| 753 | } |
| 754 | return (h & handle->hash_mask); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Here is a test program to exercise above. |
| 759 | */ |
| 760 | #ifdef TEST |
| 761 | |
| 762 | #define TABLES (6) /* number of hash tables to maintain */ |
| 763 | /* (at once) in any testing */ |
| 764 | #define STATBUFSIZE (12) /* we can have 12 statistics */ |
| 765 | |
| 766 | int statbuf[STATBUFSIZE]; /* display statistics here */ |
| 767 | char answer[100]; /* human farts here */ |
| 768 | char *hashtable[TABLES]; /* we test many hash tables at once */ |
| 769 | char *h; /* points to curent hash_control */ |
| 770 | char **pp; |
| 771 | char *p; |
| 772 | char *name; |
| 773 | char *value; |
| 774 | int size; |
| 775 | int used; |
| 776 | char command; |
| 777 | int number; /* number 0:TABLES-1 of current hashed */ |
| 778 | /* symbol table */ |
| 779 | |
| 780 | main () |
| 781 | { |
| 782 | char (*applicatee ()); |
| 783 | char *destroy (); |
| 784 | char *what (); |
| 785 | int *ip; |
| 786 | |
| 787 | number = 0; |
| 788 | h = 0; |
| 789 | printf ("type h <RETURN> for help\n"); |
| 790 | for (;;) |
| 791 | { |
| 792 | printf ("hash_test command: "); |
| 793 | gets (answer); |
| 794 | command = answer[0]; |
| 795 | if (isupper (command)) |
| 796 | command = tolower (command); /* ecch! */ |
| 797 | switch (command) |
| 798 | { |
| 799 | case '#': |
| 800 | printf ("old hash table #=%d.\n", number); |
| 801 | whattable (); |
| 802 | break; |
| 803 | case '?': |
| 804 | for (pp = hashtable; pp < hashtable + TABLES; pp++) |
| 805 | { |
| 806 | printf ("address of hash table #%d control block is %xx\n" |
| 807 | ,pp - hashtable, *pp); |
| 808 | } |
| 809 | break; |
| 810 | case 'a': |
| 811 | hash_apply (h, applicatee); |
| 812 | break; |
| 813 | case 'd': |
| 814 | hash_apply (h, destroy); |
| 815 | hash_die (h); |
| 816 | break; |
| 817 | case 'f': |
| 818 | p = hash_find (h, name = what ("symbol")); |
| 819 | printf ("value of \"%s\" is \"%s\"\n", name, p ? p : "NOT-PRESENT"); |
| 820 | break; |
| 821 | case 'h': |
| 822 | printf ("# show old, select new default hash table number\n"); |
| 823 | printf ("? display all hashtable control block addresses\n"); |
| 824 | printf ("a apply a simple display-er to each symbol in table\n"); |
| 825 | printf ("d die: destroy hashtable\n"); |
| 826 | printf ("f find value of nominated symbol\n"); |
| 827 | printf ("h this help\n"); |
| 828 | printf ("i insert value into symbol\n"); |
| 829 | printf ("j jam value into symbol\n"); |
| 830 | printf ("n new hashtable\n"); |
| 831 | printf ("r replace a value with another\n"); |
| 832 | printf ("s say what %% of table is used\n"); |
| 833 | printf ("q exit this program\n"); |
| 834 | printf ("x delete a symbol from table, report its value\n"); |
| 835 | break; |
| 836 | case 'i': |
| 837 | p = hash_insert (h, name = what ("symbol"), value = what ("value")); |
| 838 | if (p) |
| 839 | { |
| 840 | printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, |
| 841 | p); |
| 842 | } |
| 843 | break; |
| 844 | case 'j': |
| 845 | p = hash_jam (h, name = what ("symbol"), value = what ("value")); |
| 846 | if (p) |
| 847 | { |
| 848 | printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, p); |
| 849 | } |
| 850 | break; |
| 851 | case 'n': |
| 852 | h = hashtable[number] = (char *) hash_new (); |
| 853 | break; |
| 854 | case 'q': |
| 855 | exit (); |
| 856 | case 'r': |
| 857 | p = hash_replace (h, name = what ("symbol"), value = what ("value")); |
| 858 | printf ("old value was \"%s\"\n", p ? p : "{}"); |
| 859 | break; |
| 860 | case 's': |
| 861 | hash_say (h, statbuf, STATBUFSIZE); |
| 862 | for (ip = statbuf; ip < statbuf + STATBUFSIZE; ip++) |
| 863 | { |
| 864 | printf ("%d ", *ip); |
| 865 | } |
| 866 | printf ("\n"); |
| 867 | break; |
| 868 | case 'x': |
| 869 | p = hash_delete (h, name = what ("symbol")); |
| 870 | printf ("old value was \"%s\"\n", p ? p : "{}"); |
| 871 | break; |
| 872 | default: |
| 873 | printf ("I can't understand command \"%c\"\n", command); |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | char * |
| •880 | what (char *description) |
| 881 | { |
| 882 | char *retval; |
| 883 | char *malloc (); |
| 884 | |
| 885 | printf (" %s : ", description); |
| 886 | gets (answer); |
| 887 | /* will one day clean up answer here */ |
| 888 | retval = malloc (strlen (answer) + 1); |
| 889 | if (!retval) |
| 890 | { |
| 891 | error ("room"); |
| 892 | } |
| 893 | (void) strcpy (retval, answer); |
| 894 | return (retval); |
| 895 | } |
| 896 | |
| 897 | char * |
| •898 | destroy (char *string, char *value) |
| 899 | { |
| 900 | free (string); |
| 901 | free (value); |
| 902 | return (NULL); |
| 903 | } |
| 904 | |
| 905 | |
| 906 | char * |
| •907 | applicatee (char *string, char *value) |
| 908 | { |
| 909 | printf ("%.20s-%.20s\n", string, value); |
| 910 | return (NULL); |
| 911 | } |
| 912 | |
| 913 | whattable () /* determine number: what hash table to use */ |
| 914 | /* also determine h: points to hash_control */ |
| 915 | { |
| 916 | |
| 917 | for (;;) |
| 918 | { |
| 919 | printf (" what hash table (%d:%d) ? ", 0, TABLES - 1); |
| 920 | gets (answer); |
| 921 | sscanf (answer, "%d", &number); |
| 922 | if (number >= 0 && number < TABLES) |
| 923 | { |
| 924 | h = hashtable[number]; |
| 925 | if (!h) |
| 926 | { |
| 927 | printf ("warning: current hash-table-#%d. has no hash-control\n", number); |
| 928 | } |
| 929 | return; |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | printf ("invalid hash table number: %d\n", number); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | |
| 939 | |
| 940 | #endif /* #ifdef TEST */ |
| 941 | |
| 942 | /* $Id: hash.c,v 1.2 1998/08/13 06:13:29 boyns Exp $ */ |