| 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 |
|
| static char const coeff[128] = { |
| 20 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 21 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 22 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 23 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 24 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 25 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 26 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1, |
| 27 |
|
| 61,59,53,47,43,41,37,31,29,23,17,13,11,7,3,1}; |
| 28 |
|
| |
| 29 |
|
| STR * |
| 30 |
0 |
| hfetch(HASH *tb, char *key) |
| 31 |
|
| { |
| 32 |
|
| char *s; |
| 33 |
|
| int i; |
| 34 |
|
| int hash; |
| 35 |
|
| HENT *entry; |
| 36 |
|
| |
| 37 |
0 |
▶ | if (!tb) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tb) |
0 |
0 |
0 |
|
| 38 |
0 |
| return NULL; |
| 39 |
0 |
| for (s=key, i=0, hash = 0; |
| 40 |
0 |
▶ | /* while */ *s; |
Branch: 1 branch
| Branch | True | False | Total |
| /* while */ *s; |
0 |
0 |
0 |
|
| 41 |
0 |
| s++, i++, hash *= 5) { |
| 42 |
0 |
| hash += *s * coeff[i]; |
| 43 |
|
| } |
| 44 |
0 |
| entry = tb->tbl_array[hash & tb->tbl_max]; |
| 45 |
0 |
▶ | for (; entry; entry = entry->hent_next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; entry; entry = entry->hent_next) { |
0 |
0 |
0 |
|
| 46 |
0 |
▶ | if (entry->hent_hash != hash) /* strings can't be equal */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->hent_hash != hash) /* strings can't be equal */ |
0 |
0 |
0 |
|
| 47 |
0 |
| continue; |
| 48 |
0 |
▶ | if (strNE(entry->hent_key,key)) /* is this it? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (strNE(entry->hent_key,key)) /* is this it? */ |
0 |
0 |
0 |
|
| 49 |
0 |
| continue; |
| 50 |
0 |
| return entry->hent_val; |
| 51 |
|
| } |
| 52 |
0 |
| return NULL; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
|
| bool |
| 56 |
0 |
| hstore(HASH *tb, char *key, STR *val) |
| 57 |
|
| { |
| 58 |
|
| char *s; |
| 59 |
|
| int i; |
| 60 |
|
| int hash; |
| 61 |
|
| HENT *entry; |
| 62 |
|
| HENT **oentry; |
| 63 |
|
| |
| 64 |
0 |
▶ | if (!tb) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tb) |
0 |
0 |
0 |
|
| 65 |
0 |
| return FALSE; |
| 66 |
0 |
| for (s=key, i=0, hash = 0; |
| 67 |
0 |
▶ | /* while */ *s; |
Branch: 1 branch
| Branch | True | False | Total |
| /* while */ *s; |
0 |
0 |
0 |
|
| 68 |
0 |
| s++, i++, hash *= 5) { |
| 69 |
0 |
| hash += *s * coeff[i]; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
0 |
| oentry = &(tb->tbl_array[hash & tb->tbl_max]); |
| 73 |
0 |
| i = 1; |
| 74 |
|
| |
| 75 |
0 |
▶ | for (entry = *oentry; entry; i=0, entry = entry->hent_next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (entry = *oentry; entry; i=0, entry = entry->hent_next) { |
0 |
0 |
0 |
|
| 76 |
0 |
▶ | if (entry->hent_hash != hash) /* strings can't be equal */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->hent_hash != hash) /* strings can't be equal */ |
0 |
0 |
0 |
|
| 77 |
0 |
| continue; |
| 78 |
0 |
▶ | if (strNE(entry->hent_key,key)) /* is this it? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (strNE(entry->hent_key,key)) /* is this it? */ |
0 |
0 |
0 |
|
| 79 |
0 |
| continue; |
| 80 |
|
| /*NOSTRICT*/ |
| 81 |
0 |
| safefree(entry->hent_val); |
| 82 |
0 |
| entry->hent_val = val; |
| 83 |
0 |
| return TRUE; |
| 84 |
|
| } |
| 85 |
|
| /*NOSTRICT*/ |
| 86 |
0 |
| entry = (HENT*) safemalloc(sizeof(HENT)); |
| 87 |
|
| |
| 88 |
0 |
| entry->hent_key = savestr(key); |
| 89 |
0 |
| entry->hent_val = val; |
| 90 |
0 |
| entry->hent_hash = hash; |
| 91 |
0 |
| entry->hent_next = *oentry; |
| 92 |
0 |
| *oentry = entry; |
| 93 |
|
| |
| 94 |
0 |
▶ | if (i) { /* initial entry? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (i) { /* initial entry? */ |
0 |
0 |
0 |
|
| 95 |
0 |
| tb->tbl_fill++; |
| 96 |
0 |
▶ | if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT) |
0 |
0 |
0 |
|
| 97 |
0 |
| hsplit(tb); |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
| return FALSE; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| void |
| 104 |
0 |
| hsplit(HASH *tb) |
| 105 |
|
| { |
| 106 |
0 |
| const int oldsize = tb->tbl_max + 1; |
| 107 |
0 |
| int newsize = oldsize * 2; |
| 108 |
|
| int i; |
| 109 |
|
| HENT **a; |
| 110 |
|
| HENT **b; |
| 111 |
|
| HENT *entry; |
| 112 |
|
| HENT **oentry; |
| 113 |
|
| |
| 114 |
0 |
| a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*)); |
| 115 |
0 |
| memset(&a[oldsize], 0, oldsize * sizeof(HENT*)); /* zero second half */ |
| 116 |
0 |
| tb->tbl_max = --newsize; |
| 117 |
0 |
| tb->tbl_array = a; |
| 118 |
|
| |
| 119 |
0 |
▶ | for (i=0; i<oldsize; i++,a++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<oldsize; i++,a++) { |
0 |
0 |
0 |
|
| 120 |
0 |
▶ | if (!*a) /* non-existent */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*a) /* non-existent */ |
0 |
0 |
0 |
|
| 121 |
0 |
| continue; |
| 122 |
0 |
| b = a+oldsize; |
| 123 |
0 |
▶ | for (oentry = a, entry = *a; entry; entry = *oentry) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (oentry = a, entry = *a; entry; entry = *oentry) { |
0 |
0 |
0 |
|
| 124 |
0 |
▶ | if ((entry->hent_hash & newsize) != i) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((entry->hent_hash & newsize) != i) { |
0 |
0 |
0 |
|
| 125 |
0 |
| *oentry = entry->hent_next; |
| 126 |
0 |
| entry->hent_next = *b; |
| 127 |
0 |
▶ | if (!*b) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*b) |
0 |
0 |
0 |
|
| 128 |
0 |
| tb->tbl_fill++; |
| 129 |
0 |
| *b = entry; |
| 130 |
0 |
| continue; |
| 131 |
|
| } |
| 132 |
|
| else |
| 133 |
0 |
| oentry = &entry->hent_next; |
| 134 |
|
| } |
| 135 |
0 |
▶ | if (!*a) /* everything moved */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*a) /* everything moved */ |
0 |
0 |
0 |
|
| 136 |
0 |
| tb->tbl_fill--; |
| 137 |
|
| } |
| 138 |
0 |
| } |
| 139 |
|
| |
| 140 |
|
| HASH * |
| 141 |
0 |
| hnew(void) |
| 142 |
|
| { |
| 143 |
0 |
| HASH *tb = (HASH*)safemalloc(sizeof(HASH)); |
| 144 |
|
| |
| 145 |
0 |
| tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*)); |
| 146 |
0 |
| tb->tbl_fill = 0; |
| 147 |
0 |
| tb->tbl_max = 7; |
| 148 |
0 |
| hiterinit(tb); /* so each() will start off right */ |
| 149 |
0 |
| memset(tb->tbl_array, 0, 8 * sizeof(HENT*)); |
| 150 |
0 |
| return tb; |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
|
| int |
| 154 |
0 |
| hiterinit(HASH *tb) |
| 155 |
|
| { |
| 156 |
0 |
| tb->tbl_riter = -1; |
| 157 |
0 |
| tb->tbl_eiter = (HENT*)NULL; |
| 158 |
0 |
| return tb->tbl_fill; |
| 159 |
|
| } |