1*d5c9a868SElliott Hughes /* Copyright 1996,1997,2001,2002,2009 Alain Knaff.
2*d5c9a868SElliott Hughes * This file is part of mtools.
3*d5c9a868SElliott Hughes *
4*d5c9a868SElliott Hughes * Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes * it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes * the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes * (at your option) any later version.
8*d5c9a868SElliott Hughes *
9*d5c9a868SElliott Hughes * Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*d5c9a868SElliott Hughes * GNU General Public License for more details.
13*d5c9a868SElliott Hughes *
14*d5c9a868SElliott Hughes * You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes *
17*d5c9a868SElliott Hughes * hash.c - hash table.
18*d5c9a868SElliott Hughes */
19*d5c9a868SElliott Hughes
20*d5c9a868SElliott Hughes #include "sysincludes.h"
21*d5c9a868SElliott Hughes #include "htable.h"
22*d5c9a868SElliott Hughes #include "mtools.h"
23*d5c9a868SElliott Hughes
24*d5c9a868SElliott Hughes struct hashtable {
25*d5c9a868SElliott Hughes T_HashFunc f1,f2;
26*d5c9a868SElliott Hughes T_ComparFunc compar;
27*d5c9a868SElliott Hughes size_t size; /* actual size of the array */
28*d5c9a868SElliott Hughes size_t fill; /* number of deleted or in use slots */
29*d5c9a868SElliott Hughes size_t inuse; /* number of slots in use */
30*d5c9a868SElliott Hughes size_t max; /* maximal number of elements to keep efficient */
31*d5c9a868SElliott Hughes T_HashTableEl *entries;
32*d5c9a868SElliott Hughes };
33*d5c9a868SElliott Hughes
34*d5c9a868SElliott Hughes static size_t sizes[]=
35*d5c9a868SElliott Hughes { 5, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421, 12853,
36*d5c9a868SElliott Hughes 25717, 51437, 102877, 205759, 411527, 823117, 1646237,
37*d5c9a868SElliott Hughes 3292489, 6584983, 13169977, 26339969, 52679969, 105359939,
38*d5c9a868SElliott Hughes 210719881, 421439783, 842879579, 1685759167, 0 };
39*d5c9a868SElliott Hughes static int deleted=0;
40*d5c9a868SElliott Hughes static int unallocated=0;
41*d5c9a868SElliott Hughes
alloc_ht(T_HashTable * H,size_t size)42*d5c9a868SElliott Hughes static int alloc_ht(T_HashTable *H, size_t size)
43*d5c9a868SElliott Hughes {
44*d5c9a868SElliott Hughes int i;
45*d5c9a868SElliott Hughes size_t ii;
46*d5c9a868SElliott Hughes
47*d5c9a868SElliott Hughes for(i=0; sizes[i]; i++)
48*d5c9a868SElliott Hughes if (sizes[i] > size*4 )
49*d5c9a868SElliott Hughes break;
50*d5c9a868SElliott Hughes if (!sizes[i])
51*d5c9a868SElliott Hughes for(i=0; sizes[i]; i++)
52*d5c9a868SElliott Hughes if (sizes[i] > size*2 )
53*d5c9a868SElliott Hughes break;
54*d5c9a868SElliott Hughes if (!sizes[i])
55*d5c9a868SElliott Hughes for(i=0; sizes[i]; i++)
56*d5c9a868SElliott Hughes if (sizes[i] > size)
57*d5c9a868SElliott Hughes break;
58*d5c9a868SElliott Hughes if(!sizes[i])
59*d5c9a868SElliott Hughes return -1;
60*d5c9a868SElliott Hughes size = sizes[i];
61*d5c9a868SElliott Hughes if(size < H->size)
62*d5c9a868SElliott Hughes size = H->size; /* never shrink the table */
63*d5c9a868SElliott Hughes H->max = size * 4 / 5 - 2;
64*d5c9a868SElliott Hughes H->size = size;
65*d5c9a868SElliott Hughes H->fill = 0;
66*d5c9a868SElliott Hughes H->inuse = 0;
67*d5c9a868SElliott Hughes H->entries = NewArray(size, T_HashTableEl);
68*d5c9a868SElliott Hughes if (H->entries == NULL)
69*d5c9a868SElliott Hughes return -1; /* out of memory error */
70*d5c9a868SElliott Hughes
71*d5c9a868SElliott Hughes for(ii=0; ii < size; ii++)
72*d5c9a868SElliott Hughes H->entries[ii] = &unallocated;
73*d5c9a868SElliott Hughes return 0;
74*d5c9a868SElliott Hughes }
75*d5c9a868SElliott Hughes
make_ht(T_HashFunc f1,T_HashFunc f2,T_ComparFunc c,size_t size,T_HashTable ** H)76*d5c9a868SElliott Hughes int make_ht(T_HashFunc f1, T_HashFunc f2, T_ComparFunc c, size_t size,
77*d5c9a868SElliott Hughes T_HashTable **H)
78*d5c9a868SElliott Hughes {
79*d5c9a868SElliott Hughes *H = New(T_HashTable);
80*d5c9a868SElliott Hughes if (*H == NULL){
81*d5c9a868SElliott Hughes return -1; /* out of memory error */
82*d5c9a868SElliott Hughes }
83*d5c9a868SElliott Hughes
84*d5c9a868SElliott Hughes (*H)->f1 = f1;
85*d5c9a868SElliott Hughes (*H)->f2 = f2;
86*d5c9a868SElliott Hughes (*H)->compar = c;
87*d5c9a868SElliott Hughes (*H)->size = 0;
88*d5c9a868SElliott Hughes if(alloc_ht(*H,size))
89*d5c9a868SElliott Hughes return -1;
90*d5c9a868SElliott Hughes return 0;
91*d5c9a868SElliott Hughes }
92*d5c9a868SElliott Hughes
free_ht(T_HashTable * H,T_HashFunc entry_free)93*d5c9a868SElliott Hughes int free_ht(T_HashTable *H, T_HashFunc entry_free)
94*d5c9a868SElliott Hughes {
95*d5c9a868SElliott Hughes size_t i;
96*d5c9a868SElliott Hughes if(entry_free)
97*d5c9a868SElliott Hughes for(i=0; i< H->size; i++)
98*d5c9a868SElliott Hughes if (H->entries[i] != &unallocated &&
99*d5c9a868SElliott Hughes H->entries[i] != &deleted)
100*d5c9a868SElliott Hughes entry_free(H->entries[i]);
101*d5c9a868SElliott Hughes Free(H->entries);
102*d5c9a868SElliott Hughes Free(H);
103*d5c9a868SElliott Hughes return 0;
104*d5c9a868SElliott Hughes }
105*d5c9a868SElliott Hughes
106*d5c9a868SElliott Hughes /* add into hash table without checking for repeats */
_hash_add(T_HashTable * H,T_HashTableEl * E,size_t * hint)107*d5c9a868SElliott Hughes static int _hash_add(T_HashTable *H,T_HashTableEl *E, size_t *hint)
108*d5c9a868SElliott Hughes {
109*d5c9a868SElliott Hughes size_t f2, pos;
110*d5c9a868SElliott Hughes int ctr;
111*d5c9a868SElliott Hughes
112*d5c9a868SElliott Hughes pos = H->f1(E) % H->size;
113*d5c9a868SElliott Hughes f2 = (size_t) -1;
114*d5c9a868SElliott Hughes ctr = 0;
115*d5c9a868SElliott Hughes while(H->entries[pos] != &unallocated &&
116*d5c9a868SElliott Hughes H->entries[pos] != &deleted){
117*d5c9a868SElliott Hughes if (f2 == (size_t) -1)
118*d5c9a868SElliott Hughes f2 = H->f2(E) % (H->size - 1);
119*d5c9a868SElliott Hughes pos = (pos+f2+1) % H->size;
120*d5c9a868SElliott Hughes ctr++;
121*d5c9a868SElliott Hughes }
122*d5c9a868SElliott Hughes if(H->entries[pos] == &unallocated)
123*d5c9a868SElliott Hughes H->fill++; /* only increase fill if the previous element was not yet
124*d5c9a868SElliott Hughes * counted, i.e. unallocated */
125*d5c9a868SElliott Hughes H->inuse++;
126*d5c9a868SElliott Hughes H->entries[pos] = E;
127*d5c9a868SElliott Hughes if(hint)
128*d5c9a868SElliott Hughes *hint = pos;
129*d5c9a868SElliott Hughes return 0;
130*d5c9a868SElliott Hughes }
131*d5c9a868SElliott Hughes
rehash(T_HashTable * H)132*d5c9a868SElliott Hughes static int rehash(T_HashTable *H)
133*d5c9a868SElliott Hughes {
134*d5c9a868SElliott Hughes size_t size,i;
135*d5c9a868SElliott Hughes T_HashTableEl *oldentries;
136*d5c9a868SElliott Hughes /* resize the table */
137*d5c9a868SElliott Hughes
138*d5c9a868SElliott Hughes size = H->size;
139*d5c9a868SElliott Hughes oldentries = H->entries;
140*d5c9a868SElliott Hughes if(alloc_ht(H,((H->inuse+1)*4+H->fill)/5))
141*d5c9a868SElliott Hughes return -1;
142*d5c9a868SElliott Hughes
143*d5c9a868SElliott Hughes for(i=0; i < size; i++){
144*d5c9a868SElliott Hughes if(oldentries[i] != &unallocated && oldentries[i] != &deleted)
145*d5c9a868SElliott Hughes _hash_add(H, oldentries[i], 0);
146*d5c9a868SElliott Hughes }
147*d5c9a868SElliott Hughes Free(oldentries);
148*d5c9a868SElliott Hughes return 0;
149*d5c9a868SElliott Hughes }
150*d5c9a868SElliott Hughes
hash_add(T_HashTable * H,T_HashTableEl * E,size_t * hint)151*d5c9a868SElliott Hughes int hash_add(T_HashTable *H, T_HashTableEl *E, size_t *hint)
152*d5c9a868SElliott Hughes {
153*d5c9a868SElliott Hughes if (H->fill >= H->max)
154*d5c9a868SElliott Hughes rehash(H);
155*d5c9a868SElliott Hughes if (H->fill == H->size)
156*d5c9a868SElliott Hughes return -1; /*out of memory error */
157*d5c9a868SElliott Hughes return _hash_add(H,E, hint);
158*d5c9a868SElliott Hughes }
159*d5c9a868SElliott Hughes
160*d5c9a868SElliott Hughes
161*d5c9a868SElliott Hughes /* add into hash table without checking for repeats */
_hash_lookup(T_HashTable * H,T_HashTableEl * E,T_HashTableEl ** E2,size_t * hint,int isIdentity)162*d5c9a868SElliott Hughes static int _hash_lookup(T_HashTable *H,T_HashTableEl *E, T_HashTableEl **E2,
163*d5c9a868SElliott Hughes size_t *hint, int isIdentity)
164*d5c9a868SElliott Hughes {
165*d5c9a868SElliott Hughes size_t f2, pos, upos, ttl;
166*d5c9a868SElliott Hughes
167*d5c9a868SElliott Hughes pos = H->f1(E) % H->size;
168*d5c9a868SElliott Hughes ttl = H->size;
169*d5c9a868SElliott Hughes f2 = (size_t) -1;
170*d5c9a868SElliott Hughes upos = (size_t) -1;
171*d5c9a868SElliott Hughes while(ttl &&
172*d5c9a868SElliott Hughes H->entries[pos] != &unallocated &&
173*d5c9a868SElliott Hughes (H->entries[pos] == &deleted ||
174*d5c9a868SElliott Hughes ((isIdentity || H->compar(H->entries[pos], E) != 0) &&
175*d5c9a868SElliott Hughes (!isIdentity || H->entries[pos] != E)))){
176*d5c9a868SElliott Hughes if (f2 == (size_t) -1)
177*d5c9a868SElliott Hughes f2 = H->f2(E) % (H->size - 1);
178*d5c9a868SElliott Hughes if (upos == (size_t) -1 && H->entries[pos] == &deleted)
179*d5c9a868SElliott Hughes upos = pos;
180*d5c9a868SElliott Hughes pos = (pos+f2+1) % H->size;
181*d5c9a868SElliott Hughes ttl--;
182*d5c9a868SElliott Hughes }
183*d5c9a868SElliott Hughes if(H->entries[pos] == &unallocated || !ttl)
184*d5c9a868SElliott Hughes return -1;
185*d5c9a868SElliott Hughes if (upos != (size_t) -1){
186*d5c9a868SElliott Hughes H->entries[upos] = H->entries[pos];
187*d5c9a868SElliott Hughes H->entries[pos] = &deleted;
188*d5c9a868SElliott Hughes pos = upos;
189*d5c9a868SElliott Hughes }
190*d5c9a868SElliott Hughes if(hint)
191*d5c9a868SElliott Hughes *hint = pos;
192*d5c9a868SElliott Hughes *E2= H->entries[pos];
193*d5c9a868SElliott Hughes return 0;
194*d5c9a868SElliott Hughes }
195*d5c9a868SElliott Hughes
196*d5c9a868SElliott Hughes
hash_lookup(T_HashTable * H,T_HashTableEl * E,T_HashTableEl ** E2,size_t * hint)197*d5c9a868SElliott Hughes int hash_lookup(T_HashTable *H,T_HashTableEl *E, T_HashTableEl **E2,
198*d5c9a868SElliott Hughes size_t *hint)
199*d5c9a868SElliott Hughes {
200*d5c9a868SElliott Hughes return _hash_lookup(H, E, E2, hint, 0);
201*d5c9a868SElliott Hughes }
202*d5c9a868SElliott Hughes
203*d5c9a868SElliott Hughes /* add into hash table without checking for repeats */
hash_remove(T_HashTable * H,T_HashTableEl * E,size_t hint)204*d5c9a868SElliott Hughes int hash_remove(T_HashTable *H,T_HashTableEl *E, size_t hint)
205*d5c9a868SElliott Hughes {
206*d5c9a868SElliott Hughes T_HashTableEl *E2;
207*d5c9a868SElliott Hughes
208*d5c9a868SElliott Hughes if (hint < H->size && H->entries[hint] == E){
209*d5c9a868SElliott Hughes H->inuse--;
210*d5c9a868SElliott Hughes H->entries[hint] = &deleted;
211*d5c9a868SElliott Hughes return 0;
212*d5c9a868SElliott Hughes }
213*d5c9a868SElliott Hughes
214*d5c9a868SElliott Hughes if(_hash_lookup(H, E, &E2, &hint, 1)) {
215*d5c9a868SElliott Hughes fprintf(stderr, "Removing non-existent entry\n");
216*d5c9a868SElliott Hughes exit(1);
217*d5c9a868SElliott Hughes }
218*d5c9a868SElliott Hughes H->inuse--;
219*d5c9a868SElliott Hughes H->entries[hint] = &deleted;
220*d5c9a868SElliott Hughes return 0;
221*d5c9a868SElliott Hughes }
222