home *** CD-ROM | disk | FTP | other *** search
/ The Best Internet Programs / BESTINTERNET.bin / latest / ged2ht20 / index.c < prev    next >
C/C++ Source or Header  |  1995-04-06  |  1KB  |  72 lines

  1. /*
  2.  * XREF ID --> pointer index routines
  3.  *
  4.  * If too much time is being spent in this code, replace it with something
  5.  * more sophisticated.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "index.h"
  12.  
  13. struct ientry {
  14.   char *id;
  15.   void *value;
  16.   struct ientry *next;
  17. };
  18.  
  19. void out_of_memory();
  20.  
  21. #define HASHSIZE 5003
  22. #define HASHMULT 251
  23.  
  24. struct ientry *hashtab[HASHSIZE];
  25.  
  26. int hash(char *id)
  27. {
  28.   unsigned int h = 0;
  29.   while(*id) {
  30.     h = (h * HASHMULT) + *id;
  31.     id++;
  32.   }
  33.   return(h % HASHSIZE);
  34. }
  35.  
  36. int index_enter(char *id, void *value)
  37. {
  38.   int h;
  39.   struct ientry *ip, *pip, *new;
  40.  
  41.   h = hash(id);
  42.   for(ip = hashtab[h], pip = NULL; ip != NULL; pip = ip, ip = ip->next) {
  43.     if(!strcmp(ip->id, id)) {
  44.       fprintf(stderr, "Multiply defined cross-reference ID: %s\n", id);
  45.       return(-1);
  46.     }
  47.   }
  48.   if((new = malloc(sizeof(struct ientry))) == NULL)
  49.     out_of_memory();
  50.   new->id = strdup(id);
  51.   new->value = value;
  52.   new->next = NULL;
  53.   if(pip == NULL) {
  54.     hashtab[h] = new;
  55.   } else {
  56.     pip->next = new;
  57.   }
  58. }
  59.  
  60. void *index_find(char *id)
  61. {
  62.   int h;
  63.   struct ientry *ip;
  64.  
  65.   h = hash(id);
  66.   for(ip = hashtab[h]; ip != NULL; ip = ip->next) {
  67.     if(!strcmp(ip->id, id))
  68.       return(ip->value);
  69.   }
  70.   return(NULL);
  71. }
  72.