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