blob: 195c88176cb88a5ab2949c5700a1ba7cc40a6ec9 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
Benny Prijonodd859a62005-11-01 16:42:51 +00002 */
3#include <pj/hash.h>
4#include <pj/log.h>
5#include <pj/string.h>
6#include <pj/pool.h>
7#include <pj/os.h>
8
9/**
10 * The hash multiplier used to calculate hash value.
11 */
12#define PJ_HASH_MULTIPLIER 33
13
14
15struct pj_hash_entry
16{
17 struct pj_hash_entry *next;
18 const void *key;
19 pj_uint32_t hash;
20 pj_uint32_t keylen;
21 void *value;
22};
23
24
25struct pj_hash_table_t
26{
27 pj_hash_entry **table;
28 unsigned count, rows;
29 pj_hash_iterator_t iterator;
30};
31
32
33
34PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, unsigned keylen)
35{
36 PJ_CHECK_STACK();
37
38 if (keylen==PJ_HASH_KEY_STRING) {
39 const unsigned char *p = key;
40 for ( ; *p; ++p ) {
41 hash = hash * PJ_HASH_MULTIPLIER + *p;
42 }
43 keylen = p - (const unsigned char*)key;
44 } else {
45 const unsigned char *p = key,
46 *end = p + keylen;
47 for ( ; p!=end; ++p) {
48 hash = hash * PJ_HASH_MULTIPLIER + *p;
49 }
50 }
51 return hash;
52}
Benny Prijonof010e692005-11-11 19:01:31 +000053
54PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval,
55 char *result,
56 const pj_str_t *key)
57{
58 long i;
59
60 for (i=0; i<key->slen; ++i) {
61 result[i] = (char)pj_tolower(key->ptr[i]);
62 hval = hval * PJ_HASH_MULTIPLIER + result[i];
63 }
64
65 return hval;
66}
Benny Prijonodd859a62005-11-01 16:42:51 +000067
68
69PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
70{
71 pj_hash_table_t *h;
72 unsigned table_size;
73
74 h = pj_pool_alloc(pool, sizeof(pj_hash_table_t));
75 h->count = 0;
76
77 PJ_LOG( 5, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
78
79 /* size must be 2^n - 1.
80 round-up the size to this rule, except when size is 2^n, then size
81 will be round-down to 2^n-1.
82 */
83 table_size = 8;
84 do {
85 table_size <<= 1;
86 } while (table_size <= size);
87 table_size -= 1;
88
89 h->rows = table_size;
90 h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
91 return h;
92}
93
94static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
95 const void *key, unsigned keylen,
96 void *val)
97{
98 pj_uint32_t hash;
99 pj_hash_entry **p_entry, *entry;
100
101 hash=0;
102 if (keylen==PJ_HASH_KEY_STRING) {
103 const unsigned char *p = key;
104 for ( ; *p; ++p ) {
105 hash = hash * PJ_HASH_MULTIPLIER + *p;
106 }
107 keylen = p - (const unsigned char*)key;
108 } else {
109 const unsigned char *p = key,
110 *end = p + keylen;
111 for ( ; p!=end; ++p) {
112 hash = hash * PJ_HASH_MULTIPLIER + *p;
113 }
114 }
115
116 /* scan the linked list */
117 for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry;
118 entry;
119 p_entry = &entry->next, entry = *p_entry)
120 {
121 if (entry->hash==hash && entry->keylen==keylen &&
122 memcmp(entry->key, key, keylen)==0)
123 {
124 break;
125 }
126 }
127
128 if (entry || val==NULL)
129 return p_entry;
130
131 /* create a new entry */
132 entry = pj_pool_alloc(pool, sizeof(pj_hash_entry));
133 PJ_LOG(5, ("hashtbl", "%p: New p_entry %p created, pool used=%u, cap=%u", ht, entry,
134 pj_pool_get_used_size(pool), pj_pool_get_capacity(pool)));
135 entry->next = NULL;
136 entry->hash = hash;
137 entry->key = key;
138 entry->keylen = keylen;
139 entry->value = val;
140 *p_entry = entry;
141
142 ++ht->count;
143
144 return p_entry;
145}
146
147PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
148 const void *key, unsigned keylen )
149{
150 pj_hash_entry *entry;
151 entry = *find_entry( NULL, ht, key, keylen, NULL);
152 return entry ? entry->value : NULL;
153}
154
155PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
156 const void *key, unsigned keylen,
157 void *value )
158{
159 pj_hash_entry **p_entry;
160
161 p_entry = find_entry( pool, ht, key, keylen, value );
162 if (*p_entry) {
163 if (value == NULL) {
164 /* delete entry */
165 PJ_LOG(5, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
166 *p_entry = (*p_entry)->next;
167 --ht->count;
168
169 } else {
170 /* overwrite */
171 (*p_entry)->value = value;
172 PJ_LOG(5, ("hashtbl", "%p: p_entry %p value set to %p", ht, *p_entry, value));
173 }
174 }
175}
176
177PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
178{
179 return ht->count;
180}
181
182PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
183 pj_hash_iterator_t *it )
184{
185 it->index = 0;
186 it->entry = NULL;
187
188 for (; it->index < ht->rows; ++it->index) {
189 it->entry = ht->table[it->index];
190 if (it->entry) {
191 break;
192 }
193 }
194
195 return it->entry ? it : NULL;
196}
197
198PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
199 pj_hash_iterator_t *it )
200{
201 it->entry = it->entry->next;
202 if (it->entry) {
203 return it;
204 }
205
206 for (++it->index; it->index < ht->rows; ++it->index) {
207 it->entry = ht->table[it->index];
208 if (it->entry) {
209 break;
210 }
211 }
212
213 return it->entry ? it : NULL;
214}
215
216PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
217{
218 PJ_CHECK_STACK();
219 PJ_UNUSED_ARG(ht);
220 return it->entry->value;
221}
222
223#if 0
224void pj_hash_dump_collision( pj_hash_table_t *ht )
225{
226 unsigned min=0xFFFFFFFF, max=0;
227 unsigned i;
228 char line[120];
229 int len, totlen = 0;
230
231 for (i=0; i<ht->rows; ++i) {
232 unsigned count = 0;
233 pj_hash_entry *entry = ht->table[i];
234 while (entry) {
235 ++count;
236 entry = entry->next;
237 }
238 if (count < min)
239 min = count;
240 if (count > max)
241 max = count;
242 len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
243 if (len < 1)
244 break;
245 totlen += len;
246
247 if ((i+1) % 10 == 0) {
248 line[totlen] = '\0';
249 PJ_LOG(4,(__FILE__, line));
250 }
251 }
252
253 PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
254}
255#endif
256
257