| 1 | /* hash.c
|
|---|
| 2 | *
|
|---|
| 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002,
|
|---|
| 4 | * 2005 by Larry Wall and others
|
|---|
| 5 | *
|
|---|
| 6 | * You may distribute under the terms of either the GNU General Public
|
|---|
| 7 | * License or the Artistic License, as specified in the README file.
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #include <stdio.h>
|
|---|
| 11 | #include "EXTERN.h"
|
|---|
| 12 | #include "a2p.h"
|
|---|
| 13 | #include "util.h"
|
|---|
| 14 |
|
|---|
| 15 | #ifdef NETWARE
|
|---|
| 16 | char *savestr(char *str);
|
|---|
| 17 | #endif
|
|---|
| 18 |
|
|---|
| 19 | STR *
|
|---|
| 20 | hfetch(register HASH *tb, char *key)
|
|---|
| 21 | {
|
|---|
| 22 | register char *s;
|
|---|
| 23 | register int i;
|
|---|
| 24 | register int hash;
|
|---|
| 25 | register HENT *entry;
|
|---|
| 26 |
|
|---|
| 27 | if (!tb)
|
|---|
| 28 | return Nullstr;
|
|---|
| 29 | for (s=key, i=0, hash = 0;
|
|---|
| 30 | /* while */ *s;
|
|---|
| 31 | s++, i++, hash *= 5) {
|
|---|
| 32 | hash += *s * coeff[i];
|
|---|
| 33 | }
|
|---|
| 34 | entry = tb->tbl_array[hash & tb->tbl_max];
|
|---|
| 35 | for (; entry; entry = entry->hent_next) {
|
|---|
| 36 | if (entry->hent_hash != hash) /* strings can't be equal */
|
|---|
| 37 | continue;
|
|---|
| 38 | if (strNE(entry->hent_key,key)) /* is this it? */
|
|---|
| 39 | continue;
|
|---|
| 40 | return entry->hent_val;
|
|---|
| 41 | }
|
|---|
| 42 | return Nullstr;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | bool
|
|---|
| 46 | hstore(register HASH *tb, char *key, STR *val)
|
|---|
| 47 | {
|
|---|
| 48 | register char *s;
|
|---|
| 49 | register int i;
|
|---|
| 50 | register int hash;
|
|---|
| 51 | register HENT *entry;
|
|---|
| 52 | register HENT **oentry;
|
|---|
| 53 |
|
|---|
| 54 | if (!tb)
|
|---|
| 55 | return FALSE;
|
|---|
| 56 | for (s=key, i=0, hash = 0;
|
|---|
| 57 | /* while */ *s;
|
|---|
| 58 | s++, i++, hash *= 5) {
|
|---|
| 59 | hash += *s * coeff[i];
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | oentry = &(tb->tbl_array[hash & tb->tbl_max]);
|
|---|
| 63 | i = 1;
|
|---|
| 64 |
|
|---|
| 65 | for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
|
|---|
| 66 | if (entry->hent_hash != hash) /* strings can't be equal */
|
|---|
| 67 | continue;
|
|---|
|
|---|