apr_hash.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #ifndef APR_HASH_H
  17. #define APR_HASH_H
  18. /**
  19. * @file apr_hash.h
  20. * @brief APR Hash Tables
  21. */
  22. #include "apr_pools.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * @defgroup apr_hash Hash Tables
  28. * @ingroup APR
  29. * @{
  30. */
  31. /**
  32. * When passing a key to apr_hash_set or apr_hash_get, this value can be
  33. * passed to indicate a string-valued key, and have apr_hash compute the
  34. * length automatically.
  35. *
  36. * @remark apr_hash will use strlen(key) for the length. The NUL terminator
  37. * is not included in the hash value (why throw a constant in?).
  38. * Since the hash table merely references the provided key (rather
  39. * than copying it), apr_hash_this() will return the NUL-term'd key.
  40. */
  41. #define APR_HASH_KEY_STRING (-1)
  42. /**
  43. * Abstract type for hash tables.
  44. */
  45. typedef struct apr_hash_t apr_hash_t;
  46. /**
  47. * Abstract type for scanning hash tables.
  48. */
  49. typedef struct apr_hash_index_t apr_hash_index_t;
  50. /**
  51. * Callback functions for calculating hash values.
  52. * @param key The key.
  53. * @param klen The length of the key, or APR_HASH_KEY_STRING to use the string
  54. * length. If APR_HASH_KEY_STRING then returns the actual key length.
  55. */
  56. typedef unsigned int (*apr_hashfunc_t)(const char *key, apr_ssize_t *klen);
  57. /**
  58. * The default hash function.
  59. */
  60. APR_DECLARE_NONSTD(unsigned int) apr_hashfunc_default(const char *key,
  61. apr_ssize_t *klen);
  62. /**
  63. * Create a hash table.
  64. * @param pool The pool to allocate the hash table out of
  65. * @return The hash table just created
  66. */
  67. APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool);
  68. /**
  69. * Create a hash table with a custom hash function
  70. * @param pool The pool to allocate the hash table out of
  71. * @param hash_func A custom hash function.
  72. * @return The hash table just created
  73. */
  74. APR_DECLARE(apr_hash_t *) apr_hash_make_custom(apr_pool_t *pool,
  75. apr_hashfunc_t hash_func);
  76. /**
  77. * Make a copy of a hash table
  78. * @param pool The pool from which to allocate the new hash table
  79. * @param h The hash table to clone
  80. * @return The hash table just created
  81. * @remark Makes a shallow copy
  82. */
  83. APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
  84. const apr_hash_t *h);
  85. /**
  86. * Associate a value with a key in a hash table.
  87. * @param ht The hash table
  88. * @param key Pointer to the key
  89. * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  90. * @param val Value to associate with the key
  91. * @remark If the value is NULL the hash entry is deleted. The key is stored as is,
  92. * and so must have a lifetime at least as long as the hash table's pool.
  93. */
  94. APR_DECLARE(void) apr_hash_set(apr_hash_t *ht, const void *key,
  95. apr_ssize_t klen, const void *val);
  96. /**
  97. * Look up the value associated with a key in a hash table.
  98. * @param ht The hash table
  99. * @param key Pointer to the key
  100. * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  101. * @return Returns NULL if the key is not present.
  102. */
  103. APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht, const void *key,
  104. apr_ssize_t klen);
  105. /**
  106. * Start iterating over the entries in a hash table.
  107. * @param p The pool to allocate the apr_hash_index_t iterator. If this
  108. * pool is NULL, then an internal, non-thread-safe iterator is used.
  109. * @param ht The hash table
  110. * @return The iteration state
  111. * @remark There is no restriction on adding or deleting hash entries during
  112. * an iteration (although the results may be unpredictable unless all you do
  113. * is delete the current entry) and multiple iterations can be in
  114. * progress at the same time.
  115. *
  116. * @par Example:
  117. *
  118. * @code
  119. * int sum_values(apr_pool_t *p, apr_hash_t *ht)
  120. * {
  121. * apr_hash_index_t *hi;
  122. * void *val;
  123. * int sum = 0;
  124. * for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
  125. * apr_hash_this(hi, NULL, NULL, &val);
  126. * sum += *(int *)val;
  127. * }
  128. * return sum;
  129. * }
  130. * @endcode
  131. */
  132. APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht);
  133. /**
  134. * Continue iterating over the entries in a hash table.
  135. * @param hi The iteration state
  136. * @return a pointer to the updated iteration state. NULL if there are no more
  137. * entries.
  138. */
  139. APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi);
  140. /**
  141. * Get the current entry's details from the iteration state.
  142. * @param hi The iteration state
  143. * @param key Return pointer for the pointer to the key.
  144. * @param klen Return pointer for the key length.
  145. * @param val Return pointer for the associated value.
  146. * @remark The return pointers should point to a variable that will be set to the
  147. * corresponding data, or they may be NULL if the data isn't interesting.
  148. */
  149. APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi, const void **key,
  150. apr_ssize_t *klen, void **val);
  151. /**
  152. * Get the current entry's key from the iteration state.
  153. * @param hi The iteration state
  154. * @return The pointer to the key
  155. */
  156. APR_DECLARE(const void*) apr_hash_this_key(apr_hash_index_t *hi);
  157. /**
  158. * Get the current entry's key length from the iteration state.
  159. * @param hi The iteration state
  160. * @return The key length
  161. */
  162. APR_DECLARE(apr_ssize_t) apr_hash_this_key_len(apr_hash_index_t *hi);
  163. /**
  164. * Get the current entry's value from the iteration state.
  165. * @param hi The iteration state
  166. * @return The pointer to the value
  167. */
  168. APR_DECLARE(void*) apr_hash_this_val(apr_hash_index_t *hi);
  169. /**
  170. * Get the number of key/value pairs in the hash table.
  171. * @param ht The hash table
  172. * @return The number of key/value pairs in the hash table.
  173. */
  174. APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht);
  175. /**
  176. * Clear any key/value pairs in the hash table.
  177. * @param ht The hash table
  178. */
  179. APR_DECLARE(void) apr_hash_clear(apr_hash_t *ht);
  180. /**
  181. * Merge two hash tables into one new hash table. The values of the overlay
  182. * hash override the values of the base if both have the same key. Both
  183. * hash tables must use the same hash function.
  184. * @param p The pool to use for the new hash table
  185. * @param overlay The table to add to the initial table
  186. * @param base The table that represents the initial values of the new table
  187. * @return A new hash table containing all of the data from the two passed in
  188. */
  189. APR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p,
  190. const apr_hash_t *overlay,
  191. const apr_hash_t *base);
  192. /**
  193. * Merge two hash tables into one new hash table. If the same key
  194. * is present in both tables, call the supplied merge function to
  195. * produce a merged value for the key in the new table. Both
  196. * hash tables must use the same hash function.
  197. * @param p The pool to use for the new hash table
  198. * @param h1 The first of the tables to merge
  199. * @param h2 The second of the tables to merge
  200. * @param merger A callback function to merge values, or NULL to
  201. * make values from h1 override values from h2 (same semantics as
  202. * apr_hash_overlay())
  203. * @param data Client data to pass to the merger function
  204. * @return A new hash table containing all of the data from the two passed in
  205. */
  206. APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
  207. const apr_hash_t *h1,
  208. const apr_hash_t *h2,
  209. void * (*merger)(apr_pool_t *p,
  210. const void *key,
  211. apr_ssize_t klen,
  212. const void *h1_val,
  213. const void *h2_val,
  214. const void *data),
  215. const void *data);
  216. /**
  217. * Declaration prototype for the iterator callback function of apr_hash_do().
  218. *
  219. * @param rec The data passed as the first argument to apr_hash_[v]do()
  220. * @param key The key from this iteration of the hash table
  221. * @param klen The key length from this iteration of the hash table
  222. * @param value The value from this iteration of the hash table
  223. * @remark Iteration continues while this callback function returns non-zero.
  224. * To export the callback function for apr_hash_do() it must be declared
  225. * in the _NONSTD convention.
  226. */
  227. typedef int (apr_hash_do_callback_fn_t)(void *rec, const void *key,
  228. apr_ssize_t klen,
  229. const void *value);
  230. /**
  231. * Iterate over a hash table running the provided function once for every
  232. * element in the hash table. The @param comp function will be invoked for
  233. * every element in the hash table.
  234. *
  235. * @param comp The function to run
  236. * @param rec The data to pass as the first argument to the function
  237. * @param ht The hash table to iterate over
  238. * @return FALSE if one of the comp() iterations returned zero; TRUE if all
  239. * iterations returned non-zero
  240. * @see apr_hash_do_callback_fn_t
  241. */
  242. APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
  243. void *rec, const apr_hash_t *ht);
  244. /**
  245. * Get a pointer to the pool which the hash table was created in
  246. */
  247. APR_POOL_DECLARE_ACCESSOR(hash);
  248. /** @} */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif /* !APR_HASH_H */