fspr_hash.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 fspr_hash.h
  20. * @brief APR Hash Tables
  21. */
  22. #include "fspr_pools.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * @defgroup fspr_hash Hash Tables
  28. * @ingroup APR
  29. * @{
  30. */
  31. /**
  32. * When passing a key to fspr_hash_set or fspr_hash_get, this value can be
  33. * passed to indicate a string-valued key, and have fspr_hash compute the
  34. * length automatically.
  35. *
  36. * @remark fspr_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), fspr_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 fspr_hash_t fspr_hash_t;
  46. /**
  47. * Abstract type for scanning hash tables.
  48. */
  49. typedef struct fspr_hash_index_t fspr_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 (*fspr_hashfunc_t)(const char *key, fspr_ssize_t *klen);
  57. /**
  58. * The default hash function.
  59. */
  60. APR_DECLARE_NONSTD(unsigned int) fspr_hashfunc_default(const char *key,
  61. fspr_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(fspr_hash_t *) fspr_hash_make(fspr_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(fspr_hash_t *) fspr_hash_make_custom(fspr_pool_t *pool,
  75. fspr_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(fspr_hash_t *) fspr_hash_copy(fspr_pool_t *pool,
  84. const fspr_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.
  92. */
  93. APR_DECLARE(void) fspr_hash_set(fspr_hash_t *ht, const void *key,
  94. fspr_ssize_t klen, const void *val);
  95. /**
  96. * Look up the value associated with a key in a hash table.
  97. * @param ht The hash table
  98. * @param key Pointer to the key
  99. * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  100. * @return Returns NULL if the key is not present.
  101. */
  102. APR_DECLARE(void *) fspr_hash_get(fspr_hash_t *ht, const void *key,
  103. fspr_ssize_t klen);
  104. /**
  105. * Start iterating over the entries in a hash table.
  106. * @param p The pool to allocate the fspr_hash_index_t iterator. If this
  107. * pool is NULL, then an internal, non-thread-safe iterator is used.
  108. * @param ht The hash table
  109. * @remark There is no restriction on adding or deleting hash entries during
  110. * an iteration (although the results may be unpredictable unless all you do
  111. * is delete the current entry) and multiple iterations can be in
  112. * progress at the same time.
  113. * @example
  114. */
  115. /**
  116. * <PRE>
  117. *
  118. * int sum_values(fspr_pool_t *p, fspr_hash_t *ht)
  119. * {
  120. * fspr_hash_index_t *hi;
  121. * void *val;
  122. * int sum = 0;
  123. * for (hi = fspr_hash_first(p, ht); hi; hi = fspr_hash_next(hi)) {
  124. * fspr_hash_this(hi, NULL, NULL, &val);
  125. * sum += *(int *)val;
  126. * }
  127. * return sum;
  128. * }
  129. * </PRE>
  130. */
  131. APR_DECLARE(fspr_hash_index_t *) fspr_hash_first(fspr_pool_t *p, fspr_hash_t *ht);
  132. /**
  133. * Continue iterating over the entries in a hash table.
  134. * @param hi The iteration state
  135. * @return a pointer to the updated iteration state. NULL if there are no more
  136. * entries.
  137. */
  138. APR_DECLARE(fspr_hash_index_t *) fspr_hash_next(fspr_hash_index_t *hi);
  139. /**
  140. * Get the current entry's details from the iteration state.
  141. * @param hi The iteration state
  142. * @param key Return pointer for the pointer to the key.
  143. * @param klen Return pointer for the key length.
  144. * @param val Return pointer for the associated value.
  145. * @remark The return pointers should point to a variable that will be set to the
  146. * corresponding data, or they may be NULL if the data isn't interesting.
  147. */
  148. APR_DECLARE(void) fspr_hash_this(fspr_hash_index_t *hi, const void **key,
  149. fspr_ssize_t *klen, void **val);
  150. /**
  151. * Get the number of key/value pairs in the hash table.
  152. * @param ht The hash table
  153. * @return The number of key/value pairs in the hash table.
  154. */
  155. APR_DECLARE(unsigned int) fspr_hash_count(fspr_hash_t *ht);
  156. /**
  157. * Clear any key/value pairs in the hash table.
  158. * @param ht The hash table
  159. */
  160. APR_DECLARE(void) fspr_hash_clear(fspr_hash_t *ht);
  161. /**
  162. * Merge two hash tables into one new hash table. The values of the overlay
  163. * hash override the values of the base if both have the same key. Both
  164. * hash tables must use the same hash function.
  165. * @param p The pool to use for the new hash table
  166. * @param overlay The table to add to the initial table
  167. * @param base The table that represents the initial values of the new table
  168. * @return A new hash table containing all of the data from the two passed in
  169. */
  170. APR_DECLARE(fspr_hash_t *) fspr_hash_overlay(fspr_pool_t *p,
  171. const fspr_hash_t *overlay,
  172. const fspr_hash_t *base);
  173. /**
  174. * Merge two hash tables into one new hash table. If the same key
  175. * is present in both tables, call the supplied merge function to
  176. * produce a merged value for the key in the new table. Both
  177. * hash tables must use the same hash function.
  178. * @param p The pool to use for the new hash table
  179. * @param h1 The first of the tables to merge
  180. * @param h2 The second of the tables to merge
  181. * @param merger A callback function to merge values, or NULL to
  182. * make values from h1 override values from h2 (same semantics as
  183. * fspr_hash_overlay())
  184. * @param data Client data to pass to the merger function
  185. * @return A new hash table containing all of the data from the two passed in
  186. */
  187. APR_DECLARE(fspr_hash_t *) fspr_hash_merge(fspr_pool_t *p,
  188. const fspr_hash_t *h1,
  189. const fspr_hash_t *h2,
  190. void * (*merger)(fspr_pool_t *p,
  191. const void *key,
  192. fspr_ssize_t klen,
  193. const void *h1_val,
  194. const void *h2_val,
  195. const void *data),
  196. const void *data);
  197. /**
  198. * Get a pointer to the pool which the hash table was created in
  199. */
  200. APR_POOL_DECLARE_ACCESSOR(hash);
  201. /** @} */
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* !APR_HASH_H */