Line data Source code
1 : /* Copyright (C) 2000 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 : #include "pari.h"
15 : #include "paripriv.h"
16 :
17 : /********************************************************************/
18 : /* */
19 : /* GENERAL HASHTABLES */
20 : /* */
21 : /********************************************************************/
22 : /* http://planetmath.org/encyclopedia/GoodHashTablePrimes.html */
23 : static const ulong hashprimes[] = {
24 : 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613,
25 : 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653,
26 : 100663319, 201326611, 402653189, 805306457, 1610612741
27 : };
28 : static const int hashprimes_len = numberof(hashprimes);
29 :
30 : INLINE void
31 51925 : setlen(hashtable *h, ulong len) {
32 51925 : h->maxnb = (ulong)ceil(len * 0.65);
33 51925 : h->len = len;
34 51925 : }
35 :
36 : static int
37 47932 : get_prime_index(ulong len)
38 : {
39 : int i;
40 74068 : for (i=0; i < hashprimes_len; i++)
41 74068 : if (hashprimes[i] > len) return i;
42 0 : pari_err_OVERFLOW("hash table [too large]");
43 : return -1; /* LCOV_EXCL_LINE */
44 : }
45 :
46 : /* link hashentry e to hashtable h, setting e->hash / e->next */
47 : INLINE void
48 1504477 : hash_link2(hashtable *h, hashentry *e, ulong hash)
49 : {
50 : ulong index;
51 1504477 : e->hash = hash; index = e->hash % h->len;
52 1504477 : e->next = h->table[index]; h->table[index] = e;
53 1504477 : }
54 : INLINE void
55 14498 : hash_link(hashtable *h, hashentry *e) { hash_link2(h,e,h->hash(e->key));}
56 :
57 : hashtable *
58 34793 : hash_create(ulong minsize, ulong (*hash)(void*), int (*eq)(void*,void*),
59 : int use_stack)
60 : {
61 34793 : int i = get_prime_index(minsize);
62 34793 : ulong len = hashprimes[i];
63 : hashtable *h;
64 :
65 34793 : if (use_stack)
66 : {
67 31515 : h = (hashtable*)stack_malloc(sizeof(hashtable));
68 31515 : h->table = (hashentry**)stack_calloc(len * sizeof(hashentry*));
69 31515 : h->use_stack = 1;
70 : }
71 : else
72 : {
73 3278 : h = (hashtable*)pari_malloc(sizeof(hashtable));
74 3278 : h->table = (hashentry**)pari_calloc(len * sizeof(hashentry*));
75 3278 : h->use_stack = 0;
76 : }
77 34793 : h->pindex = i;
78 34793 : h->nb = 0;
79 34793 : h->hash = hash;
80 34793 : h->eq = eq;
81 34793 : setlen(h, len); return h;
82 : }
83 : static ulong
84 521484 : hash_id(void *x) { return (ulong)x; }
85 : static int
86 258666 : eq_id(void *x, void *y) { return x == y; }
87 : hashtable *
88 918 : hash_create_ulong(ulong s, long stack)
89 918 : { return hash_create(s, &hash_id, &eq_id, stack); }
90 :
91 : void
92 13139 : hash_init(hashtable *h, ulong minsize, ulong (*hash)(void*),
93 : int (*eq)(void*,void*), int use_stack)
94 : {
95 13139 : int i = get_prime_index(minsize);
96 13139 : ulong len = hashprimes[i];
97 13139 : if (use_stack)
98 13139 : h->table = (hashentry**)stack_calloc(len * sizeof(hashentry*));
99 : else
100 0 : h->table = (hashentry**)pari_calloc(len * sizeof(hashentry*));
101 13139 : h->use_stack = use_stack;
102 13139 : h->pindex = i;
103 13139 : h->nb = 0;
104 13139 : h->hash = hash;
105 13139 : h->eq = eq;
106 13139 : setlen(h, len);
107 13139 : }
108 :
109 : void
110 10013 : hash_init_GEN(hashtable *h, ulong minsize, int (*eq)(GEN,GEN), int use_stack)
111 10013 : { hash_init(h, minsize,(ulong (*)(void*)) hash_GEN,
112 : (int (*)(void*,void*)) eq, use_stack);
113 10013 : }
114 :
115 : void
116 3126 : hash_init_ulong(hashtable *h, ulong minsize, int use_stack)
117 3126 : { hash_init(h, minsize,hash_id, eq_id, use_stack); }
118 :
119 : void
120 1489979 : hash_insert2(hashtable *h, void *k, void *v, ulong hash)
121 : {
122 : hashentry *e;
123 : ulong index;
124 :
125 1489979 : if (h->use_stack)
126 1481927 : e = (hashentry*) stack_malloc(sizeof(hashentry));
127 : else
128 8052 : e = (hashentry*) pari_malloc(sizeof(hashentry));
129 :
130 1489979 : if (++(h->nb) > h->maxnb && h->pindex < hashprimes_len-1)
131 : { /* double table size */
132 3993 : ulong i, newlen = hashprimes[++(h->pindex)];
133 : hashentry *E, **newtable;
134 3993 : if (h->use_stack)
135 3993 : newtable = (hashentry**)stack_calloc(newlen*sizeof(hashentry*));
136 : else
137 0 : newtable = (hashentry**)pari_calloc(newlen*sizeof(hashentry*));
138 1144902 : for (i = 0; i < h->len; i++)
139 1884869 : while ( (E = h->table[i]) )
140 : {
141 743960 : h->table[i] = E->next;
142 743960 : index = E->hash % newlen;
143 743960 : E->next = newtable[index];
144 743960 : newtable[index] = E;
145 : }
146 3993 : if (!h->use_stack) pari_free(h->table);
147 3993 : h->table = newtable;
148 3993 : setlen(h, newlen);
149 : }
150 1489979 : e->key = k;
151 1489979 : e->val = v; hash_link2(h, e, hash);
152 1489979 : }
153 : void
154 658497 : hash_insert(hashtable *h, void *k, void *v)
155 658497 : { hash_insert2(h,k,v,h->hash(k)); }
156 :
157 : void
158 46907 : hash_insert_long(hashtable *h, void *k, long v)
159 46907 : { hash_insert2(h,k,(void*)v,h->hash(k)); }
160 :
161 : /* the key 'k' may correspond to different values in the hash, return
162 : * one satisfying the selection callback */
163 : hashentry *
164 66 : hash_select(hashtable *h, void *k, void *E,int(*select)(void *,hashentry *))
165 : {
166 66 : ulong hash = h->hash(k);
167 66 : hashentry *e = h->table[ hash % h->len ];
168 126 : while (e)
169 : {
170 78 : if (hash == e->hash && h->eq(k, e->key) && select(E,e)) return e;
171 60 : e = e->next;
172 : }
173 48 : return NULL;
174 : }
175 :
176 : GEN
177 922 : hash_keys(hashtable *h)
178 : {
179 922 : long k = 1;
180 : ulong i;
181 922 : GEN v = cgetg(h->nb+1, t_VECSMALL);
182 178308 : for (i = 0; i < h->len; i++)
183 : {
184 177386 : hashentry *e = h->table[i];
185 178124 : while (e) { v[k++] = (long)e->key; e = e->next; }
186 : }
187 922 : return v;
188 : }
189 :
190 : GEN
191 3327 : hash_keys_GEN(hashtable *h)
192 : {
193 3327 : long k = 1;
194 : ulong i;
195 3327 : GEN v = cgetg(h->nb+1, t_VEC);
196 761206 : for (i = 0; i < h->len; i++)
197 : {
198 757879 : hashentry *e = h->table[i];
199 1086759 : while (e) { gel(v,k++) = (GEN)e->key; e = e->next; }
200 : }
201 3327 : return v;
202 : }
203 :
204 : GEN
205 1738 : hash_values(hashtable *h)
206 : {
207 1738 : long k = 1;
208 : ulong i;
209 1738 : GEN v = cgetg(h->nb+1, t_VECSMALL);
210 337172 : for (i = 0; i < h->len; i++)
211 : {
212 335434 : hashentry *e = h->table[i];
213 342448 : while (e) { v[k++] = (long)e->val; e = e->next; }
214 : }
215 1738 : return v;
216 : }
217 :
218 : /* assume hash = h->hash(k) */
219 : hashentry *
220 2962325 : hash_search2(hashtable *h, void *k, ulong hash)
221 : {
222 2962325 : hashentry *e = h->table[ hash % h->len ];
223 3723097 : while (e)
224 : {
225 2431191 : if (hash == e->hash && h->eq(k, e->key)) return e;
226 760772 : e = e->next;
227 : }
228 1291906 : return NULL; /* not found */
229 : }
230 : /* returns entry attached to key k or NULL */
231 : hashentry *
232 1403307 : hash_search(hashtable *h, void *k)
233 : {
234 1403307 : if (h->nb == 0) return NULL;
235 1398770 : return hash_search2(h, k, h->hash(k));
236 : }
237 :
238 : int
239 287579 : hash_haskey_long(hashtable *h, void *k, long *v)
240 : {
241 287579 : hashentry * e = hash_search(h, k);
242 287579 : if (e) { *v = (long) e->val; return 1; }
243 41261 : else return 0;
244 : }
245 :
246 : GEN
247 111896 : hash_haskey_GEN(hashtable *h, void *k)
248 : {
249 111896 : hashentry * e = hash_search(h, k);
250 111896 : return e ? (GEN) e->val: NULL;
251 : }
252 :
253 : hashentry *
254 2580 : hash_remove_select(hashtable *h, void *k, void *E,
255 : int (*select)(void*,hashentry*))
256 : {
257 2580 : ulong hash = h->hash(k), index = hash % h->len;
258 2580 : hashentry **pE = &(h->table[index]), *e = *pE;
259 2580 : while (e)
260 : {
261 2580 : if (hash == e->hash && h->eq(k, e->key) && select(E,e)) {
262 2580 : *pE = e->next; h->nb--;
263 2580 : return e;
264 : }
265 0 : pE = &(e->next);
266 0 : e = e->next;
267 : }
268 0 : return NULL;
269 : }
270 :
271 : hashentry *
272 16 : hash_remove(hashtable *h, void *k)
273 : {
274 16 : ulong hash = h->hash(k), index = hash % h->len;
275 16 : hashentry **pE = &(h->table[index]), *e = *pE;
276 16 : while (e)
277 : {
278 16 : if (hash == e->hash && h->eq(k, e->key)) {
279 16 : *pE = e->next; h->nb--;
280 16 : return e;
281 : }
282 0 : pE = &(e->next);
283 0 : e = e->next;
284 : }
285 0 : return NULL;
286 : }
287 : void
288 3236 : hash_destroy(hashtable *h)
289 : {
290 : ulong i;
291 3236 : if (h->use_stack) return;
292 401264 : for (i = 0; i < h->len; i++)
293 : {
294 398028 : hashentry *e = h->table[i];
295 403464 : while (e) { hashentry *f = e; e = e->next; pari_free(f); }
296 : }
297 3236 : pari_free(h->table); pari_free(h);
298 : }
299 :
300 : static
301 14896 : int strequal(void *a, void *b) { return !strcmp((char*)a,(char*)b); }
302 : hashtable *
303 3278 : hash_create_str(ulong s, long stack)
304 3278 : { return hash_create(s, (ulong (*)(void *))&hash_str, strequal, stack); }
305 :
306 : hashtable *
307 22 : hashstr_import_static(hashentry *e, ulong size)
308 : {
309 22 : hashtable *h = hash_create_str(size, 0);
310 14520 : for ( ; e->key; e++) { hash_link(h, e); h->nb++; }
311 22 : return h;
312 : }
313 :
314 : void
315 0 : hash_dbg(hashtable *h)
316 : {
317 0 : ulong n, Total = 0, Max = 0;
318 0 : hashentry *e, **table = h->table;
319 0 : for (n=0; n < h->len; n++)
320 : {
321 0 : ulong m=0;
322 0 : for (e=table[n]; e; e=e->next) m++;
323 0 : Total += m; if (Max < m) Max = m;
324 0 : pari_printf("%4ld:%2ld ",n,m);
325 0 : if (n%9 == 8) pari_putc('\n');
326 : }
327 0 : pari_printf("\nTotal = %ld, Max = %ld\n", Total, Max);
328 0 : }
329 :
330 : /********************************************************************/
331 : /* */
332 : /* HASH FUNCTIONS */
333 : /* */
334 : /********************************************************************/
335 :
336 : INLINE ulong
337 457298429 : glue(ulong h, ulong a) { return 404936533*h + a; }
338 : ulong
339 116790097 : hash_GEN(GEN x)
340 : {
341 116790097 : ulong h = x[0] & ~CLONEBIT;
342 116790097 : long tx = typ(x), lx, i;
343 116790097 : switch(tx)
344 : { /* non recursive types */
345 43883543 : case t_INT:
346 43883543 : lx = lgefint(x);
347 43883543 : h &= TYPBITS;
348 136024423 : for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
349 43883543 : return h;
350 57670588 : case t_REAL:
351 : case t_STR:
352 : case t_VECSMALL:
353 57670588 : lx = lg(x);
354 376422774 : for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
355 57670588 : return h;
356 : /* one more special case */
357 0 : case t_LIST:
358 0 : x = list_data(x);
359 0 : if (!x) return h;
360 : /* fall through */
361 : default:
362 15235966 : lx = lg(x);
363 16324473 : for(i = 1; i < lontyp[tx]; i++) h = glue(h, x[i]);
364 60473268 : for (; i < lx; i++) h = glue(h, hash_GEN(gel(x,i)));
365 15235966 : return h;
366 : }
367 : }
368 : ulong
369 15006 : hash_zv(GEN x)
370 : {
371 15006 : long i, lx = lg(x);
372 : ulong h;
373 15006 : if (lx == 1) return 0;
374 13458 : h = x[1];
375 93012 : for (i = 1; i < lx; i++) h = glue(h, uel(x,i));
376 13458 : return h;
377 : }
|