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