home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 November / CMCD1104.ISO / Software / Complet / Apache / apache_2.0.52-win32-x86-no_ssl.msi / Data.Cab / F277208_apr_hash.h < prev    next >
C/C++ Source or Header  |  2004-02-13  |  7KB  |  201 lines

  1. /* Copyright 2000-2004 The Apache Software Foundation
  2.  *
  3.  * Licensed under the Apache License, Version 2.0 (the "License");
  4.  * you may not use this file except in compliance with the License.
  5.  * You may obtain a copy of the License at
  6.  *
  7.  *     http://www.apache.org/licenses/LICENSE-2.0
  8.  *
  9.  * Unless required by applicable law or agreed to in writing, software
  10.  * distributed under the License is distributed on an "AS IS" BASIS,
  11.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.  * See the License for the specific language governing permissions and
  13.  * limitations under the License.
  14.  */
  15.  
  16. #ifndef APR_HASH_H
  17. #define APR_HASH_H
  18.  
  19. /**
  20.  * @file apr_hash.h
  21.  * @brief APR Hash Tables
  22.  */
  23.  
  24. #include "apr_pools.h"
  25.  
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29.  
  30. /**
  31.  * @defgroup apr_hash Hash Tables
  32.  * @ingroup APR 
  33.  * @{
  34.  */
  35.  
  36. /**
  37.  * When passing a key to apr_hash_set or apr_hash_get, this value can be
  38.  * passed to indicate a string-valued key, and have apr_hash compute the
  39.  * length automatically.
  40.  *
  41.  * @remark apr_hash will use strlen(key) for the length. The null-terminator
  42.  *         is not included in the hash value (why throw a constant in?).
  43.  *         Since the hash table merely references the provided key (rather
  44.  *         than copying it), apr_hash_this() will return the null-term'd key.
  45.  */
  46. #define APR_HASH_KEY_STRING     (-1)
  47.  
  48. /**
  49.  * Abstract type for hash tables.
  50.  */
  51. typedef struct apr_hash_t apr_hash_t;
  52.  
  53. /**
  54.  * Abstract type for scanning hash tables.
  55.  */
  56. typedef struct apr_hash_index_t apr_hash_index_t;
  57.  
  58. /**
  59.  * Create a hash table.
  60.  * @param pool The pool to allocate the hash table out of
  61.  * @return The hash table just created
  62.   */
  63. APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool);
  64.  
  65. /**
  66.  * Make a copy of a hash table
  67.  * @param pool The pool from which to allocate the new hash table
  68.  * @param h The hash table to clone
  69.  * @return The hash table just created
  70.  * @remark Makes a shallow copy
  71.  */
  72. APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
  73.                                         const apr_hash_t *h);
  74.  
  75. /**
  76.  * Associate a value with a key in a hash table.
  77.  * @param ht The hash table
  78.  * @param key Pointer to the key
  79.  * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  80.  * @param val Value to associate with the key
  81.  * @remark If the value is NULL the hash entry is deleted.
  82.  */
  83. APR_DECLARE(void) apr_hash_set(apr_hash_t *ht, const void *key,
  84.                                apr_ssize_t klen, const void *val);
  85.  
  86. /**
  87.  * Look up the value associated with a key in a hash table.
  88.  * @param ht The hash table
  89.  * @param key Pointer to the key
  90.  * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  91.  * @return Returns NULL if the key is not present.
  92.  */
  93. APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht, const void *key,
  94.                                  apr_ssize_t klen);
  95.  
  96. /**
  97.  * Start iterating over the entries in a hash table.
  98.  * @param p The pool to allocate the apr_hash_index_t iterator. If this
  99.  *          pool is NULL, then an internal, non-thread-safe iterator is used.
  100.  * @param ht The hash table
  101.  * @remark  There is no restriction on adding or deleting hash entries during
  102.  * an iteration (although the results may be unpredictable unless all you do
  103.  * is delete the current entry) and multiple iterations can be in
  104.  * progress at the same time.
  105.  
  106.  * @example
  107.  */
  108. /**
  109.  * <PRE>
  110.  * 
  111.  * int sum_values(apr_pool_t *p, apr_hash_t *ht)
  112.  * {
  113.  *     apr_hash_index_t *hi;
  114.  *     void *val;
  115.  *     int sum = 0;
  116.  *     for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
  117.  *         apr_hash_this(hi, NULL, NULL, &val);
  118.  *         sum += *(int *)val;
  119.  *     }
  120.  *     return sum;
  121.  * }
  122.  * </PRE>
  123.  */
  124. APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht);
  125.  
  126. /**
  127.  * Continue iterating over the entries in a hash table.
  128.  * @param hi The iteration state
  129.  * @return a pointer to the updated iteration state.  NULL if there are no more  
  130.  *         entries.
  131.  */
  132. APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi);
  133.  
  134. /**
  135.  * Get the current entry's details from the iteration state.
  136.  * @param hi The iteration state
  137.  * @param key Return pointer for the pointer to the key.
  138.  * @param klen Return pointer for the key length.
  139.  * @param val Return pointer for the associated value.
  140.  * @remark The return pointers should point to a variable that will be set to the
  141.  *         corresponding data, or they may be NULL if the data isn't interesting.
  142.  */
  143. APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi, const void **key, 
  144.                                 apr_ssize_t *klen, void **val);
  145.  
  146. /**
  147.  * Get the number of key/value pairs in the hash table.
  148.  * @param ht The hash table
  149.  * @return The number of key/value pairs in the hash table.
  150.  */
  151. APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht);
  152.  
  153. /**
  154.  * Merge two hash tables into one new hash table. The values of the overlay
  155.  * hash override the values of the base if both have the same key.
  156.  * @param p The pool to use for the new hash table
  157.  * @param overlay The table to add to the initial table
  158.  * @param base The table that represents the initial values of the new table
  159.  * @return A new hash table containing all of the data from the two passed in
  160.  */
  161. APR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p,
  162.                                            const apr_hash_t *overlay, 
  163.                                            const apr_hash_t *base);
  164.  
  165. /**
  166.  * Merge two hash tables into one new hash table. If the same key
  167.  * is present in both tables, call the supplied merge function to
  168.  * produce a merged value for the key in the new table.
  169.  * @param p The pool to use for the new hash table
  170.  * @param h1 The first of the tables to merge
  171.  * @param h2 The second of the tables to merge
  172.  * @param merger A callback function to merge values, or NULL to
  173.  *  make values from h1 override values from h2 (same semantics as
  174.  *  apr_hash_overlay())
  175.  * @param data Client data to pass to the merger function
  176.  * @return A new hash table containing all of the data from the two passed in
  177.  */
  178. APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
  179.                                          const apr_hash_t *h1,
  180.                                          const apr_hash_t *h2,
  181.                                          void * (*merger)(apr_pool_t *p,
  182.                                                      const void *key,
  183.                                                      apr_ssize_t klen,
  184.                                                      const void *h1_val,
  185.                                                      const void *h2_val,
  186.                                                      const void *data),
  187.                                          const void *data);
  188.  
  189. /**
  190.  * Get a pointer to the pool which the hash table was created in
  191.  */
  192. APR_POOL_DECLARE_ACCESSOR(hash);
  193.  
  194. /** @} */
  195.  
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199.  
  200. #endif    /* !APR_HASH_H */
  201.