1
/* $Id: hash.h 3841 2011-10-24 09:28:13Z 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 Buffer to store the result, which must be enough to hold
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
* Associate/disassociate a value with the specified key. If value is not
120
* NULL and entry already exists, the entry's value will be overwritten.
121
* If value is not NULL and entry does not exist, a new one will be created
122
* with the specified pool. Otherwise if value is NULL, entry will be
123
* deleted if it exists.
125
* @param pool the pool to allocate the new entry if a new entry has to be
127
* @param ht the hash table.
128
* @param key the key. If pool is not specified, the key MUST point to
129
* buffer that remains valid for the duration of the entry.
130
* @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
131
* string length of the key.
132
* @param hval if the value is not zero, then the hash table will use
133
* this value to search the entry's index, otherwise it will
134
* compute the key. This value can be obtained when calling
136
* @param value value to be associated, or NULL to delete the entry with
139
PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
140
const void *key, unsigned keylen, pj_uint32_t hval,
145
* Associate/disassociate a value with the specified key. This function works
146
* like #pj_hash_set(), except that it doesn't use pool (hence the np -- no
147
* pool suffix). If new entry needs to be allocated, it will use the entry_buf.
149
* @param ht the hash table.
150
* @param key the key.
151
* @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
152
* string length of the key.
153
* @param hval if the value is not zero, then the hash table will use
154
* this value to search the entry's index, otherwise it will
155
* compute the key. This value can be obtained when calling
157
* @param entry_buf Buffer which will be used for the new entry, when one needs
159
* @param value value to be associated, or NULL to delete the entry with
162
PJ_DECL(void) pj_hash_set_np(pj_hash_table_t *ht,
163
const void *key, unsigned keylen,
164
pj_uint32_t hval, pj_hash_entry_buf entry_buf,
168
* Get the total number of entries in the hash table.
170
* @param ht the hash table.
172
* @return the number of entries in the hash table.
174
PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
178
* Get the iterator to the first element in the hash table.
180
* @param ht the hash table.
181
* @param it the iterator for iterating hash elements.
183
* @return the iterator to the hash element, or NULL if no element presents.
185
PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
186
pj_hash_iterator_t *it );
190
* Get the next element from the iterator.
192
* @param ht the hash table.
193
* @param it the hash iterator.
195
* @return the next iterator, or NULL if there's no more element.
197
PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
198
pj_hash_iterator_t *it );
201
* Get the value associated with a hash iterator.
203
* @param ht the hash table.
204
* @param it the hash iterator.
206
* @return the value associated with the current element in iterator.
208
PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
209
pj_hash_iterator_t *it );