home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MAWK113.ZIP / mawk113 / hash.c < prev    next >
C/C++ Source or Header  |  1991-12-04  |  4KB  |  227 lines

  1.  
  2. /********************************************
  3. hash.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /* $Log: hash.c,v $
  15.  * Revision 5.1  1991/12/05  07:56:05  brennan
  16.  * 1.1 pre-release
  17.  *
  18. */
  19.  
  20.  
  21. /* hash.c */
  22.  
  23. #include "mawk.h"
  24. #include "memory.h"
  25. #include "symtype.h"
  26.  
  27.  
  28. unsigned hash(s)
  29.   register char *s ;
  30. { register unsigned h = 0 ;
  31.  
  32.   while ( *s )  h += h + *s++ ;
  33.   return  h  ;
  34. }
  35.  
  36. typedef struct hash {
  37. struct hash *link ;
  38. SYMTAB  symtab ;
  39. }  HASHNODE ;
  40.  
  41. static  HASHNODE *PROTO( delete, (char *) ) ;
  42.  
  43. #define  new_HASHNODE() (HASHNODE *) zmalloc(sizeof(HASHNODE))
  44.  
  45. static HASHNODE *hash_table[HASH_PRIME] ;
  46.  
  47. /*
  48.  *   insert -- s is not there and need not be duplicated
  49.  *   -- used during initialization
  50.  */
  51.  
  52. SYMTAB *insert(s) 
  53.   char *s ;
  54. { register HASHNODE *p = new_HASHNODE();
  55.   register unsigned h ;
  56.   
  57.   p->link = hash_table[h = hash(s) % HASH_PRIME ] ;
  58.   p->symtab.name = s ;
  59.   hash_table[h] = p ;
  60.   return &p->symtab ;
  61. }
  62.  
  63. /*
  64.  *  find --  s might be there, find it else insert and dup
  65.  *  s 
  66.  */
  67.  
  68. SYMTAB *find(s)
  69.   char *s ;
  70. { register HASHNODE *p ;
  71.   HASHNODE *q ;
  72.   unsigned h ;
  73.  
  74.   p = hash_table[h = hash(s) % HASH_PRIME ] ;
  75.   q = (HASHNODE *) 0 ;
  76.   while ( 1 )
  77.   { if ( !p )
  78.     { p = new_HASHNODE() ;
  79.       p->symtab.type = ST_NONE ;
  80.       p->symtab.name = strcpy(zmalloc( strlen(s)+1 ), s) ;
  81.       break ;
  82.     }
  83.  
  84.     if ( strcmp(p->symtab.name, s) == 0 ) /* found */
  85.       if ( !q )  /* already at the front */
  86.         return  &p->symtab ;
  87.       else /* delete from the list */
  88.       { q->link = p->link ;  break ; }
  89.  
  90.     q = p ; p = p->link ;
  91.   }
  92.   /* put p on front of the list */
  93.   p->link = hash_table[h] ;
  94.   hash_table[h] = p ;
  95.   return & p->symtab ;
  96. }
  97.  
  98.  
  99. /* remove a node from the hash table
  100.    return a ptr to the node */
  101.  
  102. static unsigned last_hash ;
  103.  
  104. static  HASHNODE  *delete( s )
  105.   char *s ;
  106. { register HASHNODE *p ;
  107.   HASHNODE *q = (HASHNODE *) 0 ;
  108.   unsigned h ;
  109.  
  110.   p = hash_table[ last_hash = h = hash(s) % HASH_PRIME ] ;
  111.   while ( p )
  112.       if ( strcmp(p->symtab.name, s) == 0 )  /* found */
  113.       {
  114.         if ( q )  q->link = p->link ;
  115.         else  hash_table[h] = p->link ;
  116.         return p ;
  117.       }
  118.       else { q = p ; p = p->link ; }
  119.  
  120. #ifdef  DEBUG   /* we should not ever get here */
  121.   bozo("delete") ;
  122. #endif
  123.   return (HASHNODE *) 0 ;
  124. }
  125.  
  126. /* when processing user functions,  global ids which are
  127.    replaced by local ids are saved on this list */
  128.  
  129. static HASHNODE  *save_list ;
  130.  
  131. /* store a global id on the save list,
  132.    return a ptr to the local symtab  */
  133. SYMTAB *save_id( s )
  134.   char *s ;
  135. { HASHNODE *p, *q ;
  136.   unsigned h ;
  137.  
  138.   p = delete(s) ;
  139.   q = new_HASHNODE() ;
  140.   q->symtab.type = ST_LOCAL_NONE ;
  141.   q->symtab.name = p->symtab.name ;
  142.   /* put q in the hash table */
  143.   q->link = hash_table[ h = last_hash ] ;
  144.   hash_table[h] = q ;
  145.  
  146.   /* save p */
  147.   p->link = save_list ; save_list = p ;
  148.  
  149.   return & q->symtab ;
  150. }
  151.  
  152. /* restore all global indentifiers */
  153. void  restore_ids()
  154. { register HASHNODE *p, *q ;
  155.   register unsigned h ;
  156.  
  157.   q = save_list ; save_list = (HASHNODE *) 0 ;
  158.   while ( q )
  159.   {
  160.     p = q ; q = q->link ;
  161.     zfree( delete(p->symtab.name) , sizeof(HASHNODE) ) ;
  162.     p->link = hash_table[h = last_hash ] ; 
  163.     hash_table[h] = p ;
  164.   }
  165. }
  166.  
  167.  
  168. /* search the symbol table backwards for the
  169.    disassembler.  This is slow -- so what
  170. */
  171.  
  172. #if ! SM_DOS
  173.  
  174. char *reverse_find( type, ptr)
  175.   int type ;
  176.   PTR ptr ;
  177. {
  178.   CELL *cp ;
  179.   ARRAY array ;
  180.   static char uk[] = "unknown" ;
  181.  
  182.   int i ;
  183.   HASHNODE *p ;
  184.  
  185.  
  186.   switch( type )
  187.   {
  188.     case ST_VAR :
  189.     case ST_FIELD :
  190.        cp = *(CELL **) ptr ;
  191.        break ;
  192.  
  193.     case ST_ARRAY :
  194.        array = *(ARRAY *) ptr ;
  195.        break ;
  196.  
  197.     default :  return uk ;
  198.   }
  199.  
  200.   for(i = 0 ; i < HASH_PRIME ; i++)
  201.   {
  202.     p = hash_table[i] ;
  203.     while ( p )
  204.     {
  205.     if ( p->symtab.type == type )
  206.         switch(type)
  207.         {
  208.           case ST_VAR :
  209.           case ST_FIELD :
  210.             if ( cp == p->symtab.stval.cp )
  211.             return p->symtab.name ;
  212.             break ;
  213.  
  214.           case ST_ARRAY :
  215.             if ( array == p->symtab.stval.array )
  216.             return p->symtab.name ;
  217.             break ;
  218.         }
  219.  
  220.     p = p->link ;
  221.     }
  222.   }
  223.   return uk ;
  224. }
  225.       
  226. #endif
  227.