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