1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more 2251875Speter * contributor license agreements. See the NOTICE file distributed with 3251875Speter * this work for additional information regarding copyright ownership. 4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0 5251875Speter * (the "License"); you may not use this file except in compliance with 6251875Speter * the License. You may obtain a copy of the License at 7251875Speter * 8251875Speter * http://www.apache.org/licenses/LICENSE-2.0 9251875Speter * 10251875Speter * Unless required by applicable law or agreed to in writing, software 11251875Speter * distributed under the License is distributed on an "AS IS" BASIS, 12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13251875Speter * See the License for the specific language governing permissions and 14251875Speter * limitations under the License. 15251875Speter */ 16251875Speter 17251875Speter#ifndef APR_HASH_H 18251875Speter#define APR_HASH_H 19251875Speter 20251875Speter/** 21251875Speter * @file apr_hash.h 22251875Speter * @brief APR Hash Tables 23251875Speter */ 24251875Speter 25251875Speter#include "apr_pools.h" 26251875Speter 27251875Speter#ifdef __cplusplus 28251875Speterextern "C" { 29251875Speter#endif 30251875Speter 31251875Speter/** 32251875Speter * @defgroup apr_hash Hash Tables 33251875Speter * @ingroup APR 34251875Speter * @{ 35251875Speter */ 36251875Speter 37251875Speter/** 38251875Speter * When passing a key to apr_hash_set or apr_hash_get, this value can be 39251875Speter * passed to indicate a string-valued key, and have apr_hash compute the 40251875Speter * length automatically. 41251875Speter * 42251875Speter * @remark apr_hash will use strlen(key) for the length. The NUL terminator 43251875Speter * is not included in the hash value (why throw a constant in?). 44251875Speter * Since the hash table merely references the provided key (rather 45251875Speter * than copying it), apr_hash_this() will return the NUL-term'd key. 46251875Speter */ 47251875Speter#define APR_HASH_KEY_STRING (-1) 48251875Speter 49251875Speter/** 50251875Speter * Abstract type for hash tables. 51251875Speter */ 52251875Spetertypedef struct apr_hash_t apr_hash_t; 53251875Speter 54251875Speter/** 55251875Speter * Abstract type for scanning hash tables. 56251875Speter */ 57251875Spetertypedef struct apr_hash_index_t apr_hash_index_t; 58251875Speter 59251875Speter/** 60251875Speter * Callback functions for calculating hash values. 61251875Speter * @param key The key. 62251875Speter * @param klen The length of the key, or APR_HASH_KEY_STRING to use the string 63251875Speter * length. If APR_HASH_KEY_STRING then returns the actual key length. 64251875Speter */ 65251875Spetertypedef unsigned int (*apr_hashfunc_t)(const char *key, apr_ssize_t *klen); 66251875Speter 67251875Speter/** 68251875Speter * The default hash function. 69251875Speter */ 70251875SpeterAPR_DECLARE_NONSTD(unsigned int) apr_hashfunc_default(const char *key, 71251875Speter apr_ssize_t *klen); 72251875Speter 73251875Speter/** 74251875Speter * Create a hash table. 75251875Speter * @param pool The pool to allocate the hash table out of 76251875Speter * @return The hash table just created 77251875Speter */ 78251875SpeterAPR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool); 79251875Speter 80251875Speter/** 81251875Speter * Create a hash table with a custom hash function 82251875Speter * @param pool The pool to allocate the hash table out of 83251875Speter * @param hash_func A custom hash function. 84251875Speter * @return The hash table just created 85251875Speter */ 86251875SpeterAPR_DECLARE(apr_hash_t *) apr_hash_make_custom(apr_pool_t *pool, 87251875Speter apr_hashfunc_t hash_func); 88251875Speter 89251875Speter/** 90251875Speter * Make a copy of a hash table 91251875Speter * @param pool The pool from which to allocate the new hash table 92251875Speter * @param h The hash table to clone 93251875Speter * @return The hash table just created 94251875Speter * @remark Makes a shallow copy 95251875Speter */ 96251875SpeterAPR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool, 97251875Speter const apr_hash_t *h); 98251875Speter 99251875Speter/** 100251875Speter * Associate a value with a key in a hash table. 101251875Speter * @param ht The hash table 102251875Speter * @param key Pointer to the key 103251875Speter * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length. 104251875Speter * @param val Value to associate with the key 105251875Speter * @remark If the value is NULL the hash entry is deleted. 106251875Speter */ 107251875SpeterAPR_DECLARE(void) apr_hash_set(apr_hash_t *ht, const void *key, 108251875Speter apr_ssize_t klen, const void *val); 109251875Speter 110251875Speter/** 111251875Speter * Look up the value associated with a key in a hash table. 112251875Speter * @param ht The hash table 113251875Speter * @param key Pointer to the key 114251875Speter * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length. 115251875Speter * @return Returns NULL if the key is not present. 116251875Speter */ 117251875SpeterAPR_DECLARE(void *) apr_hash_get(apr_hash_t *ht, const void *key, 118251875Speter apr_ssize_t klen); 119251875Speter 120251875Speter/** 121251875Speter * Start iterating over the entries in a hash table. 122251875Speter * @param p The pool to allocate the apr_hash_index_t iterator. If this 123251875Speter * pool is NULL, then an internal, non-thread-safe iterator is used. 124251875Speter * @param ht The hash table 125251875Speter * @return The iteration state 126251875Speter * @remark There is no restriction on adding or deleting hash entries during 127251875Speter * an iteration (although the results may be unpredictable unless all you do 128251875Speter * is delete the current entry) and multiple iterations can be in 129251875Speter * progress at the same time. 130251875Speter * 131251875Speter * @par Example: 132251875Speter * 133251875Speter * @code 134251875Speter * int sum_values(apr_pool_t *p, apr_hash_t *ht) 135251875Speter * { 136251875Speter * apr_hash_index_t *hi; 137251875Speter * void *val; 138251875Speter * int sum = 0; 139251875Speter * for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) { 140251875Speter * apr_hash_this(hi, NULL, NULL, &val); 141251875Speter * sum += *(int *)val; 142251875Speter * } 143251875Speter * return sum; 144251875Speter * } 145251875Speter * @endcode 146251875Speter */ 147251875SpeterAPR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht); 148251875Speter 149251875Speter/** 150251875Speter * Continue iterating over the entries in a hash table. 151251875Speter * @param hi The iteration state 152251875Speter * @return a pointer to the updated iteration state. NULL if there are no more 153251875Speter * entries. 154251875Speter */ 155251875SpeterAPR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi); 156251875Speter 157251875Speter/** 158251875Speter * Get the current entry's details from the iteration state. 159251875Speter * @param hi The iteration state 160251875Speter * @param key Return pointer for the pointer to the key. 161251875Speter * @param klen Return pointer for the key length. 162251875Speter * @param val Return pointer for the associated value. 163251875Speter * @remark The return pointers should point to a variable that will be set to the 164251875Speter * corresponding data, or they may be NULL if the data isn't interesting. 165251875Speter */ 166251875SpeterAPR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi, const void **key, 167251875Speter apr_ssize_t *klen, void **val); 168251875Speter 169251875Speter/** 170251875Speter * Get the number of key/value pairs in the hash table. 171251875Speter * @param ht The hash table 172251875Speter * @return The number of key/value pairs in the hash table. 173251875Speter */ 174251875SpeterAPR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht); 175251875Speter 176251875Speter/** 177251875Speter * Clear any key/value pairs in the hash table. 178251875Speter * @param ht The hash table 179251875Speter */ 180251875SpeterAPR_DECLARE(void) apr_hash_clear(apr_hash_t *ht); 181251875Speter 182251875Speter/** 183251875Speter * Merge two hash tables into one new hash table. The values of the overlay 184251875Speter * hash override the values of the base if both have the same key. Both 185251875Speter * hash tables must use the same hash function. 186251875Speter * @param p The pool to use for the new hash table 187251875Speter * @param overlay The table to add to the initial table 188251875Speter * @param base The table that represents the initial values of the new table 189251875Speter * @return A new hash table containing all of the data from the two passed in 190251875Speter */ 191251875SpeterAPR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p, 192251875Speter const apr_hash_t *overlay, 193251875Speter const apr_hash_t *base); 194251875Speter 195251875Speter/** 196251875Speter * Merge two hash tables into one new hash table. If the same key 197251875Speter * is present in both tables, call the supplied merge function to 198251875Speter * produce a merged value for the key in the new table. Both 199251875Speter * hash tables must use the same hash function. 200251875Speter * @param p The pool to use for the new hash table 201251875Speter * @param h1 The first of the tables to merge 202251875Speter * @param h2 The second of the tables to merge 203251875Speter * @param merger A callback function to merge values, or NULL to 204251875Speter * make values from h1 override values from h2 (same semantics as 205251875Speter * apr_hash_overlay()) 206251875Speter * @param data Client data to pass to the merger function 207251875Speter * @return A new hash table containing all of the data from the two passed in 208251875Speter */ 209251875SpeterAPR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p, 210251875Speter const apr_hash_t *h1, 211251875Speter const apr_hash_t *h2, 212251875Speter void * (*merger)(apr_pool_t *p, 213251875Speter const void *key, 214251875Speter apr_ssize_t klen, 215251875Speter const void *h1_val, 216251875Speter const void *h2_val, 217251875Speter const void *data), 218251875Speter const void *data); 219251875Speter 220251875Speter/** 221251875Speter * Declaration prototype for the iterator callback function of apr_hash_do(). 222251875Speter * 223251875Speter * @param rec The data passed as the first argument to apr_hash_[v]do() 224251875Speter * @param key The key from this iteration of the hash table 225251875Speter * @param klen The key length from this iteration of the hash table 226251875Speter * @param value The value from this iteration of the hash table 227251875Speter * @remark Iteration continues while this callback function returns non-zero. 228251875Speter * To export the callback function for apr_hash_do() it must be declared 229251875Speter * in the _NONSTD convention. 230251875Speter */ 231251875Spetertypedef int (apr_hash_do_callback_fn_t)(void *rec, const void *key, 232251875Speter apr_ssize_t klen, 233251875Speter const void *value); 234251875Speter 235251875Speter/** 236251875Speter * Iterate over a hash table running the provided function once for every 237251875Speter * element in the hash table. The @param comp function will be invoked for 238251875Speter * every element in the hash table. 239251875Speter * 240251875Speter * @param comp The function to run 241251875Speter * @param rec The data to pass as the first argument to the function 242251875Speter * @param ht The hash table to iterate over 243251875Speter * @return FALSE if one of the comp() iterations returned zero; TRUE if all 244251875Speter * iterations returned non-zero 245251875Speter * @see apr_hash_do_callback_fn_t 246251875Speter */ 247251875SpeterAPR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp, 248251875Speter void *rec, const apr_hash_t *ht); 249251875Speter 250251875Speter/** 251251875Speter * Get a pointer to the pool which the hash table was created in 252251875Speter */ 253251875SpeterAPR_POOL_DECLARE_ACCESSOR(hash); 254251875Speter 255251875Speter/** @} */ 256251875Speter 257251875Speter#ifdef __cplusplus 258251875Speter} 259251875Speter#endif 260251875Speter 261251875Speter#endif /* !APR_HASH_H */ 262