1 /* $Header: hash.c,v 3.0.1.4 90/08/09 03:50:22 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.4 90/08/09 03:50:22 lwall
10 * patch19: dbmopen(name, 'filename', undef) now refrains from creating
12 * Revision 3.0.1.3 90/03/27 15:59:09 lwall
13 * patch16: @dbmvalues{'foo','bar'} could use the same cache entry for both values
15 * Revision 3.0.1.2 89/12/21 20:03:39 lwall
16 * patch7: errno may now be a macro with an lvalue
18 * Revision 3.0.1.1 89/11/11 04:34:18 lwall
19 * patch2: CX/UX needed to set the key each time in associative iterators
21 * Revision 3.0 89/10/18 15:18:32 lwall
29 static char coeff[] = {
30 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
31 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
32 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
33 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
34 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
35 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
36 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1,
37 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1};
40 hfetch(tb,key,klen,lval)
59 /* The hash function we use on symbols has to be equal to the first
60 * character when taken modulo 128, so that str_reset() can be implemented
61 * efficiently. We throw in the second character and the last character
62 * (times 128) so that long chains of identifiers starting with the
63 * same letter don't have to be strEQ'ed within hfetch(), since it
64 * compares hash values before trying strEQ().
66 if (!tb->tbl_coeffsize)
67 hash = *key + 128 * key[1] + 128 * key[klen-1]; /* assuming klen > 0 */
68 else { /* use normal coefficients */
69 if (klen < tb->tbl_coeffsize)
72 maxi = tb->tbl_coeffsize;
73 for (s=key, i=0, hash = 0;
75 s++, i++, hash *= 5) {
76 hash += *s * coeff[i];
80 entry = tb->tbl_array[hash & tb->tbl_max];
81 for (; entry; entry = entry->hent_next) {
82 if (entry->hent_hash != hash) /* strings can't be equal */
84 if (entry->hent_klen != klen)
86 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
88 return entry->hent_val;
94 dcontent = dbm_fetch(tb->tbl_dbm,dkey);
95 if (dcontent.dptr) { /* found one */
96 str = Str_new(60,dcontent.dsize);
97 str_nset(str,dcontent.dptr,dcontent.dsize);
98 hstore(tb,key,klen,str,hash); /* cache it */
103 if (lval) { /* gonna assign to this, so it better be there */
105 hstore(tb,key,klen,str,hash);
112 hstore(tb,key,klen,val,hash)
121 register HENT *entry;
122 register HENT **oentry;
130 else if (!tb->tbl_coeffsize)
131 hash = *key + 128 * key[1] + 128 * key[klen-1];
132 else { /* use normal coefficients */
133 if (klen < tb->tbl_coeffsize)
136 maxi = tb->tbl_coeffsize;
137 for (s=key, i=0, hash = 0;
139 s++, i++, hash *= 5) {
140 hash += *s * coeff[i];
144 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
147 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
148 if (entry->hent_hash != hash) /* strings can't be equal */
150 if (entry->hent_klen != klen)
152 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
154 Safefree(entry->hent_val);
155 entry->hent_val = val;
158 New(501,entry, 1, HENT);
160 entry->hent_klen = klen;
161 entry->hent_key = nsavestr(key,klen);
162 entry->hent_val = val;
163 entry->hent_hash = hash;
164 entry->hent_next = *oentry;
167 /* hdbmstore not necessary here because it's called from stabset() */
169 if (i) { /* initial entry? */
172 if (tb->tbl_dbm && tb->tbl_max >= DBM_CACHE_MAX)
175 if (tb->tbl_fill > tb->tbl_dosplit)
179 else if (tb->tbl_dbm) { /* is this just a cache for dbm file? */
180 void hentdelayfree();
182 entry = tb->tbl_array[hash & tb->tbl_max];
183 oentry = &entry->hent_next;
185 while (entry) { /* trim chain down to 1 entry */
186 *oentry = entry->hent_next;
187 hentdelayfree(entry); /* no doubt they'll want this next. */
205 register HENT *entry;
206 register HENT **oentry;
215 if (!tb->tbl_coeffsize)
216 hash = *key + 128 * key[1] + 128 * key[klen-1];
217 else { /* use normal coefficients */
218 if (klen < tb->tbl_coeffsize)
221 maxi = tb->tbl_coeffsize;
222 for (s=key, i=0, hash = 0;
224 s++, i++, hash *= 5) {
225 hash += *s * coeff[i];
229 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
232 for (; entry; i=0, oentry = &entry->hent_next, entry = *oentry) {
233 if (entry->hent_hash != hash) /* strings can't be equal */
235 if (entry->hent_klen != klen)
237 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
239 *oentry = entry->hent_next;
240 str = str_static(entry->hent_val);
249 dbm_delete(tb->tbl_dbm,dkey);
265 int oldsize = tb->tbl_max + 1;
266 register int newsize = oldsize * 2;
270 register HENT *entry;
271 register HENT **oentry;
274 Renew(a, newsize, HENT*);
275 Zero(&a[oldsize], oldsize, HENT*); /* zero 2nd half*/
276 tb->tbl_max = --newsize;
277 tb->tbl_dosplit = tb->tbl_max * FILLPCT / 100;
280 for (i=0; i<oldsize; i++,a++) {
281 if (!*a) /* non-existent */
284 for (oentry = a, entry = *a; entry; entry = *oentry) {
285 if ((entry->hent_hash & newsize) != i) {
286 *oentry = entry->hent_next;
287 entry->hent_next = *b;
294 oentry = &entry->hent_next;
296 if (!*a) /* everything moved */
307 Newz(502,tb, 1, HASH);
309 tb->tbl_coeffsize = lookat;
310 tb->tbl_max = 7; /* it's a normal associative array */
311 tb->tbl_dosplit = tb->tbl_max * FILLPCT / 100;
314 tb->tbl_max = 127; /* it's a symbol table */
315 tb->tbl_dosplit = 128; /* so never split */
317 Newz(503,tb->tbl_array, tb->tbl_max + 1, HENT*);
322 (void)hiterinit(tb); /* so each() will start off right */
332 str_free(hent->hent_val);
333 Safefree(hent->hent_key);
343 str_2static(hent->hent_val); /* free between statements */
344 Safefree(hent->hent_key);
353 register HENT *ohent = Null(HENT*);
358 while (hent = hiternext(tb)) { /* concise but not very efficient */
365 (void)bzero((char*)tb->tbl_array, (tb->tbl_max + 1) * sizeof(HENT*));
374 register HENT *ohent = Null(HENT*);
379 while (hent = hiternext(tb)) {
384 Safefree(tb->tbl_array);
393 tb->tbl_eiter = Null(HENT*);
401 register HENT *entry;
406 entry = tb->tbl_eiter;
412 key.dptr = entry->hent_key;
413 key.dsize = entry->hent_klen;
414 key = dbm_nextkey(tb->tbl_dbm, key);
416 key = dbm_nextkey(tb->tbl_dbm);
419 key.dptr = entry->hent_key;
420 key.dsize = entry->hent_klen;
425 Newz(504,entry, 1, HENT);
426 tb->tbl_eiter = entry;
427 key = dbm_firstkey(tb->tbl_dbm);
429 entry->hent_key = key.dptr;
430 entry->hent_klen = key.dsize;
433 str_free(entry->hent_val);
435 tb->tbl_eiter = Null(HENT*);
443 entry = entry->hent_next;
446 if (tb->tbl_riter > tb->tbl_max) {
450 entry = tb->tbl_array[tb->tbl_riter];
454 tb->tbl_eiter = entry;
459 hiterkey(entry,retlen)
460 register HENT *entry;
463 *retlen = entry->hent_klen;
464 return entry->hent_key;
470 register HENT *entry;
476 key.dptr = entry->hent_key;
477 key.dsize = entry->hent_klen;
478 content = dbm_fetch(tb->tbl_dbm,key);
479 if (!entry->hent_val)
480 entry->hent_val = Str_new(62,0);
481 str_nset(entry->hent_val,content.dptr,content.dsize);
484 return entry->hent_val;
488 #if defined(FCNTL) && ! defined(O_CREAT)
499 #define O_CREAT 01000
503 static int dbmrefcnt = 0;
507 hdbmopen(tb,fname,mode)
515 if (tb->tbl_dbm) /* never really closed it */
525 tb->tbl_dbm = dbm_open(fname, O_RDWR|O_CREAT, mode);
527 tb->tbl_dbm = dbm_open(fname, O_RDWR, mode);
530 fatal("Old dbm can only open one database");
531 sprintf(buf,"%s.dir",fname);
532 if (stat(buf, &statbuf) < 0) {
533 if (mode < 0 || close(creat(buf,mode)) < 0)
535 sprintf(buf,"%s.pag",fname);
536 if (close(creat(buf,mode)) < 0)
539 tb->tbl_dbm = dbminit(fname) >= 0;
541 return tb->tbl_dbm != 0;
548 if (tb && tb->tbl_dbm) {
550 dbm_close(tb->tbl_dbm);
553 /* dbmrefcnt--; */ /* doesn't work, rats */
557 warn("Close on unopened dbm file");
561 hdbmstore(tb,key,klen,str)
567 datum dkey, dcontent;
570 if (!tb || !tb->tbl_dbm)
574 dcontent.dptr = str_get(str);
575 dcontent.dsize = str->str_cur;
576 error = dbm_store(tb->tbl_dbm, dkey, dcontent, DBM_REPLACE);
579 fatal("No write permission to dbm file");
580 warn("dbm store returned %d, errno %d, key \"%s\"",error,errno,key);
582 dbm_clearerr(tb->tbl_dbm);
587 #endif /* SOME_DBM */