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