Commit | Line | Data |
---|---|---|
832026c6 C |
1 | #include "defs" |
2 | ||
3 | #ifdef HASHEDTABLE | |
4 | /* Basic symbol table maintainer. Action depends on t: | |
5 | t = -1 Remove name from table | |
6 | t = 0 Put name in table if not there. Copy name string | |
7 | t = 1 Find name in table if there, otherwise return 0. | |
8 | t = 2 Put name in table if not there. Do not copy name | |
9 | */ | |
10 | ||
11 | struct stentry *hashtab[MAXEFLNAMES+1]; | |
12 | struct stentry **hashend = hashtab+MAXEFLNAMES+1; | |
13 | ||
14 | #define NEXT(x) (++x<hashend ? x : hashtab ) | |
15 | ||
16 | struct stentry *name(s,t) | |
17 | char *s; | |
18 | int t; | |
19 | { | |
20 | int hash; | |
21 | register struct stentry *p, **hp; | |
22 | char *copys(); | |
23 | ||
24 | hash = hashfunct(s); | |
25 | ||
26 | for(hp = hashtab + hash; (p = *hp) ; hp = NEXT(hp) ) | |
27 | if(hash==p->hashval && equals(s,p->namep)) | |
28 | switch(t) | |
29 | { | |
30 | case -1: | |
31 | cfree(p->namep); | |
32 | cfree(p); | |
33 | delhash(hp); | |
34 | --neflnames; | |
35 | return(0); | |
36 | ||
37 | case 0: | |
38 | case 1: | |
39 | case 2: | |
40 | return(p); | |
41 | ||
42 | default: | |
43 | fatal("name: illegal argument"); | |
44 | } | |
45 | ||
46 | /* not in table */ | |
47 | switch(t) | |
48 | { | |
49 | case -1: | |
50 | fatal1("cannot delete nonexistent name %s from symbol table", s); | |
51 | ||
52 | case 1: | |
53 | return(0); | |
54 | ||
55 | case 0: | |
56 | case 2: | |
57 | if(++neflnames >= MAXEFLNAMES) | |
58 | fatal("hash table full"); | |
59 | ||
60 | *hp = p = ALLOC(stentry); | |
61 | p->namep = (t==0 ? copys(s) : s); | |
62 | p->hashval = hash; | |
63 | return(p); | |
64 | ||
65 | default: | |
66 | fatal("illegal call to name"); | |
67 | } | |
68 | } | |
69 | ||
70 | ||
71 | ||
72 | hashfunct(s) | |
73 | register char *s; | |
74 | { | |
75 | register int h; | |
76 | ||
77 | h = 0; | |
78 | while(*s) | |
79 | h += *s++; | |
80 | ||
81 | return( h % (MAXEFLNAMES+1) ); | |
82 | } | |
83 | ||
84 | ||
85 | delhash(hp) | |
86 | struct stentry **hp; | |
87 | { | |
88 | struct stentry **hq, **hvp; | |
89 | ||
90 | for ( ; ; ) | |
91 | { | |
92 | *hp = 0; | |
93 | hq = hp; | |
94 | for(hp = NEXT(hp) ; *hp && | |
95 | ( (hq < (hvp = hashtab + (*hp)->hashval) && hvp<=hp) | |
96 | || (hp<hq && hq<hvp) || (hvp<=hp && hp<hq) ) ; | |
97 | hp = NEXT(hp) ) | |
98 | ; | |
99 | if(*hp == 0) | |
100 | return; | |
101 | *hq = *hp; | |
102 | } | |
103 | } | |
104 | #endif | |
105 | \f | |
106 | #ifndef HASHEDTABLE | |
107 | /* Basic symbol table maintainer. Action depends on t: | |
108 | t = -1 Remove name from table | |
109 | t = 0 Put name in table if not there. Copy name string | |
110 | t = 1 Find name in table if there, otherwise return 0. | |
111 | t = 2 Put name in table if not there. Do not copy name | |
112 | */ | |
113 | ||
114 | struct stentry *hashtab[MAXEFLNAMES]; | |
115 | struct stentry **hashend hashtab; | |
116 | ||
117 | name(s,t) | |
118 | char *s; | |
119 | int t; | |
120 | { | |
121 | int hash; | |
122 | register struct stentry *p, **hp; | |
123 | char *copys(); | |
124 | ||
125 | hash = hashfunct(s); | |
126 | ||
127 | for(hp = hashtab ; hp<hashend ; ++hp) | |
128 | if( (p = *hp) && hash==p->hashval && equals(s,p->namep)) | |
129 | switch(t) | |
130 | { | |
131 | case -1: | |
132 | cfree(p->namep); | |
133 | cfree(p); | |
134 | *hp = 0; | |
135 | return(0); | |
136 | ||
137 | case 0: | |
138 | case 1: | |
139 | case 2: | |
140 | return(p); | |
141 | ||
142 | default: | |
143 | fatal("name: illegal argument"); | |
144 | } | |
145 | ||
146 | /* not in table */ | |
147 | switch(t) | |
148 | { | |
149 | case -1: | |
150 | fatal1("cannot delete nonexistent name %s from symbol table", s); | |
151 | ||
152 | case 1: | |
153 | return(0); | |
154 | ||
155 | case 0: | |
156 | case 2: | |
157 | /* look for an empty slot */ | |
158 | for(hp = hashtab ; hp<hashend && *hp!=0 ; ++hp) | |
159 | ; | |
160 | ||
161 | if(hp == hashend) | |
162 | if(++neflnames >= MAXEFLNAMES) | |
163 | fatal("hash table full"); | |
164 | else ++hashend; | |
165 | ||
166 | *hp = p = ALLOC(stentry); | |
167 | p->namep = (t==0 ? copys(s) : s); | |
168 | p->hashval = hash; | |
169 | return(p); | |
170 | ||
171 | default: | |
172 | fatal("illegal call to name"); | |
173 | } | |
174 | } | |
175 | ||
176 | ||
177 | ||
178 | hashfunct(s) | |
179 | register char *s; | |
180 | { | |
181 | register int h; | |
182 | ||
183 | h = 0; | |
184 | while(*s) | |
185 | h = *s++; | |
186 | ||
187 | return(h); | |
188 | } | |
189 | #endif |