ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/modobject.c
Revision: 2.5
Committed: Tue Aug 11 09:54:34 1998 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.4: +1 -1 lines
Log Message:
fixed bug in objndx()

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ 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 int
29 objndx(op) /* get object number from pointer */
30 register OBJREC *op;
31 {
32 register int i, j;
33
34 for (i = nobjects>>6; i >= 0; i--) {
35 j = op - objblock[i];
36 if (j >= 0 && j < 0100)
37 return((i<<6) + j);
38 }
39 return(OVOID);
40 }
41
42
43 int
44 lastmod(obj, mname) /* find modifier definition before obj */
45 OBJECT obj;
46 char *mname;
47 {
48 register OBJREC *op;
49 register int i;
50
51 i = modifier(mname); /* try hash table first */
52 if (i < obj)
53 return(i);
54 for (i = obj; i-- > 0; ) { /* need to search */
55 op = objptr(i);
56 if (ismodifier(op->otype) && !strcmp(op->oname, mname))
57 return(i);
58 }
59 return(OVOID);
60 }
61
62
63 int
64 modifier(mname) /* get a modifier number from its name */
65 char *mname;
66 {
67 register int ndx;
68
69 ndx = otndx(mname, &modtab);
70 return(modtab.htab[ndx]);
71 }
72
73
74 #ifdef GETOBJ
75 int
76 object(oname) /* get an object number from its name */
77 char *oname;
78 {
79 register int ndx;
80
81 ndx = otndx(oname, &objtab);
82 return(objtab.htab[ndx]);
83 }
84 #endif
85
86
87 insertobject(obj) /* insert new object into our list */
88 register OBJECT obj;
89 {
90 register int i;
91
92 if (ismodifier(objptr(obj)->otype)) {
93 i = otndx(objptr(obj)->oname, &modtab);
94 modtab.htab[i] = obj;
95 }
96 #ifdef GETOBJ
97 else {
98 i = otndx(objptr(obj)->oname, &objtab);
99 objtab.htab[i] = obj;
100 }
101 #endif
102 for (i = 0; addobjnotify[i] != NULL; i++)
103 (*addobjnotify[i])(obj);
104 }
105
106
107 static int
108 nexthsiz(oldsiz) /* return next hash table size */
109 int oldsiz;
110 {
111 static int hsiztab[] = {
112 251, 509, 1021, 2039, 4093, 8191, 16381, 0
113 };
114 register int *hsp;
115
116 for (hsp = hsiztab; *hsp; hsp++)
117 if (*hsp > oldsiz)
118 return(*hsp);
119 return(oldsiz*2 + 1); /* not always prime */
120 }
121
122
123 static int
124 otndx(name, tab) /* get object table index for name */
125 char *name;
126 register struct ohtab *tab;
127 {
128 OBJECT *oldhtab;
129 int hval, i;
130 register int ndx;
131
132 if (tab->htab == NULL) { /* new table */
133 tab->hsiz = nexthsiz(tab->hsiz);
134 tab->htab = (OBJECT *)malloc(tab->hsiz*sizeof(OBJECT));
135 if (tab->htab == NULL)
136 error(SYSTEM, "out of memory in otndx");
137 ndx = tab->hsiz;
138 while (ndx--) /* empty it */
139 tab->htab[ndx] = OVOID;
140 }
141 /* look up object */
142 hval = shash(name);
143 tryagain:
144 for (i = 0; i < tab->hsiz; i++) {
145 ndx = (hval + i*i) % tab->hsiz;
146 if (tab->htab[ndx] == OVOID ||
147 !strcmp(objptr(tab->htab[ndx])->oname, name))
148 return(ndx);
149 }
150 /* table is full, reallocate */
151 oldhtab = tab->htab;
152 ndx = tab->hsiz;
153 tab->htab = NULL;
154 while (ndx--)
155 if (oldhtab[ndx] != OVOID) {
156 i = otndx(objptr(oldhtab[ndx])->oname, tab);
157 tab->htab[i] = oldhtab[ndx];
158 }
159 free((char *)oldhtab);
160 goto tryagain; /* should happen only once! */
161 }