home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / RADIANCE / SRC / COMMON / MODOBJEC.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  3KB  |  137 lines

  1. /* Copyright (c) 1986 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)modobject.c 2.1 11/12/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  Routines for tracking object modifiers
  9.  */
  10.  
  11. #include  "standard.h"
  12.  
  13. #include  "object.h"
  14.  
  15. #include  "otypes.h"
  16.  
  17.  
  18. extern int  (*addobjnotify[])();    /* people to notify of new objects */
  19.  
  20. static struct ohtab {
  21.     int  hsiz;            /* current table size */
  22.     OBJECT  *htab;            /* table, if allocated */
  23. }  modtab = {100, NULL}, objtab = {1000, NULL};    /* modifiers and objects */
  24.  
  25. static int  otndx();
  26.  
  27.  
  28. #ifdef  GETOBJ
  29. int
  30. object(oname)            /* get an object number from its name */
  31. char  *oname;
  32. {
  33.     register int  ndx;
  34.  
  35.     ndx = otndx(oname, &objtab);
  36.     return(objtab.htab[ndx]);
  37. }
  38. #endif
  39.  
  40.  
  41. int
  42. modifier(mname)            /* get a modifier number from its name */
  43. char  *mname;
  44. {
  45.     register int  ndx;
  46.  
  47.     ndx = otndx(mname, &modtab);
  48.     return(modtab.htab[ndx]);
  49. }
  50.  
  51.  
  52. insertobject(obj)        /* insert new object into our list */
  53. register OBJECT  obj;
  54. {
  55.     register int  i;
  56.  
  57. #ifdef  GETOBJ
  58.     i = otndx(objptr(obj)->oname, &objtab);
  59.     objtab.htab[i] = obj;
  60. #endif
  61.     if (ismodifier(objptr(obj)->otype)) {
  62.         i = otndx(objptr(obj)->oname, &modtab);
  63.         modtab.htab[i] = obj;
  64.     }
  65.     for (i = 0; addobjnotify[i] != NULL; i++)
  66.         (*addobjnotify[i])(obj);
  67. }
  68.  
  69.  
  70. static int
  71. nexthsiz(oldsiz)        /* return next hash table size */
  72. int  oldsiz;
  73. {
  74.     static int  hsiztab[] = {
  75.         251, 509, 1021, 2039, 4093, 8191, 16381, 0
  76.     };
  77.     register int  *hsp;
  78.  
  79.     for (hsp = hsiztab; *hsp; hsp++)
  80.         if (*hsp > oldsiz)
  81.             return(*hsp);
  82.     return(oldsiz*2 + 1);        /* not always prime */
  83. }
  84.  
  85.  
  86. static int
  87. shash(s)            /* hash a string */
  88. register char  *s;
  89. {
  90.     register int  h = 0;
  91.  
  92.     while (*s)
  93.         h = (h<<1 & 0x7fff) ^ *s++;
  94.     return(h);
  95. }
  96.  
  97.  
  98. static int
  99. otndx(name, tab)        /* get object table index for name */
  100. char  *name;
  101. register struct ohtab  *tab;
  102. {
  103.     OBJECT  *oldhtab;
  104.     int  hval, i;
  105.     register int  ndx;
  106.  
  107.     if (tab->htab == NULL) {        /* new table */
  108.         tab->hsiz = nexthsiz(tab->hsiz);
  109.         tab->htab = (OBJECT *)malloc(tab->hsiz*sizeof(OBJECT));
  110.         if (tab->htab == NULL)
  111.             error(SYSTEM, "out of memory in otndx");
  112.         ndx = tab->hsiz;
  113.         while (ndx--)            /* empty it */
  114.             tab->htab[ndx] = OVOID;
  115.     }
  116.                     /* look up object */
  117.     hval = shash(name);
  118. tryagain:
  119.     for (i = 0; i < tab->hsiz; i++) {
  120.         ndx = (hval + i*i) % tab->hsiz;
  121.         if (tab->htab[ndx] == OVOID ||
  122.                 !strcmp(objptr(tab->htab[ndx])->oname, name))
  123.             return(ndx);
  124.     }
  125.                     /* table is full, reallocate */
  126.     oldhtab = tab->htab;
  127.     ndx = tab->hsiz;
  128.     tab->htab = NULL;
  129.     while (ndx--)
  130.         if (oldhtab[ndx] != OVOID) {
  131.             i = otndx(objptr(oldhtab[ndx])->oname, tab);
  132.             tab->htab[i] = oldhtab[ndx];
  133.         }
  134.     free((char *)oldhtab);
  135.     goto tryagain;            /* should happen only once! */
  136. }
  137.