1
/* $Id: hash.h 4208 2012-07-18 07:52:33Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
* @defgroup PJ_HASH Hash Table
36
* A hash table is a dictionary in which keys are mapped to array positions by
37
* hash functions. Having the keys of more than one item map to the same
38
* position is called a collision. In this library, we will chain the nodes
39
* that have the same key in a list.
43
* If this constant is used as keylen, then the key is interpreted as
44
* NULL terminated string.
46
#define PJ_HASH_KEY_STRING ((unsigned)-1)
49
* This indicates the size of of each hash entry.
51
#define PJ_HASH_ENTRY_BUF_SIZE (3*sizeof(void*) + 2*sizeof(pj_uint32_t))
54
* Type declaration for entry buffer, used by #pj_hash_set_np()
56
typedef void *pj_hash_entry_buf[(PJ_HASH_ENTRY_BUF_SIZE+sizeof(void*)-1)/(sizeof(void*))];
59
* This is the function that is used by the hash table to calculate hash value
60
* of the specified key.
62
* @param hval the initial hash value, or zero.
63
* @param key the key to calculate.
64
* @param keylen the length of the key, or PJ_HASH_KEY_STRING to treat
65
* the key as null terminated string.
67
* @return the hash value.
69
PJ_DECL(pj_uint32_t) pj_hash_calc(pj_uint32_t hval,
70
const void *key, unsigned keylen);
74
* Convert the key to lowercase and calculate the hash value. The resulting
75
* string is stored in \c result.
77
* @param hval The initial hash value, normally zero.
78
* @param result Optional. Buffer to store the result, which must be enough
80
* @param key The input key to be converted and calculated.
82
* @return The hash value.
84
PJ_DECL(pj_uint32_t) pj_hash_calc_tolower(pj_uint32_t hval,
89
* Create a hash table with the specified 'bucket' size.
91
* @param pool the pool from which the hash table will be allocated from.
92
* @param size the bucket size, which will be round-up to the nearest 2^n-1
94
* @return the hash table.
96
PJ_DECL(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size);
100
* Get the value associated with the specified key.
102
* @param ht the hash table.
103
* @param key the key to look for.
104
* @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
105
* string length of the key.
106
* @param hval if this argument is not NULL and the value is not zero,
107
* the value will be used as the computed hash value. If
108
* the argument is not NULL and the value is zero, it will
109
* be filled with the computed hash upon return.
111
* @return the value associated with the key, or NULL if the key is not found.
113
PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht,
114
const void *key, unsigned keylen,
119
* Variant of #pj_hash_get() with the key being converted to lowercase when
120
* calculating the hash value.
124
PJ_DECL(void *) pj_hash_get_lower( pj_hash_table_t *ht,
125
const void *key, unsigned keylen,
130
* Associate/disassociate a value with the specified key. If value is not
131
* NULL and entry already exists, the entry's value will be overwritten.
132
* If value is not NULL and entry does not exist, a new one will be created
133
* with the specified pool. Otherwise if value is NULL, entry will be
134
* deleted if it exists.
136
* @param pool the pool to allocate the new entry if a new entry has to be
138
* @param ht the hash table.
139
* @param key the key. If pool is not specified, the key MUST point to
140
* buffer that remains valid for the duration of the entry.
141
* @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
142
* string length of the key.
143
* @param hval if the value is not zero, then the hash table will use
144
* this value to search the entry's index, otherwise it will
145
* compute the key. This value can be obtained when calling
147
* @param value value to be associated, or NULL to delete the entry with
150
PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
151
const void *key, unsigned keylen, pj_uint32_t hval,
156
* Variant of #pj_hash_set() with the key being converted to lowercase when
157
* calculating the hash value.
161
PJ_DECL(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht,
162
const void *key, unsigned keylen,
163
pj_uint32_t hval, void *value );
167
* Associate/disassociate a value with the specified key. This function works
168
* like #pj_hash_set(), except that it doesn't use pool (hence the np -- no
169
* pool suffix). If new entry needs to be allocated, it will use the entry_buf.
171
* @param ht the hash table.
172
* @param key the key.
173
* @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
174
* string length of the key.
175
* @param hval if the value is not zero, then the hash table will use
176
* this value to search the entry's index, otherwise it will
177
* compute the key. This value can be obtained when calling
179
* @param entry_buf Buffer which will be used for the new entry, when one needs
181
* @param value value to be associated, or NULL to delete the entry with
184
PJ_DECL(void) pj_hash_set_np(pj_hash_table_t *ht,
185
const void *key, unsigned keylen,
186
pj_uint32_t hval, pj_hash_entry_buf entry_buf,
190
* Variant of #pj_hash_set_np() with the key being converted to lowercase
191
* when calculating the hash value.
193
* @see pj_hash_set_np()
195
PJ_DECL(void) pj_hash_set_np_lower(pj_hash_table_t *ht,
196
const void *key, unsigned keylen,
198
pj_hash_entry_buf entry_buf,
202
* Get the total number of entries in the hash table.
204
* @param ht the hash table.
206
* @return the number of entries in the hash table.
208
PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
212
* Get the iterator to the first element in the hash table.
214
* @param ht the hash table.
215
* @param it the iterator for iterating hash elements.
217
* @return the iterator to the hash element, or NULL if no element presents.
219
PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
220
pj_hash_iterator_t *it );
224
* Get the next element from the iterator.
226
* @param ht the hash table.
227
* @param it the hash iterator.
229
* @return the next iterator, or NULL if there's no more element.
231
PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
232
pj_hash_iterator_t *it );
235
* Get the value associated with a hash iterator.
237
* @param ht the hash table.
238
* @param it the hash iterator.
240
* @return the value associated with the current element in iterator.
242
PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
243
pj_hash_iterator_t *it );