blob: 499032cb74e6dc38adac58475b8bbd8f7a0465dc [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$
2 */
3/*
4 * PJLIB - PJ Foundation Library
5 * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
6 *
7 * Author:
8 * Benny Prijono <bennylp@bulukucing.org>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24#include <pj/hash.h>
25#include <pj/log.h>
26#include <pj/string.h>
27#include <pj/pool.h>
28#include <pj/os.h>
29#include <pj/ctype.h>
30
31/**
32 * The hash multiplier used to calculate hash value.
33 */
34#define PJ_HASH_MULTIPLIER 33
35
36
37struct pj_hash_entry
38{
39 struct pj_hash_entry *next;
40 const void *key;
41 pj_uint32_t hash;
42 pj_uint32_t keylen;
43 void *value;
44};
45
46
47struct pj_hash_table_t
48{
49 pj_hash_entry **table;
50 unsigned count, rows;
51 pj_hash_iterator_t iterator;
52};
53
54
55
56PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, unsigned keylen)
57{
58 PJ_CHECK_STACK();
59
60 if (keylen==PJ_HASH_KEY_STRING) {
61 const unsigned char *p = key;
62 for ( ; *p; ++p ) {
63 hash = hash * PJ_HASH_MULTIPLIER + *p;
64 }
65 keylen = p - (const unsigned char*)key;
66 } else {
67 const unsigned char *p = key,
68 *end = p + keylen;
69 for ( ; p!=end; ++p) {
70 hash = hash * PJ_HASH_MULTIPLIER + *p;
71 }
72 }
73 return hash;
74}
Benny Prijonof010e692005-11-11 19:01:31 +000075
76PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval,
77 char *result,
78 const pj_str_t *key)
79{
80 long i;
81
82 for (i=0; i<key->slen; ++i) {
83 result[i] = (char)pj_tolower(key->ptr[i]);
84 hval = hval * PJ_HASH_MULTIPLIER + result[i];
85 }
86
87 return hval;
88}
Benny Prijonoe7224612005-11-13 19:40:44 +000089
90
91PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
92{
93 pj_hash_table_t *h;
94 unsigned table_size;
95
96 h = pj_pool_alloc(pool, sizeof(pj_hash_table_t));
97 h->count = 0;
98
99 PJ_LOG( 5, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
100
101 /* size must be 2^n - 1.
102 round-up the size to this rule, except when size is 2^n, then size
103 will be round-down to 2^n-1.
104 */
105 table_size = 8;
106 do {
107 table_size <<= 1;
108 } while (table_size <= size);
109 table_size -= 1;
110
111 h->rows = table_size;
112 h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
113 return h;
114}
115
116static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
117 const void *key, unsigned keylen,
118 void *val)
119{
120 pj_uint32_t hash;
121 pj_hash_entry **p_entry, *entry;
122
123 hash=0;
124 if (keylen==PJ_HASH_KEY_STRING) {
125 const unsigned char *p = key;
126 for ( ; *p; ++p ) {
127 hash = hash * PJ_HASH_MULTIPLIER + *p;
128 }
129 keylen = p - (const unsigned char*)key;
130 } else {
131 const unsigned char *p = key,
132 *end = p + keylen;
133 for ( ; p!=end; ++p) {
134 hash = hash * PJ_HASH_MULTIPLIER + *p;
135 }
136 }
137
138 /* scan the linked list */
139 for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
140 entry;
141 p_entry = &entry->next, entry = *p_entry)
142 {
143 if (entry->hash==hash && entry->keylen==keylen &&
144 memcmp(entry->key, key, keylen)==0)
145 {
146 break;
147 }
148 }
149
150 if (entry || val==NULL)
151 return p_entry;
152
153 /* create a new entry */
154 entry = pj_pool_alloc(pool, sizeof(pj_hash_entry));
155 PJ_LOG(5, ("hashtbl", "%p: New p_entry %p created, pool used=%u, cap=%u", ht, entry,
156 pj_pool_get_used_size(pool), pj_pool_get_capacity(pool)));
157 entry->next = NULL;
158 entry->hash = hash;
159 entry->key = key;
160 entry->keylen = keylen;
161 entry->value = val;
162 *p_entry = entry;
163
164 ++ht->count;
165
166 return p_entry;
167}
168
169PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
170 const void *key, unsigned keylen )
171{
172 pj_hash_entry *entry;
173 entry = *find_entry( NULL, ht, key, keylen, NULL);
174 return entry ? entry->value : NULL;
175}
176
177PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
178 const void *key, unsigned keylen,
179 void *value )
180{
181 pj_hash_entry **p_entry;
182
183 p_entry = find_entry( pool, ht, key, keylen, value );
184 if (*p_entry) {
185 if (value == NULL) {
186 /* delete entry */
187 PJ_LOG(5, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
188 *p_entry = (*p_entry)->next;
189 --ht->count;
190
191 } else {
192 /* overwrite */
193 (*p_entry)->value = value;
194 PJ_LOG(5, ("hashtbl", "%p: p_entry %p value set to %p", ht, *p_entry, value));
195 }
196 }
197}
198
199PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
200{
201 return ht->count;
202}
203
204PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
205 pj_hash_iterator_t *it )
206{
207 it->index = 0;
208 it->entry = NULL;
209
210 for (; it->index < ht->rows; ++it->index) {
211 it->entry = ht->table[it->index];
212 if (it->entry) {
213 break;
214 }
215 }
216
217 return it->entry ? it : NULL;
218}
219
220PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
221 pj_hash_iterator_t *it )
222{
223 it->entry = it->entry->next;
224 if (it->entry) {
225 return it;
226 }
227
228 for (++it->index; it->index < ht->rows; ++it->index) {
229 it->entry = ht->table[it->index];
230 if (it->entry) {
231 break;
232 }
233 }
234
235 return it->entry ? it : NULL;
236}
237
238PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
239{
240 PJ_CHECK_STACK();
241 PJ_UNUSED_ARG(ht);
242 return it->entry->value;
243}
244
245#if 0
246void pj_hash_dump_collision( pj_hash_table_t *ht )
247{
248 unsigned min=0xFFFFFFFF, max=0;
249 unsigned i;
250 char line[120];
251 int len, totlen = 0;
252
253 for (i=0; i<ht->rows; ++i) {
254 unsigned count = 0;
255 pj_hash_entry *entry = ht->table[i];
256 while (entry) {
257 ++count;
258 entry = entry->next;
259 }
260 if (count < min)
261 min = count;
262 if (count > max)
263 max = count;
264 len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
265 if (len < 1)
266 break;
267 totlen += len;
268
269 if ((i+1) % 10 == 0) {
270 line[totlen] = '\0';
271 PJ_LOG(4,(__FILE__, line));
272 }
273 }
274
275 PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
276}
277#endif
278
279