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 | |
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 | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 103 | /* Check that PJ_HASH_ENTRY_SIZE is correct. */ |
| 104 | PJ_ASSERT_RETURN(sizeof(pj_hash_entry)==PJ_HASH_ENTRY_SIZE, NULL); |
| 105 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 106 | h = pj_pool_alloc(pool, sizeof(pj_hash_table_t)); |
| 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; |
| 122 | h->table = pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*)); |
| 123 | return h; |
| 124 | } |
| 125 | |
| 126 | static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht, |
| 127 | const void *key, unsigned keylen, |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 128 | void *val, pj_uint32_t *hval, |
| 129 | void *entry_buf) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 130 | { |
| 131 | pj_uint32_t hash; |
| 132 | pj_hash_entry **p_entry, *entry; |
| 133 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 134 | if (hval && *hval != 0) { |
| 135 | hash = *hval; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 136 | } else { |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 137 | /* This slightly differs with pj_hash_calc() because we need |
| 138 | * to get the keylen when keylen is PJ_HASH_KEY_STRING. |
| 139 | */ |
| 140 | hash=0; |
| 141 | if (keylen==PJ_HASH_KEY_STRING) { |
| 142 | const unsigned char *p = key; |
| 143 | for ( ; *p; ++p ) { |
| 144 | hash = hash * PJ_HASH_MULTIPLIER + *p; |
| 145 | } |
| 146 | keylen = p - (const unsigned char*)key; |
| 147 | } else { |
| 148 | const unsigned char *p = key, |
| 149 | *end = p + keylen; |
| 150 | for ( ; p!=end; ++p) { |
| 151 | hash = hash * PJ_HASH_MULTIPLIER + *p; |
| 152 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 153 | } |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 154 | |
| 155 | /* Report back the computed hash. */ |
| 156 | if (hval) |
| 157 | *hval = hash; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* scan the linked list */ |
| 161 | for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry; |
| 162 | entry; |
| 163 | p_entry = &entry->next, entry = *p_entry) |
| 164 | { |
| 165 | if (entry->hash==hash && entry->keylen==keylen && |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 166 | pj_memcmp(entry->key, key, keylen)==0) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 167 | { |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (entry || val==NULL) |
| 173 | return p_entry; |
| 174 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 175 | /* Entry not found, create a new one. |
| 176 | * If entry_buf is specified, use it. Otherwise allocate from pool. |
| 177 | */ |
| 178 | if (entry_buf) { |
| 179 | entry = entry_buf; |
| 180 | } else { |
| 181 | /* Pool must be specified! */ |
| 182 | PJ_ASSERT_RETURN(pool != NULL, NULL); |
| 183 | |
| 184 | entry = pj_pool_alloc(pool, sizeof(pj_hash_entry)); |
| 185 | PJ_LOG(6, ("hashtbl", |
| 186 | "%p: New p_entry %p created, pool used=%u, cap=%u", |
| 187 | ht, entry, pj_pool_get_used_size(pool), |
| 188 | pj_pool_get_capacity(pool))); |
| 189 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 190 | entry->next = NULL; |
| 191 | entry->hash = hash; |
| 192 | entry->key = key; |
| 193 | entry->keylen = keylen; |
| 194 | entry->value = val; |
| 195 | *p_entry = entry; |
| 196 | |
| 197 | ++ht->count; |
| 198 | |
| 199 | return p_entry; |
| 200 | } |
| 201 | |
| 202 | PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht, |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 203 | const void *key, unsigned keylen, |
| 204 | pj_uint32_t *hval) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 205 | { |
| 206 | pj_hash_entry *entry; |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 207 | entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 208 | return entry ? entry->value : NULL; |
| 209 | } |
| 210 | |
| 211 | 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] | 212 | const void *key, unsigned keylen, pj_uint32_t hval, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 213 | void *value ) |
| 214 | { |
| 215 | pj_hash_entry **p_entry; |
| 216 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 217 | p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 218 | if (*p_entry) { |
| 219 | if (value == NULL) { |
| 220 | /* delete entry */ |
Benny Prijono | c81dfef | 2006-01-07 18:41:22 +0000 | [diff] [blame] | 221 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 222 | *p_entry = (*p_entry)->next; |
| 223 | --ht->count; |
| 224 | |
| 225 | } else { |
| 226 | /* overwrite */ |
| 227 | (*p_entry)->value = value; |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame] | 228 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, |
| 229 | *p_entry, value)); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht, |
| 235 | const void *key, unsigned keylen, |
| 236 | pj_uint32_t hval, void *entry_buf, void *value) |
| 237 | { |
| 238 | pj_hash_entry **p_entry; |
| 239 | |
| 240 | p_entry = find_entry( NULL, ht, key, keylen, value, &hval, entry_buf ); |
| 241 | if (*p_entry) { |
| 242 | if (value == NULL) { |
| 243 | /* delete entry */ |
| 244 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); |
| 245 | *p_entry = (*p_entry)->next; |
| 246 | --ht->count; |
| 247 | |
| 248 | } else { |
| 249 | /* overwrite */ |
| 250 | (*p_entry)->value = value; |
| 251 | PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, |
| 252 | *p_entry, value)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht ) |
| 258 | { |
| 259 | return ht->count; |
| 260 | } |
| 261 | |
| 262 | PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht, |
| 263 | pj_hash_iterator_t *it ) |
| 264 | { |
| 265 | it->index = 0; |
| 266 | it->entry = NULL; |
| 267 | |
| 268 | for (; it->index < ht->rows; ++it->index) { |
| 269 | it->entry = ht->table[it->index]; |
| 270 | if (it->entry) { |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return it->entry ? it : NULL; |
| 276 | } |
| 277 | |
| 278 | PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht, |
| 279 | pj_hash_iterator_t *it ) |
| 280 | { |
| 281 | it->entry = it->entry->next; |
| 282 | if (it->entry) { |
| 283 | return it; |
| 284 | } |
| 285 | |
| 286 | for (++it->index; it->index < ht->rows; ++it->index) { |
| 287 | it->entry = ht->table[it->index]; |
| 288 | if (it->entry) { |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return it->entry ? it : NULL; |
| 294 | } |
| 295 | |
| 296 | PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it ) |
| 297 | { |
| 298 | PJ_CHECK_STACK(); |
| 299 | PJ_UNUSED_ARG(ht); |
| 300 | return it->entry->value; |
| 301 | } |
| 302 | |
| 303 | #if 0 |
| 304 | void pj_hash_dump_collision( pj_hash_table_t *ht ) |
| 305 | { |
| 306 | unsigned min=0xFFFFFFFF, max=0; |
| 307 | unsigned i; |
| 308 | char line[120]; |
| 309 | int len, totlen = 0; |
| 310 | |
| 311 | for (i=0; i<ht->rows; ++i) { |
| 312 | unsigned count = 0; |
| 313 | pj_hash_entry *entry = ht->table[i]; |
| 314 | while (entry) { |
| 315 | ++count; |
| 316 | entry = entry->next; |
| 317 | } |
| 318 | if (count < min) |
| 319 | min = count; |
| 320 | if (count > max) |
| 321 | max = count; |
| 322 | len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count); |
| 323 | if (len < 1) |
| 324 | break; |
| 325 | totlen += len; |
| 326 | |
| 327 | if ((i+1) % 10 == 0) { |
| 328 | line[totlen] = '\0'; |
| 329 | PJ_LOG(4,(__FILE__, line)); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max)); |
| 334 | } |
| 335 | #endif |
| 336 | |
| 337 | |