Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame^] | 1 | /* $Id$
|
| 2 | *
|
| 3 | */
|
| 4 | /*
|
| 5 | * PJLIB - PJ Foundation Library
|
| 6 | * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
|
| 7 | *
|
| 8 | * Author:
|
| 9 | * Benny Prijono <bennylp@bulukucing.org>
|
| 10 | *
|
| 11 | * This library is free software; you can redistribute it and/or
|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
| 13 | * License as published by the Free Software Foundation; either
|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
| 15 | *
|
| 16 | * This library is distributed in the hope that it will be useful,
|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 19 | * Lesser General Public License for more details.
|
| 20 | *
|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
| 22 | * License along with this library; if not, write to the Free Software
|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 24 | */
|
| 25 |
|
| 26 | #ifndef __PJ_HASH_H__
|
| 27 | #define __PJ_HASH_H__
|
| 28 |
|
| 29 | /**
|
| 30 | * @file hash.h
|
| 31 | * @brief Hash Table.
|
| 32 | */
|
| 33 |
|
| 34 | #include <pj/types.h>
|
| 35 |
|
| 36 | PJ_BEGIN_DECL
|
| 37 |
|
| 38 | /**
|
| 39 | * @defgroup PJ_HASH Hash Table
|
| 40 | * @ingroup PJ_DS
|
| 41 | * @{
|
| 42 | * A hash table is a dictionary in which keys are mapped to array positions by
|
| 43 | * hash functions. Having the keys of more than one item map to the same
|
| 44 | * position is called a collision. In this library, we will chain the nodes
|
| 45 | * that have the same key in a list.
|
| 46 | */
|
| 47 |
|
| 48 | /**
|
| 49 | * If this constant is used as keylen, then the key is interpreted as
|
| 50 | * NULL terminated string.
|
| 51 | */
|
| 52 | #define PJ_HASH_KEY_STRING ((unsigned)-1)
|
| 53 |
|
| 54 | /**
|
| 55 | * This is the function that is used by the hash table to calculate hash value
|
| 56 | * of the specified key.
|
| 57 | *
|
| 58 | * @param hval the initial hash value, or zero.
|
| 59 | * @param key the key to calculate.
|
| 60 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to treat
|
| 61 | * the key as null terminated string.
|
| 62 | *
|
| 63 | * @return the hash value.
|
| 64 | */
|
| 65 | PJ_DECL(pj_uint32_t) pj_hash_calc(pj_uint32_t hval,
|
| 66 | const void *key, unsigned keylen);
|
| 67 |
|
Benny Prijono | f010e69 | 2005-11-11 19:01:31 +0000 | [diff] [blame] | 68 |
|
| 69 | /**
|
| 70 | * Convert the key to lowercase and calculate the hash value. The resulting
|
| 71 | * string is stored in \c result.
|
| 72 | *
|
| 73 | * @param hval The initial hash value, normally zero.
|
| 74 | * @param result Buffer to store the result, which must be enough to hold
|
| 75 | * the string.
|
| 76 | * @param key The input key to be converted and calculated.
|
| 77 | *
|
| 78 | * @return The hash value.
|
| 79 | */
|
| 80 | PJ_DECL(pj_uint32_t) pj_hash_calc_tolower(pj_uint32_t hval,
|
| 81 | char *result,
|
| 82 | const pj_str_t *key);
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame^] | 83 |
|
| 84 | /**
|
| 85 | * Create a hash table with the specified 'bucket' size.
|
| 86 | *
|
| 87 | * @param pool the pool from which the hash table will be allocated from.
|
| 88 | * @param size the bucket size, which will be round-up to the nearest 2^n+1
|
| 89 | *
|
| 90 | * @return the hash table.
|
| 91 | */
|
| 92 | PJ_DECL(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size);
|
| 93 |
|
| 94 |
|
| 95 | /**
|
| 96 | * Get the value associated with the specified key.
|
| 97 | *
|
| 98 | * @param ht the hash table.
|
| 99 | * @param key the key to look for.
|
| 100 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
|
| 101 | * string length of the key.
|
| 102 | *
|
| 103 | * @return the value associated with the key, or NULL if the key is not found.
|
| 104 | */
|
| 105 | PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht,
|
| 106 | const void *key, unsigned keylen );
|
| 107 |
|
| 108 |
|
| 109 | /**
|
| 110 | * Associate/disassociate a value with the specified key.
|
| 111 | *
|
| 112 | * @param pool the pool to allocate the new entry if a new entry has to be
|
| 113 | * created.
|
| 114 | * @param ht the hash table.
|
| 115 | * @param key the key.
|
| 116 | * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
|
| 117 | * string length of the key.
|
| 118 | * @param value value to be associated, or NULL to delete the entry with
|
| 119 | * the specified key.
|
| 120 | */
|
| 121 | PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
|
| 122 | const void *key, unsigned keylen,
|
| 123 | void *value );
|
| 124 |
|
| 125 | /**
|
| 126 | * Get the total number of entries in the hash table.
|
| 127 | *
|
| 128 | * @param ht the hash table.
|
| 129 | *
|
| 130 | * @return the number of entries in the hash table.
|
| 131 | */
|
| 132 | PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
|
| 133 |
|
| 134 |
|
| 135 | /**
|
| 136 | * Get the iterator to the first element in the hash table.
|
| 137 | *
|
| 138 | * @param ht the hash table.
|
| 139 | * @param it the iterator for iterating hash elements.
|
| 140 | *
|
| 141 | * @return the iterator to the hash element, or NULL if no element presents.
|
| 142 | */
|
| 143 | PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
|
| 144 | pj_hash_iterator_t *it );
|
| 145 |
|
| 146 |
|
| 147 | /**
|
| 148 | * Get the next element from the iterator.
|
| 149 | *
|
| 150 | * @param ht the hash table.
|
| 151 | * @param it the hash iterator.
|
| 152 | *
|
| 153 | * @return the next iterator, or NULL if there's no more element.
|
| 154 | */
|
| 155 | PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
|
| 156 | pj_hash_iterator_t *it );
|
| 157 |
|
| 158 | /**
|
| 159 | * Get the value associated with a hash iterator.
|
| 160 | *
|
| 161 | * @param ht the hash table.
|
| 162 | * @param it the hash iterator.
|
| 163 | *
|
| 164 | * @return the value associated with the current element in iterator.
|
| 165 | */
|
| 166 | PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
|
| 167 | pj_hash_iterator_t *it );
|
| 168 |
|
| 169 |
|
| 170 | /**
|
| 171 | * @}
|
| 172 | */
|
| 173 |
|
| 174 | PJ_END_DECL
|
| 175 |
|
| 176 | #endif
|
| 177 |
|
| 178 |
|