28 |
|
}; |
29 |
|
register int *hsp; |
30 |
|
|
31 |
< |
nel += nel>>2; /* 75% occupancy */ |
31 |
> |
nel += nel>>1; /* 66% occupancy */ |
32 |
|
for (hsp = hsiztab; *hsp; hsp++) |
33 |
|
if (*hsp > nel) |
34 |
|
break; |
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 |
|
|
61 |
|
{ |
62 |
|
int hval, i; |
63 |
|
register int ndx; |
63 |
– |
register LUENT *le; |
64 |
|
LUENT *oldtabl; |
65 |
|
/* look up object */ |
66 |
|
hval = lu_hash(key); |
74 |
|
/* table is full, reallocate */ |
75 |
|
oldtabl = tbl->tabl; |
76 |
|
ndx = tbl->tsiz; |
77 |
< |
if (!lu_init(tbl, ndx)) { /* no more memory! */ |
77 |
> |
i = tbl->ndel; |
78 |
> |
if (!lu_init(tbl, ndx-i)) { /* no more memory! */ |
79 |
|
tbl->tabl = oldtabl; |
80 |
|
tbl->tsiz = ndx; |
81 |
+ |
tbl->ndel = i; |
82 |
|
return(NULL); |
83 |
|
} |
84 |
|
if (!ndx) |
85 |
|
goto tryagain; |
86 |
|
while (ndx--) |
87 |
< |
if (oldtabl[ndx].key != NULL) { |
88 |
< |
le = lu_find(tbl, oldtabl[ndx].key); |
89 |
< |
le->key = oldtabl[ndx].key; |
90 |
< |
le->data = oldtabl[ndx].data; |
91 |
< |
} |
87 |
> |
if (oldtabl[ndx].key != NULL) |
88 |
> |
if (oldtabl[ndx].data != NULL) |
89 |
> |
*lu_find(tbl, oldtabl[ndx].key) = oldtabl[ndx]; |
90 |
> |
else if (tbl->freek != NULL) |
91 |
> |
(*tbl->freek)(oldtabl[ndx].key); |
92 |
|
free((MEM_PTR)oldtabl); |
93 |
|
goto tryagain; /* should happen only once! */ |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
void |
98 |
< |
lu_done(tbl, f) /* free table and contents */ |
99 |
< |
LUTAB *tbl; |
100 |
< |
int (*f)(); |
98 |
> |
lu_delete(tbl, key) /* delete a table entry */ |
99 |
> |
register LUTAB *tbl; |
100 |
> |
char *key; |
101 |
|
{ |
102 |
+ |
register LUENT *le; |
103 |
+ |
|
104 |
+ |
if ((le = lu_find(tbl, key)) == NULL) |
105 |
+ |
return; |
106 |
+ |
if (le->key == NULL || le->data == NULL) |
107 |
+ |
return; |
108 |
+ |
if (tbl->freed != NULL) |
109 |
+ |
(*tbl->freed)(le->data); |
110 |
+ |
le->data = NULL; |
111 |
+ |
tbl->ndel++; |
112 |
+ |
} |
113 |
+ |
|
114 |
+ |
|
115 |
+ |
void |
116 |
+ |
lu_done(tbl) /* free table and contents */ |
117 |
+ |
register LUTAB *tbl; |
118 |
+ |
{ |
119 |
|
register LUENT *tp; |
120 |
|
|
121 |
|
if (!tbl->tsiz) |
122 |
|
return; |
123 |
< |
if (f != NULL) |
124 |
< |
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
125 |
< |
if (tp->key != NULL) |
126 |
< |
(*f)(tp); |
123 |
> |
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
124 |
> |
if (tp->key != NULL) { |
125 |
> |
if (tbl->freek != NULL) |
126 |
> |
(*tbl->freek)(tp->key); |
127 |
> |
if (tp->data != NULL && tbl->freed != NULL) |
128 |
> |
(*tbl->freed)(tp->data); |
129 |
> |
} |
130 |
|
free((MEM_PTR)tbl->tabl); |
131 |
|
tbl->tabl = NULL; |
132 |
|
tbl->tsiz = 0; |
133 |
+ |
tbl->ndel = 0; |
134 |
|
} |