1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 * @brief APR Hash Tables
25 #include "apr_pools.h"
32 * @defgroup apr_hash Hash Tables
38 * When passing a key to apr_hash_set or apr_hash_get, this value can be
39 * passed to indicate a string-valued key, and have apr_hash compute the
40 * length automatically.
42 * @remark apr_hash will use strlen(key) for the length. The null-terminator
43 * is not included in the hash value (why throw a constant in?).
44 * Since the hash table merely references the provided key (rather
45 * than copying it), apr_hash_this() will return the null-term'd key.
47 #define APR_HASH_KEY_STRING (-1)
50 * Abstract type for hash tables.
52 typedef struct apr_hash_t apr_hash_t;
55 * Abstract type for scanning hash tables.
57 typedef struct apr_hash_index_t apr_hash_index_t;
60 * Create a hash table.
61 * @param pool The pool to allocate the hash table out of
62 * @return The hash table just created
64 APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool);
67 * Make a copy of a hash table
68 * @param pool The pool from which to allocate the new hash table
69 * @param h The hash table to clone
70 * @return The hash table just created
71 * @remark Makes a shallow copy
73 APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
77 * Associate a value with a key in a hash table.
78 * @param ht The hash table
79 * @param key Pointer to the key
80 * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
81 * @param val Value to associate with the key
82 * @remark If the value is NULL the hash entry is deleted.
84 APR_DECLARE(void) apr_hash_set(apr_hash_t *ht, const void *key,
85 apr_ssize_t klen, const void *val);
88 * Look up the value associated with a key in a hash table.
89 * @param ht The hash table
90 * @param key Pointer to the key
91 * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
92 * @return Returns NULL if the key is not present.
94 APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht, const void *key,
98 * Start iterating over the entries in a hash table.
99 * @param p The pool to allocate the apr_hash_index_t iterator. If this
100 * pool is NULL, then an internal, non-thread-safe iterator is used.
101 * @param ht The hash table
102 * @remark There is no restriction on adding or deleting hash entries during
103 * an iteration (although the results may be unpredictable unless all you do
104 * is delete the current entry) and multiple iterations can be in
105 * progress at the same time.
112 * int sum_values(apr_pool_t *p, apr_hash_t *ht)
114 * apr_hash_index_t *hi;
117 * for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
118 * apr_hash_this(hi, NULL, NULL, &val);
119 * sum += *(int *)val;
125 APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht);
128 * Continue iterating over the entries in a hash table.
129 * @param hi The iteration state
130 * @return a pointer to the updated iteration state. NULL if there are no more
133 APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi);
136 * Get the current entry's details from the iteration state.
137 * @param hi The iteration state
138 * @param key Return pointer for the pointer to the key.
139 * @param klen Return pointer for the key length.
140 * @param val Return pointer for the associated value.
141 * @remark The return pointers should point to a variable that will be set to the
142 * corresponding data, or they may be NULL if the data isn't interesting.
144 APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi, const void **key,
145 apr_ssize_t *klen, void **val);
148 * Get the number of key/value pairs in the hash table.
149 * @param ht The hash table
150 * @return The number of key/value pairs in the hash table.
152 APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht);
155 * Merge two hash tables into one new hash table. The values of the overlay
156 * hash override the values of the base if both have the same key.
157 * @param p The pool to use for the new hash table
158 * @param overlay The table to add to the initial table
159 * @param base The table that represents the initial values of the new table
160 * @return A new hash table containing all of the data from the two passed in
162 APR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p,
163 const apr_hash_t *overlay,
164 const apr_hash_t *base);
167 * Merge two hash tables into one new hash table. If the same key
168 * is present in both tables, call the supplied merge function to
169 * produce a merged value for the key in the new table.
170 * @param p The pool to use for the new hash table
171 * @param h1 The first of the tables to merge
172 * @param h2 The second of the tables to merge
173 * @param merger A callback function to merge values, or NULL to
174 * make values from h1 override values from h2 (same semantics as
175 * apr_hash_overlay())
176 * @param data Client data to pass to the merger function
177 * @return A new hash table containing all of the data from the two passed in
179 APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
180 const apr_hash_t *h1,
181 const apr_hash_t *h2,
182 void * (*merger)(apr_pool_t *p,
191 * Get a pointer to the pool which the hash table was created in
193 APR_POOL_DECLARE_ACCESSOR(hash);
201 #endif /* !APR_HASH_H */