Debian Codegraph @

Xpjproject/pjlib/include/pj/hash.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 /**
48 * This indicates the size of of each hash entry.
49 */
50 #define PJ_HASH_ENTRY_BUF_SIZE (3*sizeof(void*) + 2*sizeof(pj_uint32_t))
51
52 /**
53 * Type declaration for entry buffer, used by #pj_hash_set_np()
54 */
55 typedef void *pj_hash_entry_buf[(PJ_HASH_ENTRY_BUF_SIZE+sizeof(void*)-1)/(sizeof(void*))];
56
57 /**
58 * This is the function that is used by the hash table to calculate hash value
59 * of the specified key.
60 *
61 * @param hval the initial hash value, or zero.
62 * @param key the key to calculate.
63 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to treat
64 * the key as null terminated string.
65 *
66 * @return the hash value.
67 */
68 PJ_DECL(pj_uint32_t) pj_hash_calc(pj_uint32_t hval,
69 const void *key, unsigned keylen);
70
71
72 /**
73 * Convert the key to lowercase and calculate the hash value. The resulting
74 * string is stored in \c result.
75 *
76 * @param hval The initial hash value, normally zero.
77 * @param result Optional. Buffer to store the result, which must be enough
78 * to hold the string.
79 * @param key The input key to be converted and calculated.
80 *
81 * @return The hash value.
82 */
83 PJ_DECL(pj_uint32_t) pj_hash_calc_tolower(pj_uint32_t hval,
84 char *result,
85 const pj_str_t *key);
86
87 /**
88 * Create a hash table with the specified 'bucket' size.
89 *
90 * @param pool the pool from which the hash table will be allocated from.
91 * @param size the bucket size, which will be round-up to the nearest 2^n-1
92 *
93 * @return the hash table.
94 */
95 PJ_DECL(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size);
96
97
98 /**
99 * Get the value associated with the specified key.
100 *
101 * @param ht the hash table.
102 * @param key the key to look for.
103 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
104 * string length of the key.
105 * @param hval if this argument is not NULL and the value is not zero,
106 * the value will be used as the computed hash value. If
107 * the argument is not NULL and the value is zero, it will
108 * be filled with the computed hash upon return.
109 *
110 * @return the value associated with the key, or NULL if the key is not found.
111 */
112 PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht,
113 const void *key, unsigned keylen,
114 pj_uint32_t *hval );
115
116
117 /**
118 * Variant of #pj_hash_get() with the key being converted to lowercase when
119 * calculating the hash value.
120 *
121 * @see pj_hash_get()
122 */
123 PJ_DECL(void *) pj_hash_get_lower( pj_hash_table_t *ht,
124 const void *key, unsigned keylen,
125 pj_uint32_t *hval );
126
127
128 /**
129 * Associate/disassociate a value with the specified key. If value is not
130 * NULL and entry already exists, the entry's value will be overwritten.
131 * If value is not NULL and entry does not exist, a new one will be created
132 * with the specified pool. Otherwise if value is NULL, entry will be
133 * deleted if it exists.
134 *
135 * @param pool the pool to allocate the new entry if a new entry has to be
136 * created.
137 * @param ht the hash table.
138 * @param key the key. If pool is not specified, the key MUST point to
139 * buffer that remains valid for the duration of the entry.
140 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
141 * string length of the key.
142 * @param hval if the value is not zero, then the hash table will use
143 * this value to search the entry's index, otherwise it will
144 * compute the key. This value can be obtained when calling
145 * #pj_hash_get().
146 * @param value value to be associated, or NULL to delete the entry with
147 * the specified key.
148 */
149 PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
150 const void *key, unsigned keylen, pj_uint32_t hval,
151 void *value );
152
153
154 /**
155 * Variant of #pj_hash_set() with the key being converted to lowercase when
156 * calculating the hash value.
157 *
158 * @see pj_hash_set()
159 */
160 PJ_DECL(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht,
161 const void *key, unsigned keylen,
162 pj_uint32_t hval, void *value );
163
164
165 /**
166 * Associate/disassociate a value with the specified key. This function works
167 * like #pj_hash_set(), except that it doesn't use pool (hence the np -- no
168 * pool suffix). If new entry needs to be allocated, it will use the entry_buf.
169 *
170 * @param ht the hash table.
171 * @param key the key.
172 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
173 * string length of the key.
174 * @param hval if the value is not zero, then the hash table will use
175 * this value to search the entry's index, otherwise it will
176 * compute the key. This value can be obtained when calling
177 * #pj_hash_get().
178 * @param entry_buf Buffer which will be used for the new entry, when one needs
179 * to be created.
180 * @param value value to be associated, or NULL to delete the entry with
181 * the specified key.
182 */
183 PJ_DECL(void) pj_hash_set_np(pj_hash_table_t *ht,
184 const void *key, unsigned keylen,
185 pj_uint32_t hval, pj_hash_entry_buf entry_buf,
186 void *value);
187
188 /**
189 * Variant of #pj_hash_set_np() with the key being converted to lowercase
190 * when calculating the hash value.
191 *
192 * @see pj_hash_set_np()
193 */
194 PJ_DECL(void) pj_hash_set_np_lower(pj_hash_table_t *ht,
195 const void *key, unsigned keylen,
196 pj_uint32_t hval,
197 pj_hash_entry_buf entry_buf,
198 void *value);
199
200 /**
201 * Get the total number of entries in the hash table.
202 *
203 * @param ht the hash table.
204 *
205 * @return the number of entries in the hash table.
206 */
207 PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
208
209
210 /**
211 * Get the iterator to the first element in the hash table.
212 *
213 * @param ht the hash table.
214 * @param it the iterator for iterating hash elements.
215 *
216 * @return the iterator to the hash element, or NULL if no element presents.
217 */
218 PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
219 pj_hash_iterator_t *it );
220
221
222 /**
223 * Get the next element from the iterator.
224 *
225 * @param ht the hash table.
226 * @param it the hash iterator.
227 *
228 * @return the next iterator, or NULL if there's no more element.
229 */
230 PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
231 pj_hash_iterator_t *it );
232
233 /**
234 * Get the value associated with a hash iterator.
235 *
236 * @param ht the hash table.
237 * @param it the hash iterator.
238 *
239 * @return the value associated with the current element in iterator.
240 */
241 PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
242 pj_hash_iterator_t *it );
243
244
245 /**
246 * @}
247 */
248
249 PJ_END_DECL
250
251 #endif
252
253