File: schash.c

package info (click to toggle)
pact 980714-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 13,096 kB
  • ctags: 26,034
  • sloc: ansic: 109,076; lisp: 9,645; csh: 7,147; fortran: 1,050; makefile: 136; lex: 95; sh: 32
file content (345 lines) | stat: -rw-r--r-- 9,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * SCHASH.C - routines to manipulate hash tables
 *          - intended to be a complete module that
 *          - can be used with any application by defining
 *          - the struct, hashel, in SCORE.H suitably
 *
 * Source Version: 2.0
 * Software Release #92-0043
 *
 */

#include "cpyright.h"

#include "score.h"

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_HASH - compute hash value for string s in a table of size */

int SC_hash(s, size)
   char *s;
   int size;
   {int hashval;

    for (hashval = 0; *s != '\0'; )
        hashval = (hashval << 1) ^ (*s++);

    return(abs(hashval) % size);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_LOOKUP - lookup s in hash table, table */

hashel *SC_lookup(s, tab)
   char *s;
   HASHTAB *tab;
   {hashel *np, **tb;
    int sz;

    if (tab == NULL)
       return(NULL);

    sz = tab->size;
    tb = tab->table;
    for (np = tb[SC_hash(s, sz)]; np != NULL; np = np->next)
        if (strcmp(s, np->name) == 0)
           return(np);                                          /* found it */

    return(NULL);}                                             /* not found */

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_DEF_LOOKUP - return a pointer to the object version of LOOKUP */

byte *SC_def_lookup(s, tab)
   char *s;
   HASHTAB *tab;
   {hashel *np;
   
    if (tab == NULL)
       return(NULL);

    np = SC_lookup(s, tab);
    if (np != NULL)
       return(np->def);
    else
       return(NULL);}
    
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_INSTALL - install an object in the hash table and mark it
 *            - the object type is defined in hash.h and is generic
 *            - to enhance the portability of this code
 *            - WARNING: do NOT use literals or volatiles for the type;
 *            -          for efficiency they are not strsavef'd !!!
 */

hashel *SC_install(name, obj, type, tab)
   char *name;
   byte *obj;
   char *type;
   HASHTAB *tab;
   {return (_SC_install(name, obj, type, tab, TRUE));}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _SC_INSTALL - install an object in the hash table
 *             - the object type is defined in hash.h and is generic
 *             - to enhance the portability of this code
 *             - WARNING: do NOT use literals or volatiles for the type;
 *             -          for efficiency they are not strsave'd !!!
 */

hashel *_SC_install(name, obj, type, tab, mark)
   char *name;
   byte *obj;
   char *type;
   HASHTAB *tab;
   int mark;
   {hashel *np, **tb;
    int hashval, sz;

    sz = tab->size;
    tb = tab->table;
    np = SC_lookup(name, tab);

/* if not found install it */
    if (np == NULL)
       {np = FMAKE(hashel, "_SC_INSTALL:np");
        if (np == NULL)
           return(NULL);

        np->name = SC_strsavef(name, "char*:_SC_INSTALL:name");
        if (np->name == NULL)
           return(NULL);

        hashval     = SC_hash(np->name, sz);
        np->next    = tb[hashval];
        tb[hashval] = np;
        (tab->nelements)++;}

    np->type = type;
    np->def  = obj;

    if (mark)
       {np->free = TRUE;
        SC_mark(obj, 1);}
    else
       np->free = FALSE;

    return(np);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_HASH_REM - remove the specified entry from the given hash table
 *             - return TRUE if successfully removed and
 *             - otherwise return FALSE
 */

int SC_hash_rem(name, tab)
   char *name;
   HASHTAB *tab;
   {hashel *np, *nxt, **tb;
    int sz, i;

    sz = tab->size;
    tb = tab->table;
    i  = SC_hash(name, sz);
    np = tb[i];

/* if not found nothing else to do */
    if (np == NULL)
       return(FALSE);
    else
       {if (strcmp(name, np->name) == 0)
           {tb[i] = np->next;

/* undo the MARK in SC_install */
            if (np->free == TRUE)
	       SFREE(np->def);
            SFREE(np->name);
            SFREE(np);
            (tab->nelements)--;
            return(TRUE);}

/* otherwise search for it */
        else
           for (; np->next != NULL; np = np->next)
               {nxt = np->next;
                if (strcmp(name, nxt->name) == 0)
                   {np->next = nxt->next;
		    if (np->free == TRUE)
		      SFREE(nxt->def);
                    SFREE(nxt->name);
                    SFREE(nxt);
                    (tab->nelements)--;
                    return(TRUE);};};};
   
    return(FALSE);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_HASH_CLR - clear the specified hash table */

void SC_hash_clr(tab)
   HASHTAB *tab;
   {int i, sz;
    hashel **tb, *np, *nxt;

    sz = tab->size;
    tb = tab->table;
    for (i = 0; i < sz; i++)
        {for (np = tb[i]; np != NULL; np = nxt)
             {nxt = np->next;

/* undo the MARK in SC_install */
	      if (np->free == TRUE)
		 SFREE(np->def);
              SFREE(np->name);
              SFREE(np);};
         tb[i] = NULL;};

    return;}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_MAKE_HASH_TABLE - allocate and initialize a hash table of size, sz
 *                    - returns a HASHTAB pointer
 */

HASHTAB *SC_make_hash_table(sz, docflag)
   int sz, docflag;
   {HASHTAB *tab;
    hashel **tb;
    int i;

/* allocate a new hash table */
    tab = FMAKE(HASHTAB, "SC_MAKE_HASH_TABLE:tab");

    if (tab == NULL)
       {printf("\nCannot allocate a new hash table of size %d\n", sz);
        return(NULL);};

    tb = FMAKE_N(hashel *, sz, "SC_MAKE_HASH_TABLE:tb");
    if (tb == NULL)
       return(NULL);

    tab->size      = sz;
    tab->docp      = docflag;
    tab->nelements = 0;
    tab->table     = tb;

/* explicitly NULL the pointers */
    for (i = 0; i < sz; i++)
        tb[i] = NULL;

    return(tab);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_RL_HASH_TABLE - release a hash table
 *                  - call SC_HASH_CLR first to release the contents
 *                  - of the table
 */

void SC_rl_hash_table(tab)
   HASHTAB *tab;
   {SC_hash_clr(tab);

    SFREE(tab->table);
    SFREE(tab);

    return;}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _SC_DUMP_HASH - return an array of pointers whose entries point to the
 *               - installed names in the given hash table
 */

char **_SC_dump_hash(tab, patt, type, sort)
   HASHTAB *tab;
   char *patt, *type;
   int sort;
   {hashel *np, **tb;
    char **lineptr, *name;
    int i, sz, nlines;

    if (tab == NULL)
       return(NULL);

/* allocate a list of pointers to the names in the hash table */
    lineptr = FMAKE_N(char *, tab->nelements, "SC_HASH_DUMP:lineptr");
    if (lineptr == NULL)
       return(NULL);

/* fill in the list of pointers to names in the hash table */
    sz = tab->size;
    tb = tab->table;

    nlines = 0;
    for (i = 0; i < sz; i++)
        for (np = tb[i]; np != NULL; np = np->next)
            {if ((type != NULL) && (strcmp(type, np->type) != 0))
	        continue;
	        
	     name = np->name;
             if (patt == NULL)
                lineptr[nlines++] = name;

             else if (SC_regx_match(name, patt))
                lineptr[nlines++] = name;};

/* check that the number of names found is what is expected */
    if (nlines > tab->nelements)
       return(NULL);

    REMAKE_N(lineptr, char *, nlines + 1);
    lineptr[nlines] = NULL;

/* sort the names */
    if (sort)
       SC_string_sort(lineptr, nlines);

/* return the list of names */
    return(lineptr);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_DUMP_HASH - return an array of pointers whose entries point to the
 *              - installed names in the given hash table
 */

char **SC_dump_hash(tab, patt, sort)
   HASHTAB *tab;
   char *patt;
   int sort;
   {return(_SC_dump_hash(tab, patt, NULL, sort));}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* SC_HASH_DUMP - return an array of pointers whose entries point to the
 *              - installed names in the given hash table and
 *              - are alphabetically ordered (by strcmp)
 */

char **SC_hash_dump(tab, patt)
   HASHTAB *tab;
   char *patt;
   {return(_SC_dump_hash(tab, patt, NULL, TRUE));}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/