home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / refer / thash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  1.7 KB  |  141 lines

  1. # include "stdio.h"
  2. int nh 500;
  3. int saw[6000];
  4. char *comname "/usr/lib/eign";
  5.  
  6. main (argc,argv)
  7.     char *argv[];
  8. {
  9.  
  10. int i, z;
  11. char *name;
  12.  
  13. FILE *f;
  14.  
  15. while (argc>1 && argv[1][0] == '-')
  16.     {
  17.     switch(argv[1][1])
  18.         {
  19.         case 'h':
  20.             nh = atoi(argv[1]+2); break;
  21.         }
  22.     argc--; argv++;
  23.     }
  24. if (argc<=1)
  25.     dofile(stdin, "");
  26. else
  27. for(i=1; i<argc; i++)
  28.     {
  29.     f = fopen(name=argv[i], "r");
  30.     if (f==NULL)
  31.         err("No file %s",name);
  32.     else
  33.         dofile(f, name);
  34.     }
  35. for(z=i=0; i<nh; i++)
  36.     {
  37.     if (saw[i]) z++;
  38.     }
  39. printf("hashes %d used %d\n",nh,z);
  40. }
  41. # include "stdio.h"
  42.  
  43. dofile(f, name)
  44.     FILE *f;
  45.     char *name;
  46. {
  47.  
  48. /* read file f & spit out keys & ptrs */
  49. # define MAXLINE 750
  50. char line[MAXLINE], *s;
  51. char key[20], *p;
  52. int k 0;
  53. int c, lim;
  54. int alph 0;
  55. int used 0;
  56. long lp 0;
  57.  
  58. while (fgets(line, MAXLINE, f))
  59.     {
  60.     k++;
  61.     used=alph=0;
  62.     lim = strlen(line);
  63.     p = key;
  64.     for(s=line; c= *s; s++)
  65.         {
  66.         if (isalpha(c) || isdigit(c))
  67.             {
  68.             if (alph++ < 6)
  69.                 *p++ = c;
  70.             }
  71.         else
  72.             {
  73.             *p = 0;
  74.             if (outkey(p=key))
  75.                 {
  76.                 tkey(key,k);
  77.                 used=1;
  78.                 }
  79.             alph=0;
  80.             }
  81.         }
  82.     lp += lim;
  83.     }
  84. }
  85.  
  86. outkey( ky)
  87.     char *ky;
  88. {
  89.     int n;
  90. n = strlen(ky);
  91. if (n<3) return(0);
  92. if (isdigit(ky[0]))
  93.     if (ky[0] != '1' || ky[1] != '9' || n!= 4) return(0);
  94. return(1);
  95. }
  96. # include "stdio.h"
  97. hash (s)
  98.     char *s;
  99. {
  100. int c, n, q;
  101. for(q=n=0; c= *s; s++)
  102.     n += (c*n + c << (n%4));
  103. return(n);
  104. }
  105. err (s, a)
  106.     char *s;
  107. {
  108. fprintf(stderr, "Error: ");
  109. fprintf(stderr, s, a);
  110. putc('\n', stderr);
  111. }
  112. prefix(t, s)
  113.     char *t, *s;
  114. {
  115. int c, d;
  116. while ( (c= *t++) == *s++)
  117.     if (c==0) return(1);
  118. return(c==0 ? 1: 0);
  119. }
  120. mindex(s, c)
  121.     char *s;
  122. {
  123. register char *p;
  124. for( p=s; *p; p++)
  125.     if (*p ==c)
  126.         return(p);
  127. return(0);
  128. }
  129. tkey(s,nw)
  130.     char *s;
  131. {
  132. int x;
  133. x = abs(hash(s)) % nh;
  134. /* if (saw[x]) printf("%d %d\n", x, nw); */
  135. saw[x]= nw;
  136. }
  137. abs(n)
  138. {
  139. return(n>0 ? n : -n);
  140. }
  141.