Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $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> |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 25 | #include <pj/assert.h> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * The hash multiplier used to calculate hash value. |
| 29 | */ |
| 30 | #define PJ_HASH_MULTIPLIER 33 |
| 31 | |
| 32 | |
| 33 | struct pj_hash_entry |
| 34 | { |
| 35 | struct pj_hash_entry *next; |
| 36 | const void *key; |
| 37 | pj_uint32_t hash; |
| 38 | pj_uint32_t keylen; |
| 39 | void *value; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | struct pj_hash_table_t |
| 44 | { |
| 45 | pj_hash_entry **table; |
| 46 | unsigned count, rows; |
| 47 | pj_hash_iterator_t iterator; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 52 | PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, |
| 53 | unsigned keylen) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 54 | { |
| 55 | PJ_CHECK_STACK(); |
| 56 | |
| 57 | if (keylen==PJ_HASH_KEY_STRING) { |
| 58 | const unsigned char *p = key; |
| 59 | for ( ; *p; ++p ) { |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 60 | hash = (hash * PJ_HASH_MULTIPLIER) + *p; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 61 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 62 | } else { |
| 63 | const unsigned char *p = key, |
| 64 | *end = p + keylen; |
| 65 | for ( ; p!=end; ++p) { |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 66 | hash = (hash * PJ_HASH_MULTIPLIER) + *p; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | return hash; |
| 70 | } |
| 71 | |
| 72 | PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval, |
| 73 | char *result, |
| 74 | const pj_str_t *key) |
| 75 | { |
| 76 | long i; |
| 77 | |
| 78 | for (i=0; i<key->slen; ++i) { |
| 79 | result[i] = (char)pj_tolower(key->ptr[i]); |
| 80 | hval = hval * PJ_HASH_MULTIPLIER + result[i]; |
| 81 | } |
| 82 | |
| 83 | return hval; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size) |
| 88 | { |
| 89 | pj_hash_table_t *h; |
| 90 | unsigned table_size; |
| 91 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 92 | /* Check that PJ_HASH_ENTRY_SIZE is correct. */ |
| 93 | PJ_ASSERT_RETURN(sizeof(pj_hash_entry)==PJ_HASH_ENTRY_SIZE, NULL); |
| 94 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 95 | h = pj_pool_alloc(pool, sizeof(pj_hash_table_t)); |
| 96 | h->count = 0; |
| 97 | |
Benny Prijono | c81dfef | 2006-01-07 18:41:22 +0000 | [diff] [blame] | 98 | PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool))); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 99 | |
| 100 | /* size must be 2^n - 1. |
| 101 | round-up the size to this rule, except when size is 2^n, then size |
| 102 | will be round-down to 2^n-1. |
| 103 | */ |
| 104 | table_size = 8; |
| 105 | do { |
| 106 | table_size <<= 1; |
| 107 | } while (table_size <= size); |
| 108 | table_size -= 1; |
| 109 | |
| 110 | h->rows = table_size; |
| 111 | h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*)); |
| 112 | return h; |
| 113 | } |
| 114 | |
| 115 | static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht, |
| 116 | const void *key, unsigned keylen, |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 117 | void *val, pj_uint32_t *hval, |
| 118 | void *entry_buf) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 119 | { |
| 120 | pj_uint32_t hash; |
| 121 | pj_hash_entry **p_entry, *entry; |
| 122 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 123 | if (hval && *hval != 0) { |
| 124 | hash = *hval; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 125 | } else { |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 126 | /* This slightly differs with pj_hash_calc() because we need |
| 127 | * to get the keylen when keylen is PJ_HASH_KEY_STRING. |
| 128 | */ |
| 129 | hash=0; |
| 130 | if (keylen==PJ_HASH_KEY_STRING) { |
| 131 | const unsigned char *p = key; |
| 132 | for ( ; *p; ++p ) { |
| 133 | hash = hash * PJ_HASH_MULTIPLIER + *p; |
| 134 | } |
| 135 | keylen = p - (const unsigned char*)key; |
| 136 | } else { |
| 137 | const unsigned char *p = key, |
| 138 | *end = p + keylen; |
| 139 | for ( ; p!=end; ++p) { |
| 140 | hash = hash * PJ_HASH_MULTIPLIER + *p; |
| 141 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 142 | } |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 143 | |
| 144 | /* Report back the computed hash. */ |
| 145 | if (hval) |
| 146 | *hval = hash; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* scan the linked list */ |
| 150 | for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry; |
| 151 | entry; |
| 152 | p_entry = &entry->next, entry = *p_entry) |
| 153 | { |
| 154 | if (entry->hash==hash && entry->keylen==keylen && |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 155 | pj_memcmp(entry->key, key, keylen)==0) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 156 | { |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (entry || val==NULL) |
| 162 | return p_entry; |
| 163 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 164 | /* Entry not found, create a new one. |
| 165 | * If entry_buf is specified, use it. Otherwise allocate from pool. |
| 166 | */ |
| 167 | if (entry_buf) { |
| 168 | entry = entry_buf; |
| 169 | } else { |
| 170 | /* Pool must be specified! */ |
| 171 | PJ_ASSERT_RETURN(pool != NULL, NULL); |
| 172 | |
| 173 | entry = pj_pool_alloc(pool, sizeof(pj_hash_entry)); |
| 174 | PJ_LOG(6, ("hashtbl", |
| 175 | "%p: New p_entry %p created, pool used=%u, cap=%u", |
| 176 | ht, entry, pj_pool_get_used_size(pool), |
| 177 | pj_pool_get_capacity(pool))); |
| 178 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 179 | entry->next = NULL; |
| 180 | entry->hash = hash; |
| 181 | entry->key = key; |
| 182 | entry->keylen = keylen; |
| 183 | entry->value = val; |
| 184 | *p_entry = entry; |
| 185 | |
| 186 | ++ht->count; |
| 187 | |
| 188 | return p_entry; |
| 189 | } |
| 190 | |
| 191 | PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht, |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 192 | const void *key, unsigned keylen, |
| 193 | pj_uint32_t *hval) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 194 | { |
| 195 | pj_hash_entry *entry; |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 196 | |
| 197 | if (hval) *hval = 0; |
| 198 | entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 199 | return entry ? entry->value : NULL; |
| 200 | } |
| 201 | |
| 202 | PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht, |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 203 | const void *key, unsigned keylen, pj_uint32_t hval, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 204 | void *value ) |
| 205 | { |
| 206 | pj_hash_entry **p_entry; |
| 207 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 208 | p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 209 | if (*p_entry) { |
| 210 | if (value == NULL) { |
| 211 | /* delete entry */ |
Benny Prijono | c81dfef | 2006-01-07 18:41:22 +0000 | [diff] [blame] | 212 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 213 | *p_entry = (*p_entry)->next; |
| 214 | --ht->count; |
| 215 | |
| 216 | } else { |
| 217 | /* overwrite */ |
| 218 | (*p_entry)->value = value; |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 219 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, |
| 220 | *p_entry, value)); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht, |
| 226 | const void *key, unsigned keylen, |
| 227 | pj_uint32_t hval, void *entry_buf, void *value) |
| 228 | { |
| 229 | pj_hash_entry **p_entry; |
| 230 | |
| 231 | p_entry = find_entry( NULL, ht, key, keylen, value, &hval, entry_buf ); |
| 232 | if (*p_entry) { |
| 233 | if (value == NULL) { |
| 234 | /* delete entry */ |
| 235 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); |
| 236 | *p_entry = (*p_entry)->next; |
| 237 | --ht->count; |
| 238 | |
| 239 | } else { |
| 240 | /* overwrite */ |
| 241 | (*p_entry)->value = value; |
| 242 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, |
| 243 | *p_entry, value)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht ) |
| 249 | { |
| 250 | return ht->count; |
| 251 | } |
| 252 | |
| 253 | PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht, |
| 254 | pj_hash_iterator_t *it ) |
| 255 | { |
| 256 | it->index = 0; |
| 257 | it->entry = NULL; |
| 258 | |
| 259 | for (; it->index < ht->rows; ++it->index) { |
| 260 | it->entry = ht->table[it->index]; |
| 261 | if (it->entry) { |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return it->entry ? it : NULL; |
| 267 | } |
| 268 | |
| 269 | PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht, |
| 270 | pj_hash_iterator_t *it ) |
| 271 | { |
| 272 | it->entry = it->entry->next; |
| 273 | if (it->entry) { |
| 274 | return it; |
| 275 | } |
| 276 | |
| 277 | for (++it->index; it->index < ht->rows; ++it->index) { |
| 278 | it->entry = ht->table[it->index]; |
| 279 | if (it->entry) { |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return it->entry ? it : NULL; |
| 285 | } |
| 286 | |
| 287 | PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it ) |
| 288 | { |
| 289 | PJ_CHECK_STACK(); |
| 290 | PJ_UNUSED_ARG(ht); |
| 291 | return it->entry->value; |
| 292 | } |
| 293 | |
| 294 | #if 0 |
| 295 | void pj_hash_dump_collision( pj_hash_table_t *ht ) |
| 296 | { |
| 297 | unsigned min=0xFFFFFFFF, max=0; |
| 298 | unsigned i; |
| 299 | char line[120]; |
| 300 | int len, totlen = 0; |
| 301 | |
| 302 | for (i=0; i<ht->rows; ++i) { |
| 303 | unsigned count = 0; |
| 304 | pj_hash_entry *entry = ht->table[i]; |
| 305 | while (entry) { |
| 306 | ++count; |
| 307 | entry = entry->next; |
| 308 | } |
| 309 | if (count < min) |
| 310 | min = count; |
| 311 | if (count > max) |
| 312 | max = count; |
| 313 | len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count); |
| 314 | if (len < 1) |
| 315 | break; |
| 316 | totlen += len; |
| 317 | |
| 318 | if ((i+1) % 10 == 0) { |
| 319 | line[totlen] = '\0'; |
| 320 | PJ_LOG(4,(__FILE__, line)); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max)); |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | |