blob: cee8e71fd514e343a915fa4de9b6ad15b2933f7b [file] [log] [blame]
Benny Prijonoe0312a72005-11-18 00:16:43 +00001/* $Id$ */
Benny Prijonoe7224612005-11-13 19:40:44 +00002/*
Benny Prijonoe0312a72005-11-18 00:16:43 +00003 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
Benny Prijonoe7224612005-11-13 19:40:44 +00004 *
Benny Prijonoe0312a72005-11-18 00:16:43 +00005 * 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.
Benny Prijonoe7224612005-11-13 19:40:44 +00009 *
Benny Prijonoe0312a72005-11-18 00:16:43 +000010 * This program is distributed in the hope that it will be useful,
Benny Prijonoe7224612005-11-13 19:40:44 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Benny Prijonoe0312a72005-11-18 00:16:43 +000012 * 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
Benny Prijonoe7224612005-11-13 19:40:44 +000018 */
Benny Prijonoe7224612005-11-13 19:40:44 +000019#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/**
48 * This is the function that is used by the hash table to calculate hash value
49 * of the specified key.
50 *
51 * @param hval the initial hash value, or zero.
52 * @param key the key to calculate.
53 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to treat
54 * the key as null terminated string.
55 *
56 * @return the hash value.
57 */
58PJ_DECL(pj_uint32_t) pj_hash_calc(pj_uint32_t hval,
59 const void *key, unsigned keylen);
60
Benny Prijonof010e692005-11-11 19:01:31 +000061
62/**
63 * Convert the key to lowercase and calculate the hash value. The resulting
64 * string is stored in \c result.
65 *
66 * @param hval The initial hash value, normally zero.
67 * @param result Buffer to store the result, which must be enough to hold
68 * the string.
69 * @param key The input key to be converted and calculated.
70 *
71 * @return The hash value.
72 */
73PJ_DECL(pj_uint32_t) pj_hash_calc_tolower(pj_uint32_t hval,
74 char *result,
75 const pj_str_t *key);
Benny Prijonoe7224612005-11-13 19:40:44 +000076
77/**
78 * Create a hash table with the specified 'bucket' size.
79 *
80 * @param pool the pool from which the hash table will be allocated from.
81 * @param size the bucket size, which will be round-up to the nearest 2^n+1
82 *
83 * @return the hash table.
84 */
85PJ_DECL(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size);
86
87
88/**
89 * Get the value associated with the specified key.
90 *
91 * @param ht the hash table.
92 * @param key the key to look for.
93 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
94 * string length of the key.
95 *
96 * @return the value associated with the key, or NULL if the key is not found.
97 */
98PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht,
99 const void *key, unsigned keylen );
100
101
102/**
103 * Associate/disassociate a value with the specified key.
104 *
105 * @param pool the pool to allocate the new entry if a new entry has to be
106 * created.
107 * @param ht the hash table.
108 * @param key the key.
109 * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
110 * string length of the key.
111 * @param value value to be associated, or NULL to delete the entry with
112 * the specified key.
113 */
114PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
115 const void *key, unsigned keylen,
116 void *value );
117
118/**
119 * Get the total number of entries in the hash table.
120 *
121 * @param ht the hash table.
122 *
123 * @return the number of entries in the hash table.
124 */
125PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
126
127
128/**
129 * Get the iterator to the first element in the hash table.
130 *
131 * @param ht the hash table.
132 * @param it the iterator for iterating hash elements.
133 *
134 * @return the iterator to the hash element, or NULL if no element presents.
135 */
136PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
137 pj_hash_iterator_t *it );
138
139
140/**
141 * Get the next element from the iterator.
142 *
143 * @param ht the hash table.
144 * @param it the hash iterator.
145 *
146 * @return the next iterator, or NULL if there's no more element.
147 */
148PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
149 pj_hash_iterator_t *it );
150
151/**
152 * Get the value associated with a hash iterator.
153 *
154 * @param ht the hash table.
155 * @param it the hash iterator.
156 *
157 * @return the value associated with the current element in iterator.
158 */
159PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
160 pj_hash_iterator_t *it );
161
162
163/**
164 * @}
165 */
166
167PJ_END_DECL
168
169#endif
170
171