blob: f033ab75036ee83608321c1927811ba2ee7f19e9 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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
29PJ_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 Prijono40f2f642006-01-30 18:40:05 +000048 * This indicates the size of of each hash entry.
49 */
Benny Prijono17e58ed2007-05-28 11:49:46 +000050#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 */
55typedef void *pj_hash_entry_buf[(PJ_HASH_ENTRY_BUF_SIZE+sizeof(void*)-1)/(sizeof(void*))];
Benny Prijono40f2f642006-01-30 18:40:05 +000056
57/**
Benny Prijono9033e312005-11-21 02:08:39 +000058 * 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 */
68PJ_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 Buffer to store the result, which must be enough to hold
78 * the string.
79 * @param key The input key to be converted and calculated.
80 *
81 * @return The hash value.
82 */
83PJ_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.
Benny Prijono3ae5f062006-10-03 17:13:22 +000091 * @param size the bucket size, which will be round-up to the nearest 2^n-1
Benny Prijono9033e312005-11-21 02:08:39 +000092 *
93 * @return the hash table.
94 */
95PJ_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.
Benny Prijono40f2f642006-01-30 18:40:05 +0000105 * @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.
Benny Prijono9033e312005-11-21 02:08:39 +0000109 *
110 * @return the value associated with the key, or NULL if the key is not found.
111 */
112PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000113 const void *key, unsigned keylen,
114 pj_uint32_t *hval );
Benny Prijono9033e312005-11-21 02:08:39 +0000115
116
117/**
Benny Prijono40f2f642006-01-30 18:40:05 +0000118 * Associate/disassociate a value with the specified key. If value is not
119 * NULL and entry already exists, the entry's value will be overwritten.
120 * If value is not NULL and entry does not exist, a new one will be created
121 * with the specified pool. Otherwise if value is NULL, entry will be
122 * deleted if it exists.
Benny Prijono9033e312005-11-21 02:08:39 +0000123 *
124 * @param pool the pool to allocate the new entry if a new entry has to be
125 * created.
126 * @param ht the hash table.
Benny Prijonod5971742007-03-10 23:15:36 +0000127 * @param key the key, which MUST point to buffer that remains valid
128 * for the duration of the entry.
Benny Prijono9033e312005-11-21 02:08:39 +0000129 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
130 * string length of the key.
Benny Prijono40f2f642006-01-30 18:40:05 +0000131 * @param hval if the value is not zero, then the hash table will use
132 * this value to search the entry's index, otherwise it will
133 * compute the key. This value can be obtained when calling
134 * #pj_hash_get().
Benny Prijono9033e312005-11-21 02:08:39 +0000135 * @param value value to be associated, or NULL to delete the entry with
136 * the specified key.
137 */
138PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
Benny Prijono40f2f642006-01-30 18:40:05 +0000139 const void *key, unsigned keylen, pj_uint32_t hval,
Benny Prijono9033e312005-11-21 02:08:39 +0000140 void *value );
141
Benny Prijono40f2f642006-01-30 18:40:05 +0000142
143/**
144 * Associate/disassociate a value with the specified key. This function works
145 * like #pj_hash_set(), except that it doesn't use pool (hence the np -- no
146 * pool suffix). If new entry needs to be allocated, it will use the entry_buf.
147 *
148 * @param ht the hash table.
149 * @param key the key.
150 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
151 * string length of the key.
152 * @param hval if the value is not zero, then the hash table will use
153 * this value to search the entry's index, otherwise it will
154 * compute the key. This value can be obtained when calling
155 * #pj_hash_get().
Benny Prijono17e58ed2007-05-28 11:49:46 +0000156 * @param entry_buf Buffer which will be used for the new entry, when one needs
157 * to be created.
Benny Prijono40f2f642006-01-30 18:40:05 +0000158 * @param value value to be associated, or NULL to delete the entry with
159 * the specified key.
160 */
161PJ_DECL(void) pj_hash_set_np(pj_hash_table_t *ht,
162 const void *key, unsigned keylen,
Benny Prijono17e58ed2007-05-28 11:49:46 +0000163 pj_uint32_t hval, pj_hash_entry_buf entry_buf,
164 void *value);
Benny Prijono40f2f642006-01-30 18:40:05 +0000165
Benny Prijono9033e312005-11-21 02:08:39 +0000166/**
167 * Get the total number of entries in the hash table.
168 *
169 * @param ht the hash table.
170 *
171 * @return the number of entries in the hash table.
172 */
173PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
174
175
176/**
177 * Get the iterator to the first element in the hash table.
178 *
179 * @param ht the hash table.
180 * @param it the iterator for iterating hash elements.
181 *
182 * @return the iterator to the hash element, or NULL if no element presents.
183 */
184PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
185 pj_hash_iterator_t *it );
186
187
188/**
189 * Get the next element from the iterator.
190 *
191 * @param ht the hash table.
192 * @param it the hash iterator.
193 *
194 * @return the next iterator, or NULL if there's no more element.
195 */
196PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
197 pj_hash_iterator_t *it );
198
199/**
200 * Get the value associated with a hash iterator.
201 *
202 * @param ht the hash table.
203 * @param it the hash iterator.
204 *
205 * @return the value associated with the current element in iterator.
206 */
207PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
208 pj_hash_iterator_t *it );
209
210
211/**
212 * @}
213 */
214
215PJ_END_DECL
216
217#endif
218
219