1: /* $Header$ */
   2: 
   3: /*
   4:  * Author: Peter J. Nicklin
   5:  */
   6: 
   7: #include "null.h"
   8: #include "hash.h"
   9: #include "macro.h"
  10: 
  11: /*
  12:  * htrm() removes a hash table entry. If key is null, the entire hash
  13:  * table is removed.
  14:  */
  15: void
  16: htrm(key, hash)
  17:     char *key;          /* key for hash table entry */
  18:     HASH *hash;         /* hash table */
  19: {
  20:     HASHBLK *htbrm();       /* remove hash table block */
  21:     HASHBLK *htc;           /* first hash table block in chain */
  22:     int hashval;            /* hash value for key */
  23:     int hthash();           /* compute hash value */
  24:     int i;              /* hash table index */
  25: 
  26:     if (key == NULL)
  27:         {
  28:         for (i = 0; i < hash->hashsiz; i++)
  29:             if ((htc = (hash->hashtab)[i]) != NULL)
  30:                 htc = htbrm(key, htc);
  31:         free((char *) hash);
  32:         }
  33:     else    {
  34:         hashval = hthash(key, hash);
  35:         if ((htc = (hash->hashtab)[hashval]) != NULL)
  36:             (hash->hashtab)[hashval] = htbrm(key, htc);
  37:         }
  38: }
  39: 
  40: 
  41: 
  42: /*
  43:  * htbrm() removes a hash table block identified by key. If key is null, the
  44:  * entire chain is removed. Returns a pointer to the first block in the chain.
  45:  */
  46: HASHBLK *
  47: htbrm(key, htc)
  48:     char *key;          /* key string */
  49:     HASHBLK *htc;           /* hash table block chain */
  50: {
  51:     HASHBLK *curblk;        /* current list block */
  52:     HASHBLK *nxtblk;        /* next list block */
  53:     HASHBLK *prvblk;        /* previous list block */
  54: 
  55:     if (key == NULL)
  56:         while (htc != NULL)
  57:             {
  58:             nxtblk = htc->h_next;
  59:             free(htc->h_key);
  60:             free(htc->h_def);
  61:             free((char *) htc);
  62:             htc = nxtblk;
  63:             }
  64:     else    {
  65:         /* first block is a special case */
  66:         if (EQUAL(htc->h_key, key))
  67:             {
  68:             nxtblk = htc->h_next;
  69:             free(htc->h_key);
  70:             free(htc->h_def);
  71:             free((char *) htc);
  72:             htc = nxtblk;
  73:             }
  74:         else    {
  75:             /* remainder of list */
  76:             prvblk = htc;
  77:             curblk = htc->h_next;
  78:             while (curblk != NULL)
  79:                 if (EQUAL(curblk->h_key, key))
  80:                     {
  81:                     prvblk->h_next = curblk->h_next;
  82:                     free(curblk->h_key);
  83:                     free(curblk->h_def);
  84:                     free((char *) curblk);
  85:                     curblk = prvblk->h_next;
  86:                     break;
  87:                     }
  88:                 else    {
  89:                     prvblk = curblk;
  90:                     curblk = curblk->h_next;
  91:                     }
  92:             }
  93:         }
  94:     return(htc);
  95: }

Defined functions

htbrm defined in line 46; used 3 times
htrm defined in line 15; never used
Last modified: 1985-07-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 420
Valid CSS Valid XHTML 1.0 Strict