Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

apr_hash.h

Go to the documentation of this file.
00001 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
00002  * applicable.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef APR_HASH_H
00018 #define APR_HASH_H
00019 
00020 /**
00021  * @file apr_hash.h
00022  * @brief APR Hash Tables
00023  */
00024 
00025 #include "apr_pools.h"
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00031 /**
00032  * @defgroup apr_hash Hash Tables
00033  * @ingroup APR 
00034  * @{
00035  */
00036 
00037 /**
00038  * When passing a key to apr_hash_set or apr_hash_get, this value can be
00039  * passed to indicate a string-valued key, and have apr_hash compute the
00040  * length automatically.
00041  *
00042  * @remark apr_hash will use strlen(key) for the length. The NUL terminator
00043  *         is not included in the hash value (why throw a constant in?).
00044  *         Since the hash table merely references the provided key (rather
00045  *         than copying it), apr_hash_this() will return the NUL-term'd key.
00046  */
00047 #define APR_HASH_KEY_STRING     (-1)
00048 
00049 /**
00050  * Abstract type for hash tables.
00051  */
00052 typedef struct apr_hash_t apr_hash_t;
00053 
00054 /**
00055  * Abstract type for scanning hash tables.
00056  */
00057 typedef struct apr_hash_index_t apr_hash_index_t;
00058 
00059 /**
00060  * Callback functions for calculating hash values.
00061  * @param key The key.
00062  * @param klen The length of the key, or APR_HASH_KEY_STRING to use the string 
00063  *             length. If APR_HASH_KEY_STRING then returns the actual key length.
00064  */
00065 typedef unsigned int (*apr_hashfunc_t)(const char *key, apr_ssize_t *klen);
00066 
00067 /**
00068  * The default hash function.
00069  */
00070 unsigned int apr_hashfunc_default(const char *key, apr_ssize_t *klen);
00071 
00072 /**
00073  * Create a hash table.
00074  * @param pool The pool to allocate the hash table out of
00075  * @return The hash table just created
00076   */
00077 APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool);
00078 
00079 /**
00080  * Create a hash table with a custom hash function
00081  * @param pool The pool to allocate the hash table out of
00082  * @param hash_func A custom hash function.
00083  * @return The hash table just created
00084   */
00085 APR_DECLARE(apr_hash_t *) apr_hash_make_custom(apr_pool_t *pool, 
00086                                                apr_hashfunc_t hash_func);
00087 
00088 /**
00089  * Make a copy of a hash table
00090  * @param pool The pool from which to allocate the new hash table
00091  * @param h The hash table to clone
00092  * @return The hash table just created
00093  * @remark Makes a shallow copy
00094  */
00095 APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
00096                                         const apr_hash_t *h);
00097 
00098 /**
00099  * Associate a value with a key in a hash table.
00100  * @param ht The hash table
00101  * @param key Pointer to the key
00102  * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
00103  * @param val Value to associate with the key
00104  * @remark If the value is NULL the hash entry is deleted.
00105  */
00106 APR_DECLARE(void) apr_hash_set(apr_hash_t *ht, const void *key,
00107                                apr_ssize_t klen, const void *val);
00108 
00109 /**
00110  * Look up the value associated with a key in a hash table.
00111  * @param ht The hash table
00112  * @param key Pointer to the key
00113  * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
00114  * @return Returns NULL if the key is not present.
00115  */
00116 APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht, const void *key,
00117                                  apr_ssize_t klen);
00118 
00119 /**
00120  * Start iterating over the entries in a hash table.
00121  * @param p The pool to allocate the apr_hash_index_t iterator. If this
00122  *          pool is NULL, then an internal, non-thread-safe iterator is used.
00123  * @param ht The hash table
00124  * @remark  There is no restriction on adding or deleting hash entries during
00125  * an iteration (although the results may be unpredictable unless all you do
00126  * is delete the current entry) and multiple iterations can be in
00127  * progress at the same time.
00128 
00129  * @example
00130  */
00131 /**
00132  * <PRE>
00133  * 
00134  * int sum_values(apr_pool_t *p, apr_hash_t *ht)
00135  * {
00136  *     apr_hash_index_t *hi;
00137  *     void *val;
00138  *     int sum = 0;
00139  *     for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
00140  *         apr_hash_this(hi, NULL, NULL, &val);
00141  *         sum += *(int *)val;
00142  *     }
00143  *     return sum;
00144  * }
00145  * </PRE>
00146  */
00147 APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht);
00148 
00149 /**
00150  * Continue iterating over the entries in a hash table.
00151  * @param hi The iteration state
00152  * @return a pointer to the updated iteration state.  NULL if there are no more  
00153  *         entries.
00154  */
00155 APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi);
00156 
00157 /**
00158  * Get the current entry's details from the iteration state.
00159  * @param hi The iteration state
00160  * @param key Return pointer for the pointer to the key.
00161  * @param klen Return pointer for the key length.
00162  * @param val Return pointer for the associated value.
00163  * @remark The return pointers should point to a variable that will be set to the
00164  *         corresponding data, or they may be NULL if the data isn't interesting.
00165  */
00166 APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi, const void **key, 
00167                                 apr_ssize_t *klen, void **val);
00168 
00169 /**
00170  * Get the number of key/value pairs in the hash table.
00171  * @param ht The hash table
00172  * @return The number of key/value pairs in the hash table.
00173  */
00174 APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht);
00175 
00176 /**
00177  * Merge two hash tables into one new hash table. The values of the overlay
00178  * hash override the values of the base if both have the same key.  Both
00179  * hash tables must use the same hash function.
00180  * @param p The pool to use for the new hash table
00181  * @param overlay The table to add to the initial table
00182  * @param base The table that represents the initial values of the new table
00183  * @return A new hash table containing all of the data from the two passed in
00184  */
00185 APR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p,
00186                                            const apr_hash_t *overlay, 
00187                                            const apr_hash_t *base);
00188 
00189 /**
00190  * Merge two hash tables into one new hash table. If the same key
00191  * is present in both tables, call the supplied merge function to
00192  * produce a merged value for the key in the new table.  Both
00193  * hash tables must use the same hash function.
00194  * @param p The pool to use for the new hash table
00195  * @param h1 The first of the tables to merge
00196  * @param h2 The second of the tables to merge
00197  * @param merger A callback function to merge values, or NULL to
00198  *  make values from h1 override values from h2 (same semantics as
00199  *  apr_hash_overlay())
00200  * @param data Client data to pass to the merger function
00201  * @return A new hash table containing all of the data from the two passed in
00202  */
00203 APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
00204                                          const apr_hash_t *h1,
00205                                          const apr_hash_t *h2,
00206                                          void * (*merger)(apr_pool_t *p,
00207                                                      const void *key,
00208                                                      apr_ssize_t klen,
00209                                                      const void *h1_val,
00210                                                      const void *h2_val,
00211                                                      const void *data),
00212                                          const void *data);
00213 
00214 /**
00215  * Get a pointer to the pool which the hash table was created in
00216  */
00217 APR_POOL_DECLARE_ACCESSOR(hash);
00218 
00219 /** @} */
00220 
00221 #ifdef __cplusplus
00222 }
00223 #endif
00224 
00225 #endif  /* !APR_HASH_H */

Generated on Sun Nov 20 18:48:03 2005 for Apache Portable Runtime by doxygen 1.3.6