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 | #ifndef __PJ_HASH_H__ |
| 20 | #define __PJ_HASH_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file hash.h |
| 24 | * @brief Hash Table. |
| 25 | */ |
| 26 | |
| 27 | #include <pj/types.h> |
| 28 | |
| 29 | PJ_BEGIN_DECL |
| 30 | |
| 31 | /** |
| 32 | * @defgroup PJ_HASH Hash Table |
| 33 | * @ingroup PJ_DS |
| 34 | * @{ |
| 35 | * A hash table is a dictionary in which keys are mapped to array positions by |
| 36 | * hash functions. Having the keys of more than one item map to the same |
| 37 | * position is called a collision. In this library, we will chain the nodes |
| 38 | * that have the same key in a list. |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * If this constant is used as keylen, then the key is interpreted as |
| 43 | * NULL terminated string. |
| 44 | */ |
| 45 | #define PJ_HASH_KEY_STRING ((unsigned)-1) |
| 46 | |
| 47 | /** |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame^] | 48 | * This indicates the size of of each hash entry. |
| 49 | */ |
| 50 | #define PJ_HASH_ENTRY_SIZE (3*sizeof(void*) + 2*sizeof(pj_uint32_t)) |
| 51 | |
| 52 | /** |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 53 | * This is the function that is used by the hash table to calculate hash value |
| 54 | * of the specified key. |
| 55 | * |
| 56 | * @param hval the initial hash value, or zero. |
| 57 | * @param key the key to calculate. |
| 58 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to treat |
| 59 | * the key as null terminated string. |
| 60 | * |
| 61 | * @return the hash value. |
| 62 | */ |
| 63 | PJ_DECL(pj_uint32_t) pj_hash_calc(pj_uint32_t hval, |
| 64 | const void *key, unsigned keylen); |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Convert the key to lowercase and calculate the hash value. The resulting |
| 69 | * string is stored in \c result. |
| 70 | * |
| 71 | * @param hval The initial hash value, normally zero. |
| 72 | * @param result Buffer to store the result, which must be enough to hold |
| 73 | * the string. |
| 74 | * @param key The input key to be converted and calculated. |
| 75 | * |
| 76 | * @return The hash value. |
| 77 | */ |
| 78 | PJ_DECL(pj_uint32_t) pj_hash_calc_tolower(pj_uint32_t hval, |
| 79 | char *result, |
| 80 | const pj_str_t *key); |
| 81 | |
| 82 | /** |
| 83 | * Create a hash table with the specified 'bucket' size. |
| 84 | * |
| 85 | * @param pool the pool from which the hash table will be allocated from. |
| 86 | * @param size the bucket size, which will be round-up to the nearest 2^n+1 |
| 87 | * |
| 88 | * @return the hash table. |
| 89 | */ |
| 90 | PJ_DECL(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size); |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Get the value associated with the specified key. |
| 95 | * |
| 96 | * @param ht the hash table. |
| 97 | * @param key the key to look for. |
| 98 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the |
| 99 | * string length of the key. |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame^] | 100 | * @param hval if this argument is not NULL and the value is not zero, |
| 101 | * the value will be used as the computed hash value. If |
| 102 | * the argument is not NULL and the value is zero, it will |
| 103 | * be filled with the computed hash upon return. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 104 | * |
| 105 | * @return the value associated with the key, or NULL if the key is not found. |
| 106 | */ |
| 107 | PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht, |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame^] | 108 | const void *key, unsigned keylen, |
| 109 | pj_uint32_t *hval ); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /** |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame^] | 113 | * Associate/disassociate a value with the specified key. If value is not |
| 114 | * NULL and entry already exists, the entry's value will be overwritten. |
| 115 | * If value is not NULL and entry does not exist, a new one will be created |
| 116 | * with the specified pool. Otherwise if value is NULL, entry will be |
| 117 | * deleted if it exists. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 118 | * |
| 119 | * @param pool the pool to allocate the new entry if a new entry has to be |
| 120 | * created. |
| 121 | * @param ht the hash table. |
| 122 | * @param key the key. |
| 123 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the |
| 124 | * string length of the key. |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame^] | 125 | * @param hval if the value is not zero, then the hash table will use |
| 126 | * this value to search the entry's index, otherwise it will |
| 127 | * compute the key. This value can be obtained when calling |
| 128 | * #pj_hash_get(). |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 129 | * @param value value to be associated, or NULL to delete the entry with |
| 130 | * the specified key. |
| 131 | */ |
| 132 | PJ_DECL(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^] | 133 | const void *key, unsigned keylen, pj_uint32_t hval, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 134 | void *value ); |
| 135 | |
Benny Prijono | 40f2f64 | 2006-01-30 18:40:05 +0000 | [diff] [blame^] | 136 | |
| 137 | /** |
| 138 | * Associate/disassociate a value with the specified key. This function works |
| 139 | * like #pj_hash_set(), except that it doesn't use pool (hence the np -- no |
| 140 | * pool suffix). If new entry needs to be allocated, it will use the entry_buf. |
| 141 | * |
| 142 | * @param ht the hash table. |
| 143 | * @param key the key. |
| 144 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the |
| 145 | * string length of the key. |
| 146 | * @param hval if the value is not zero, then the hash table will use |
| 147 | * this value to search the entry's index, otherwise it will |
| 148 | * compute the key. This value can be obtained when calling |
| 149 | * #pj_hash_get(). |
| 150 | * @param entry_buf Pointer to buffer which will be used for the new entry, |
| 151 | * when one needs to be created. The buffer must be at least |
| 152 | * PJ_HASH_ENTRY_SIZE long, and the first PJ_HASH_ENTRY_SIZE |
| 153 | * bytes of the buffer will be used by the hash table. |
| 154 | * Application may use the remaining portion of the buffer |
| 155 | * for its own purpose. |
| 156 | * @param value value to be associated, or NULL to delete the entry with |
| 157 | * the specified key. |
| 158 | */ |
| 159 | PJ_DECL(void) pj_hash_set_np(pj_hash_table_t *ht, |
| 160 | const void *key, unsigned keylen, |
| 161 | pj_uint32_t hval, void *entry_buf, void *value); |
| 162 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 163 | /** |
| 164 | * Get the total number of entries in the hash table. |
| 165 | * |
| 166 | * @param ht the hash table. |
| 167 | * |
| 168 | * @return the number of entries in the hash table. |
| 169 | */ |
| 170 | PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht ); |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * Get the iterator to the first element in the hash table. |
| 175 | * |
| 176 | * @param ht the hash table. |
| 177 | * @param it the iterator for iterating hash elements. |
| 178 | * |
| 179 | * @return the iterator to the hash element, or NULL if no element presents. |
| 180 | */ |
| 181 | PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht, |
| 182 | pj_hash_iterator_t *it ); |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * Get the next element from the iterator. |
| 187 | * |
| 188 | * @param ht the hash table. |
| 189 | * @param it the hash iterator. |
| 190 | * |
| 191 | * @return the next iterator, or NULL if there's no more element. |
| 192 | */ |
| 193 | PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht, |
| 194 | pj_hash_iterator_t *it ); |
| 195 | |
| 196 | /** |
| 197 | * Get the value associated with a hash iterator. |
| 198 | * |
| 199 | * @param ht the hash table. |
| 200 | * @param it the hash iterator. |
| 201 | * |
| 202 | * @return the value associated with the current element in iterator. |
| 203 | */ |
| 204 | PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht, |
| 205 | pj_hash_iterator_t *it ); |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * @} |
| 210 | */ |
| 211 | |
| 212 | PJ_END_DECL |
| 213 | |
| 214 | #endif |
| 215 | |
| 216 | |