1 /* $Header: hash.c,v 3.0.1.2 89/12/21 20:03:39 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.2 89/12/21 20:03:39 lwall
10 * patch7: errno may now be a macro with an lvalue
12 * Revision 3.0.1.1 89/11/11 04:34:18 lwall
13 * patch2: CX/UX needed to set the key each time in associative iterators
15 * Revision 3.0 89/10/18 15:18:32 lwall
24 hfetch(tb,key,klen,lval)
43 /* The hash function we use on symbols has to be equal to the first
44 * character when taken modulo 128, so that str_reset() can be implemented
45 * efficiently. We throw in the second character and the last character
46 * (times 128) so that long chains of identifiers starting with the
47 * same letter don't have to be strEQ'ed within hfetch(), since it
48 * compares hash values before trying strEQ().
50 if (!tb->tbl_coeffsize)
51 hash = *key + 128 * key[1] + 128 * key[klen-1]; /* assuming klen > 0 */
52 else { /* use normal coefficients */
53 if (klen < tb->tbl_coeffsize)
56 maxi = tb->tbl_coeffsize;
57 for (s=key, i=0, hash = 0;
59 s++, i++, hash *= 5) {
60 hash += *s * coeff[i];
64 entry = tb->tbl_array[hash & tb->tbl_max];
65 for (; entry; entry = entry->hent_next) {
66 if (entry->hent_hash != hash) /* strings can't be equal */
68 if (entry->hent_klen != klen)
70 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
72 return entry->hent_val;
78 dcontent = dbm_fetch(tb->tbl_dbm,dkey);
79 if (dcontent.dptr) { /* found one */
80 str = Str_new(60,dcontent.dsize);
81 str_nset(str,dcontent.dptr,dcontent.dsize);
82 hstore(tb,key,klen,str,hash); /* cache it */
87 if (lval) { /* gonna assign to this, so it better be there */
89 hstore(tb,key,klen,str,hash);
96 hstore(tb,key,klen,val,hash)
105 register HENT *entry;
106 register HENT **oentry;
114 else if (!tb->tbl_coeffsize)
115 hash = *key + 128 * key[1] + 128 * key[klen-1];
116 else { /* use normal coefficients */
117 if (klen < tb->tbl_coeffsize)
120 maxi = tb->tbl_coeffsize;
121 for (s=key, i=0, hash = 0;
123 s++, i++, hash *= 5) {
124 hash += *s * coeff[i];
128 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
131 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
132 if (entry->hent_hash != hash) /* strings can't be equal */
134 if (entry->hent_klen != klen)
136 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
138 Safefree(entry->hent_val);
139 entry->hent_val = val;
142 New(501,entry, 1, HENT);
144 entry->hent_klen = klen;
145 entry->hent_key = nsavestr(key,klen);
146 entry->hent_val = val;
147 entry->hent_hash = hash;
148 entry->hent_next = *oentry;
151 /* hdbmstore not necessary here because it's called from stabset() */
153 if (i) { /* initial entry? */
156 if (tb->tbl_dbm && tb->tbl_max >= DBM_CACHE_MAX)
159 if (tb->tbl_fill > tb->tbl_dosplit)
163 else if (tb->tbl_dbm) { /* is this just a cache for dbm file? */
164 entry = tb->tbl_array[hash & tb->tbl_max];
165 oentry = &entry->hent_next;
167 while (entry) { /* trim chain down to 1 entry */
168 *oentry = entry->hent_next;
169 hentfree(entry); /* no doubt they'll want this next. */
187 register HENT *entry;
188 register HENT **oentry;
197 if (!tb->tbl_coeffsize)
198 hash = *key + 128 * key[1] + 128 * key[klen-1];
199 else { /* use normal coefficients */
200 if (klen < tb->tbl_coeffsize)
203 maxi = tb->tbl_coeffsize;
204 for (s=key, i=0, hash = 0;
206 s++, i++, hash *= 5) {
207 hash += *s * coeff[i];
211 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
214 for (; entry; i=0, oentry = &entry->hent_next, entry = *oentry) {
215 if (entry->hent_hash != hash) /* strings can't be equal */
217 if (entry->hent_klen != klen)
219 if (bcmp(entry->hent_key,key,klen)) /* is this it? */
221 *oentry = entry->hent_next;
222 str = str_static(entry->hent_val);
231 dbm_delete(tb->tbl_dbm,dkey);
247 int oldsize = tb->tbl_max + 1;
248 register int newsize = oldsize * 2;
252 register HENT *entry;
253 register HENT **oentry;
256 Renew(a, newsize, HENT*);
257 Zero(&a[oldsize], oldsize, HENT*); /* zero 2nd half*/
258 tb->tbl_max = --newsize;
259 tb->tbl_dosplit = tb->tbl_max * FILLPCT / 100;
262 for (i=0; i<oldsize; i++,a++) {
263 if (!*a) /* non-existent */
266 for (oentry = a, entry = *a; entry; entry = *oentry) {
267 if ((entry->hent_hash & newsize) != i) {
268 *oentry = entry->hent_next;
269 entry->hent_next = *b;
276 oentry = &entry->hent_next;
278 if (!*a) /* everything moved */
289 Newz(502,tb, 1, HASH);
291 tb->tbl_coeffsize = lookat;
292 tb->tbl_max = 7; /* it's a normal associative array */
293 tb->tbl_dosplit = tb->tbl_max * FILLPCT / 100;
296 tb->tbl_max = 127; /* it's a symbol table */
297 tb->tbl_dosplit = 128; /* so never split */
299 Newz(503,tb->tbl_array, tb->tbl_max + 1, HENT*);
304 (void)hiterinit(tb); /* so each() will start off right */
314 str_free(hent->hent_val);
315 Safefree(hent->hent_key);
324 register HENT *ohent = Null(HENT*);
329 while (hent = hiternext(tb)) { /* concise but not very efficient */
336 (void)bzero((char*)tb->tbl_array, (tb->tbl_max + 1) * sizeof(HENT*));
345 register HENT *ohent = Null(HENT*);
350 while (hent = hiternext(tb)) {
355 Safefree(tb->tbl_array);
364 tb->tbl_eiter = Null(HENT*);
372 register HENT *entry;
377 entry = tb->tbl_eiter;
383 key.dptr = entry->hent_key;
384 key.dsize = entry->hent_klen;
385 key = dbm_nextkey(tb->tbl_dbm, key);
387 key = dbm_nextkey(tb->tbl_dbm);
390 key.dptr = entry->hent_key;
391 key.dsize = entry->hent_klen;
396 Newz(504,entry, 1, HENT);
397 tb->tbl_eiter = entry;
398 key = dbm_firstkey(tb->tbl_dbm);
400 entry->hent_key = key.dptr;
401 entry->hent_klen = key.dsize;
404 str_free(entry->hent_val);
406 tb->tbl_eiter = Null(HENT*);
414 entry = entry->hent_next;
417 if (tb->tbl_riter > tb->tbl_max) {
421 entry = tb->tbl_array[tb->tbl_riter];
425 tb->tbl_eiter = entry;
430 hiterkey(entry,retlen)
431 register HENT *entry;
434 *retlen = entry->hent_klen;
435 return entry->hent_key;
441 register HENT *entry;
447 key.dptr = entry->hent_key;
448 key.dsize = entry->hent_klen;
449 content = dbm_fetch(tb->tbl_dbm,key);
450 if (!entry->hent_val)
451 entry->hent_val = Str_new(62,0);
452 str_nset(entry->hent_val,content.dptr,content.dsize);
455 return entry->hent_val;
459 #if defined(FCNTL) && ! defined(O_CREAT)
470 #define O_CREAT 01000
474 static int dbmrefcnt = 0;
478 hdbmopen(tb,fname,mode)
486 if (tb->tbl_dbm) /* never really closed it */
493 tb->tbl_dbm = dbm_open(fname, O_RDWR|O_CREAT, mode);
494 if (!tb->tbl_dbm) /* oops, just try reading it */
495 tb->tbl_dbm = dbm_open(fname, O_RDONLY, mode);
498 fatal("Old dbm can only open one database");
499 sprintf(buf,"%s.dir",fname);
500 if (stat(buf, &statbuf) < 0) {
501 if (close(creat(buf,mode)) < 0)
503 sprintf(buf,"%s.pag",fname);
504 if (close(creat(buf,mode)) < 0)
507 tb->tbl_dbm = dbminit(fname) >= 0;
509 return tb->tbl_dbm != 0;
516 if (tb && tb->tbl_dbm) {
518 dbm_close(tb->tbl_dbm);
521 /* dbmrefcnt--; */ /* doesn't work, rats */
525 warn("Close on unopened dbm file");
529 hdbmstore(tb,key,klen,str)
535 datum dkey, dcontent;
538 if (!tb || !tb->tbl_dbm)
542 dcontent.dptr = str_get(str);
543 dcontent.dsize = str->str_cur;
544 error = dbm_store(tb->tbl_dbm, dkey, dcontent, DBM_REPLACE);
547 fatal("No write permission to dbm file");
548 warn("dbm store returned %d, errno %d, key \"%s\"",error,errno,key);
550 dbm_clearerr(tb->tbl_dbm);
555 #endif /* SOME_DBM */