| 1 |
gregl |
2.3 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */
|
| 2 |
greg |
2.1 |
|
| 3 |
|
|
#ifndef lint
|
| 4 |
gregl |
2.3 |
static char SCCSid[] = "$SunId$ SGI";
|
| 5 |
greg |
2.1 |
#endif
|
| 6 |
|
|
|
| 7 |
|
|
/*
|
| 8 |
|
|
* Table lookup routines
|
| 9 |
|
|
*/
|
| 10 |
|
|
|
| 11 |
|
|
#include <stdio.h>
|
| 12 |
|
|
#include "lookup.h"
|
| 13 |
|
|
|
| 14 |
|
|
#define MEM_PTR char *
|
| 15 |
|
|
|
| 16 |
|
|
extern MEM_PTR calloc();
|
| 17 |
|
|
|
| 18 |
|
|
|
| 19 |
|
|
int
|
| 20 |
|
|
lu_init(tbl, nel) /* initialize tbl for at least nel elements */
|
| 21 |
|
|
register LUTAB *tbl;
|
| 22 |
|
|
int nel;
|
| 23 |
|
|
{
|
| 24 |
|
|
static int hsiztab[] = {
|
| 25 |
gregl |
2.3 |
31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
|
| 26 |
|
|
32749, 65521, 131071, 262139, 524287, 1048573, 2097143,
|
| 27 |
|
|
4194301, 8388593, 0
|
| 28 |
greg |
2.1 |
};
|
| 29 |
|
|
register int *hsp;
|
| 30 |
|
|
|
| 31 |
|
|
nel += nel>>1; /* 66% occupancy */
|
| 32 |
|
|
for (hsp = hsiztab; *hsp; hsp++)
|
| 33 |
|
|
if (*hsp > nel)
|
| 34 |
|
|
break;
|
| 35 |
|
|
if (!(tbl->tsiz = *hsp))
|
| 36 |
|
|
tbl->tsiz = nel*2 + 1; /* not always prime */
|
| 37 |
|
|
tbl->tabl = (LUENT *)calloc(tbl->tsiz, sizeof(LUENT));
|
| 38 |
|
|
if (tbl->tabl == NULL)
|
| 39 |
|
|
tbl->tsiz = 0;
|
| 40 |
|
|
tbl->ndel = 0;
|
| 41 |
|
|
return(tbl->tsiz);
|
| 42 |
|
|
}
|
| 43 |
|
|
|
| 44 |
|
|
|
| 45 |
|
|
long
|
| 46 |
|
|
lu_shash(s) /* hash a nul-terminated string */
|
| 47 |
gregl |
2.3 |
char *s;
|
| 48 |
greg |
2.1 |
{
|
| 49 |
gregl |
2.3 |
static unsigned char shuffle[256] = {
|
| 50 |
|
|
0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34,
|
| 51 |
|
|
191, 92, 249, 150, 51, 208, 109, 10, 167, 68, 225,
|
| 52 |
|
|
126, 27, 184, 85, 242, 143, 44, 201, 102, 3, 160,
|
| 53 |
|
|
61, 218, 119, 20, 177, 78, 235, 136, 37, 194, 95,
|
| 54 |
|
|
252, 153, 54, 211, 112, 13, 170, 71, 228, 129, 30,
|
| 55 |
|
|
187, 88, 245, 146, 47, 204, 105, 6, 163, 64, 221,
|
| 56 |
|
|
122, 23, 180, 81, 238, 139, 40, 197, 98, 255, 156,
|
| 57 |
|
|
57, 214, 115, 16, 173, 74, 231, 132, 33, 190, 91,
|
| 58 |
|
|
248, 149, 50, 207, 108, 9, 166, 67, 224, 125, 26,
|
| 59 |
|
|
183, 84, 241, 142, 43, 200, 101, 2, 159, 60, 217,
|
| 60 |
|
|
118, 19, 176, 77, 234, 135, 36, 193, 94, 251, 152,
|
| 61 |
|
|
53, 210, 111, 12, 169, 70, 227, 128, 29, 186, 87,
|
| 62 |
|
|
244, 145, 46, 203, 104, 5, 162, 63, 220, 121, 22,
|
| 63 |
|
|
179, 80, 237, 138, 39, 196, 97, 254, 155, 56, 213,
|
| 64 |
|
|
114, 15, 172, 73, 230, 131, 32, 189, 90, 247, 148,
|
| 65 |
|
|
49, 206, 107, 8, 165, 66, 223, 124, 25, 182, 83,
|
| 66 |
|
|
240, 141, 42, 199, 100, 1, 158, 59, 216, 117, 18,
|
| 67 |
|
|
175, 76, 233, 134, 35, 192, 93, 250, 151, 52, 209,
|
| 68 |
|
|
110, 11, 168, 69, 226, 127, 28, 185, 86, 243, 144,
|
| 69 |
|
|
45, 202, 103, 4, 161, 62, 219, 120, 21, 178, 79,
|
| 70 |
|
|
236, 137, 38, 195, 96, 253, 154, 55, 212, 113, 14,
|
| 71 |
|
|
171, 72, 229, 130, 31, 188, 89, 246, 147, 48, 205,
|
| 72 |
|
|
106, 7, 164, 65, 222, 123, 24, 181, 82, 239, 140,
|
| 73 |
|
|
41, 198, 99
|
| 74 |
|
|
};
|
| 75 |
greg |
2.1 |
register int i = 0;
|
| 76 |
|
|
register long h = 0;
|
| 77 |
gregl |
2.3 |
register unsigned char *t = (unsigned char *)s;
|
| 78 |
greg |
2.1 |
|
| 79 |
gregl |
2.3 |
while (*t)
|
| 80 |
|
|
h ^= (long)shuffle[*t++] << ((i+=11) & 0xf);
|
| 81 |
|
|
|
| 82 |
greg |
2.1 |
return(h);
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
|
| 86 |
|
|
LUENT *
|
| 87 |
|
|
lu_find(tbl, key) /* find a table entry */
|
| 88 |
|
|
register LUTAB *tbl;
|
| 89 |
|
|
char *key;
|
| 90 |
|
|
{
|
| 91 |
|
|
long hval;
|
| 92 |
gregl |
2.3 |
int i, n;
|
| 93 |
greg |
2.1 |
register int ndx;
|
| 94 |
gregl |
2.3 |
register LUENT *le;
|
| 95 |
greg |
2.1 |
/* look up object */
|
| 96 |
gregl |
2.3 |
if (tbl->tsiz == 0)
|
| 97 |
|
|
lu_init(tbl, 1);
|
| 98 |
greg |
2.1 |
hval = (*tbl->hashf)(key);
|
| 99 |
|
|
tryagain:
|
| 100 |
gregl |
2.3 |
ndx = hval % tbl->tsiz;
|
| 101 |
|
|
le = &tbl->tabl[ndx];
|
| 102 |
|
|
for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) {
|
| 103 |
|
|
if (le->key == NULL) {
|
| 104 |
|
|
le->hval = hval;
|
| 105 |
|
|
return(le);
|
| 106 |
greg |
2.1 |
}
|
| 107 |
gregl |
2.3 |
if (le->hval == hval &&
|
| 108 |
|
|
(tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) {
|
| 109 |
|
|
return(le);
|
| 110 |
|
|
}
|
| 111 |
|
|
le += n;
|
| 112 |
|
|
if ((ndx += n) >= tbl->tsiz) { /* this happens rarely */
|
| 113 |
|
|
ndx = ndx % tbl->tsiz;
|
| 114 |
|
|
le = &tbl->tabl[ndx];
|
| 115 |
|
|
}
|
| 116 |
greg |
2.1 |
}
|
| 117 |
|
|
/* table is full, reallocate */
|
| 118 |
gregl |
2.3 |
le = tbl->tabl;
|
| 119 |
greg |
2.1 |
ndx = tbl->tsiz;
|
| 120 |
|
|
i = tbl->ndel;
|
| 121 |
greg |
2.2 |
if (!lu_init(tbl, ndx-i+1)) { /* no more memory! */
|
| 122 |
gregl |
2.3 |
tbl->tabl = le;
|
| 123 |
greg |
2.1 |
tbl->tsiz = ndx;
|
| 124 |
|
|
tbl->ndel = i;
|
| 125 |
|
|
return(NULL);
|
| 126 |
|
|
}
|
| 127 |
|
|
if (!ndx)
|
| 128 |
|
|
goto tryagain;
|
| 129 |
|
|
/*
|
| 130 |
|
|
* The following code may fail if the user has reclaimed many
|
| 131 |
|
|
* deleted entries and the system runs out of memory in a
|
| 132 |
|
|
* recursive call to lu_find().
|
| 133 |
|
|
*/
|
| 134 |
|
|
while (ndx--)
|
| 135 |
gregl |
2.3 |
if (le[ndx].key != NULL)
|
| 136 |
|
|
if (le[ndx].data != NULL)
|
| 137 |
|
|
*lu_find(tbl, le[ndx].key) = le[ndx];
|
| 138 |
greg |
2.1 |
else if (tbl->freek != NULL)
|
| 139 |
gregl |
2.3 |
(*tbl->freek)(le[ndx].key);
|
| 140 |
|
|
free((MEM_PTR)le);
|
| 141 |
greg |
2.1 |
goto tryagain; /* should happen only once! */
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
|
| 145 |
|
|
void
|
| 146 |
|
|
lu_delete(tbl, key) /* delete a table entry */
|
| 147 |
|
|
register LUTAB *tbl;
|
| 148 |
|
|
char *key;
|
| 149 |
|
|
{
|
| 150 |
|
|
register LUENT *le;
|
| 151 |
|
|
|
| 152 |
|
|
if ((le = lu_find(tbl, key)) == NULL)
|
| 153 |
|
|
return;
|
| 154 |
|
|
if (le->key == NULL || le->data == NULL)
|
| 155 |
|
|
return;
|
| 156 |
|
|
if (tbl->freed != NULL)
|
| 157 |
|
|
(*tbl->freed)(le->data);
|
| 158 |
|
|
le->data = NULL;
|
| 159 |
|
|
tbl->ndel++;
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
|
| 163 |
|
|
void
|
| 164 |
|
|
lu_done(tbl) /* free table and contents */
|
| 165 |
|
|
register LUTAB *tbl;
|
| 166 |
|
|
{
|
| 167 |
|
|
register LUENT *tp;
|
| 168 |
|
|
|
| 169 |
|
|
if (!tbl->tsiz)
|
| 170 |
|
|
return;
|
| 171 |
|
|
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
|
| 172 |
|
|
if (tp->key != NULL) {
|
| 173 |
|
|
if (tbl->freek != NULL)
|
| 174 |
|
|
(*tbl->freek)(tp->key);
|
| 175 |
|
|
if (tp->data != NULL && tbl->freed != NULL)
|
| 176 |
|
|
(*tbl->freed)(tp->data);
|
| 177 |
|
|
}
|
| 178 |
|
|
free((MEM_PTR)tbl->tabl);
|
| 179 |
|
|
tbl->tabl = NULL;
|
| 180 |
|
|
tbl->tsiz = 0;
|
| 181 |
|
|
tbl->ndel = 0;
|
| 182 |
|
|
}
|