home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / vn.jan.88 / part03 / hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  1.8 KB  |  112 lines

  1. /*
  2. ** vn news reader.
  3. **
  4. ** hash.c - hash table routines
  5. **
  6. ** see copyright disclaimer / history in vn.c source file
  7. */
  8.  
  9. #include <stdio.h>
  10. #include "config.h"
  11. #include "tune.h"
  12. #include "node.h"
  13.  
  14. /*
  15. ** hash table manipulation routines:
  16. */
  17.  
  18. extern int Ncount;
  19. extern NODE **Newsorder;
  20.  
  21. static NODE *Tab [HASHSIZE];    /* hash Table */
  22.  
  23. hashinit ()
  24. {
  25.     int i;
  26.     for (i=0; i < HASHSIZE; ++i)
  27.         Tab[i] = NULL;
  28.     Ncount = 0;
  29. }
  30.  
  31. /*
  32.     enter new node (name s, articles n, low l) in hash Table, 
  33.     initial flags = 0.  Set order to -1.
  34. */
  35. NODE *hashenter(s,n,l)
  36. char *s;
  37. int n;
  38. int l;
  39. {
  40.     char *str_store();
  41.     NODE *ptr,*node_store();
  42.     NODE *hashfind();
  43.     int i;
  44.  
  45.     if ((ptr = hashfind(s)) != NULL)
  46.     {
  47.         fgprintf ("Warning: group %s encountered twice",s);
  48.         return (ptr);
  49.     }
  50.  
  51.     i=hash(s);
  52.     ptr = node_store();
  53.     ptr->next = Tab[i];
  54.     Tab[i] = ptr;
  55.     if (l > n)
  56.         l = n;
  57.     ++Ncount;
  58.     ptr->lownum = l;
  59.     ptr->state = 0;
  60.     ptr->data = NULL;
  61.     ptr->flags = 0;
  62.     ptr->highnum = n;
  63.     ptr->nd_name = str_store(s);
  64.     ptr->pgshwn = 0;
  65.     ptr->order = -1;
  66.     return (ptr);
  67. }
  68.  
  69. NODE *hashfind(s)
  70. char *s;
  71. {
  72.     NODE *ptr;
  73.  
  74.     for (ptr = Tab[hash(s)]; ptr != NULL && strcmp(ptr->nd_name,s) != 0;
  75.                     ptr = ptr->next)
  76.             ;
  77.     return (ptr);
  78. }
  79.  
  80. make_newsorder()
  81. {
  82.     char *malloc();
  83.     int i;
  84.     NODE *ptr;
  85.  
  86.     if ((Newsorder = (NODE **) malloc(Ncount * sizeof(NODE *))) == NULL)
  87.         printex("Memory allocation failure - newsorder array");
  88.     for (i=0; i < Ncount; ++i)
  89.         Newsorder[i] = NULL;
  90.     for (i=0; i < HASHSIZE; ++i)
  91.     {
  92.         for (ptr = Tab[i]; ptr != NULL; ptr = ptr->next)
  93.         {
  94.             if (ptr->order < 0 || ptr->order >= Ncount)
  95.                 printex("News order range error");
  96.             Newsorder[ptr->order] = ptr;
  97.         }
  98.     }
  99.     for (i=0; i < Ncount; ++i)
  100.         if (Newsorder[i] == NULL)
  101.             printex("News order duplication error");
  102. }
  103.  
  104. static hash (s)
  105. char *s;
  106. {
  107.     unsigned rem;
  108.     for (rem=0; *s != '\0'; ++s)
  109.         rem = (rem*128 + (*s&0x7f)) % HASHSIZE;
  110.     return (rem);
  111. }
  112.