1 /* Copyright 1996,1997,2001,2002,2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
17 * hash.c - hash table.
20 #include "sysincludes.h"
27 int size; /* actual size of the array */
28 int fill; /* number of deleted or in use slots */
29 int inuse; /* number of slots in use */
30 int max; /* maximal number of elements to keep efficient */
31 T_HashTableEl *entries;
34 static int sizes[]={5, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421, 12853,
35 25717, 51437, 102877, 205759, 411527, 823117, 1646237,
36 3292489, 6584983, 13169977, 26339969, 52679969, 105359939,
37 210719881, 421439783, 842879579, 1685759167, 0 };
39 static int unallocated=0;
41 static int alloc_ht(T_HashTable *H, int size)
45 for(i=0; sizes[i]; i++)
46 if (sizes[i] > size*4 )
49 for(i=0; sizes[i]; i++)
50 if (sizes[i] > size*2 )
53 for(i=0; sizes[i]; i++)
60 size = H->size; /* never shrink the table */
61 H->max = size * 4 / 5 - 2;
65 H->entries = NewArray(size, T_HashTableEl);
66 if (H->entries == NULL)
67 return -1; /* out of memory error */
69 for(i=0; i < size; i++)
70 H->entries[i] = &unallocated;
74 int make_ht(T_HashFunc f1, T_HashFunc f2, T_ComparFunc c, int size,
77 *H = New(T_HashTable);
79 return -1; /* out of memory error */
91 int free_ht(T_HashTable *H, T_HashFunc entry_free)
95 for(i=0; i< H->size; i++)
96 if (H->entries[i] != &unallocated &&
97 H->entries[i] != &deleted)
98 entry_free(H->entries[i]);
104 /* add into hash table without checking for repeats */
105 static int _hash_add(T_HashTable *H,T_HashTableEl *E, int *hint)
109 pos = H->f1(E) % H->size;
112 while(H->entries[pos] != &unallocated &&
113 H->entries[pos] != &deleted){
115 f2 = H->f2(E) % (H->size - 1);
116 pos = (pos+f2+1) % H->size;
119 if(H->entries[pos] == &unallocated)
120 H->fill++; /* only increase fill if the previous element was not yet
121 * counted, i.e. unallocated */
129 static int rehash(T_HashTable *H)
132 T_HashTableEl *oldentries;
133 /* resize the table */
136 oldentries = H->entries;
137 if(alloc_ht(H,((H->inuse+1)*4+H->fill)/5))
140 for(i=0; i < size; i++){
141 if(oldentries[i] != &unallocated && oldentries[i] != &deleted)
142 _hash_add(H, oldentries[i], 0);
148 int hash_add(T_HashTable *H, T_HashTableEl *E, int *hint)
150 if (H->fill >= H->max)
152 if (H->fill == H->size)
153 return -1; /*out of memory error */
154 return _hash_add(H,E, hint);
158 /* add into hash table without checking for repeats */
159 static int _hash_lookup(T_HashTable *H,T_HashTableEl *E, T_HashTableEl **E2,
160 int *hint, int isIdentity)
162 int f2, pos, upos, ttl;
164 pos = H->f1(E) % H->size;
169 H->entries[pos] != &unallocated &&
170 (H->entries[pos] == &deleted ||
171 ((isIdentity || H->compar(H->entries[pos], E) != 0) &&
172 (!isIdentity || H->entries[pos] != E)))){
174 f2 = H->f2(E) % (H->size - 1);
175 if (upos == -1 && H->entries[pos] == &deleted)
177 pos = (pos+f2+1) % H->size;
180 if(H->entries[pos] == &unallocated || !ttl)
183 H->entries[upos] = H->entries[pos];
184 H->entries[pos] = &deleted;
189 *E2= H->entries[pos];
194 int hash_lookup(T_HashTable *H,T_HashTableEl *E, T_HashTableEl **E2,
197 return _hash_lookup(H, E, E2, hint, 0);
200 /* add into hash table without checking for repeats */
201 int hash_remove(T_HashTable *H,T_HashTableEl *E, int hint)
205 if (hint >=0 && hint < H->size &&
206 H->entries[hint] == E){
208 H->entries[hint] = &deleted;
212 if(_hash_lookup(H, E, &E2, &hint, 1)) {
213 fprintf(stderr, "Removing non-existent entry\n");
218 H->entries[hint] = &deleted;