hash.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_HASH_H__
  20. #define __PJ_HASH_H__
  21. /**
  22. * @file hash.h
  23. * @brief Hash Table.
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_HASH Hash Table
  29. * @ingroup PJ_DS
  30. * @{
  31. * A hash table is a dictionary in which keys are mapped to array positions by
  32. * hash functions. Having the keys of more than one item map to the same
  33. * position is called a collision. In this library, we will chain the nodes
  34. * that have the same key in a list.
  35. */
  36. /**
  37. * If this constant is used as keylen, then the key is interpreted as
  38. * NULL terminated string.
  39. */
  40. #define PJ_HASH_KEY_STRING ((unsigned)-1)
  41. /**
  42. * This indicates the size of of each hash entry.
  43. */
  44. #define PJ_HASH_ENTRY_BUF_SIZE (3*sizeof(void*) + 2*sizeof(pj_uint32_t))
  45. /**
  46. * Type declaration for entry buffer, used by #pj_hash_set_np()
  47. */
  48. typedef void *pj_hash_entry_buf[(PJ_HASH_ENTRY_BUF_SIZE+sizeof(void*)-1)/(sizeof(void*))];
  49. /**
  50. * This is the function that is used by the hash table to calculate hash value
  51. * of the specified key.
  52. *
  53. * @param hval the initial hash value, or zero.
  54. * @param key the key to calculate.
  55. * @param keylen the length of the key, or PJ_HASH_KEY_STRING to treat
  56. * the key as null terminated string.
  57. *
  58. * @return the hash value.
  59. */
  60. PJ_DECL(pj_uint32_t) pj_hash_calc(pj_uint32_t hval,
  61. const void *key, unsigned keylen);
  62. /**
  63. * Convert the key to lowercase and calculate the hash value. The resulting
  64. * string is stored in \c result.
  65. *
  66. * @param hval The initial hash value, normally zero.
  67. * @param result Optional. Buffer to store the result, which must be enough
  68. * to hold the string.
  69. * @param key The input key to be converted and calculated.
  70. *
  71. * @return The hash value.
  72. */
  73. PJ_DECL(pj_uint32_t) pj_hash_calc_tolower(pj_uint32_t hval,
  74. char *result,
  75. const pj_str_t *key);
  76. /**
  77. * Create a hash table with the specified 'bucket' size.
  78. *
  79. * @param pool the pool from which the hash table will be allocated from.
  80. * @param size the bucket size, which will be round-up to the nearest 2^n-1
  81. *
  82. * @return the hash table.
  83. */
  84. PJ_DECL(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size);
  85. /**
  86. * Get the value associated with the specified key.
  87. *
  88. * @param ht the hash table.
  89. * @param key the key to look for.
  90. * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
  91. * string length of the key.
  92. * @param hval if this argument is not NULL and the value is not zero,
  93. * the value will be used as the computed hash value. If
  94. * the argument is not NULL and the value is zero, it will
  95. * be filled with the computed hash upon return.
  96. *
  97. * @return the value associated with the key, or NULL if the key is not found.
  98. */
  99. PJ_DECL(void *) pj_hash_get( pj_hash_table_t *ht,
  100. const void *key, unsigned keylen,
  101. pj_uint32_t *hval );
  102. /**
  103. * Variant of #pj_hash_get() with the key being converted to lowercase when
  104. * calculating the hash value.
  105. *
  106. * @see pj_hash_get()
  107. */
  108. PJ_DECL(void *) pj_hash_get_lower( pj_hash_table_t *ht,
  109. const void *key, unsigned keylen,
  110. pj_uint32_t *hval );
  111. /**
  112. * Associate/disassociate a value with the specified key. If value is not
  113. * NULL and entry already exists, the entry's value will be overwritten.
  114. * If value is not NULL and entry does not exist, a new one will be created
  115. * with the specified pool. Otherwise if value is NULL, entry will be
  116. * deleted if it exists.
  117. *
  118. * @param pool the pool to allocate the new entry if a new entry has to be
  119. * created.
  120. * @param ht the hash table.
  121. * @param key the key. If pool is not specified, the key MUST point to
  122. * buffer that remains valid for the duration of the entry.
  123. * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
  124. * string length of the key.
  125. * @param hval if the value is not zero, then the hash table will use
  126. * this value to search the entry's index, otherwise it will
  127. * compute the key. This value can be obtained when calling
  128. * #pj_hash_get().
  129. * @param value value to be associated, or NULL to delete the entry with
  130. * the specified key.
  131. */
  132. PJ_DECL(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
  133. const void *key, unsigned keylen, pj_uint32_t hval,
  134. void *value );
  135. /**
  136. * Variant of #pj_hash_set() with the key being converted to lowercase when
  137. * calculating the hash value.
  138. *
  139. * @see pj_hash_set()
  140. */
  141. PJ_DECL(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht,
  142. const void *key, unsigned keylen,
  143. pj_uint32_t hval, void *value );
  144. /**
  145. * Associate/disassociate a value with the specified key. This function works
  146. * like #pj_hash_set(), except that it doesn't use pool (hence the np -- no
  147. * pool suffix). If new entry needs to be allocated, it will use the entry_buf.
  148. *
  149. * @param ht the hash table.
  150. * @param key the key.
  151. * @param keylen the length of the key, or PJ_HASH_KEY_STRING to use the
  152. * string length of the key.
  153. * @param hval if the value is not zero, then the hash table will use
  154. * this value to search the entry's index, otherwise it will
  155. * compute the key. This value can be obtained when calling
  156. * #pj_hash_get().
  157. * @param entry_buf Buffer which will be used for the new entry, when one needs
  158. * to be created.
  159. * @param value value to be associated, or NULL to delete the entry with
  160. * the specified key.
  161. */
  162. PJ_DECL(void) pj_hash_set_np(pj_hash_table_t *ht,
  163. const void *key, unsigned keylen,
  164. pj_uint32_t hval, pj_hash_entry_buf entry_buf,
  165. void *value);
  166. /**
  167. * Variant of #pj_hash_set_np() with the key being converted to lowercase
  168. * when calculating the hash value.
  169. *
  170. * @see pj_hash_set_np()
  171. */
  172. PJ_DECL(void) pj_hash_set_np_lower(pj_hash_table_t *ht,
  173. const void *key, unsigned keylen,
  174. pj_uint32_t hval,
  175. pj_hash_entry_buf entry_buf,
  176. void *value);
  177. /**
  178. * Get the total number of entries in the hash table.
  179. *
  180. * @param ht the hash table.
  181. *
  182. * @return the number of entries in the hash table.
  183. */
  184. PJ_DECL(unsigned) pj_hash_count( pj_hash_table_t *ht );
  185. /**
  186. * Get the iterator to the first element in the hash table.
  187. *
  188. * @param ht the hash table.
  189. * @param it the iterator for iterating hash elements.
  190. *
  191. * @return the iterator to the hash element, or NULL if no element presents.
  192. */
  193. PJ_DECL(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
  194. pj_hash_iterator_t *it );
  195. /**
  196. * Get the next element from the iterator.
  197. *
  198. * @param ht the hash table.
  199. * @param it the hash iterator.
  200. *
  201. * @return the next iterator, or NULL if there's no more element.
  202. */
  203. PJ_DECL(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht,
  204. pj_hash_iterator_t *it );
  205. /**
  206. * Get the value associated with a hash iterator.
  207. *
  208. * @param ht the hash table.
  209. * @param it the hash iterator.
  210. *
  211. * @return the value associated with the current element in iterator.
  212. */
  213. PJ_DECL(void*) pj_hash_this( pj_hash_table_t *ht,
  214. pj_hash_iterator_t *it );
  215. /**
  216. * @}
  217. */
  218. PJ_END_DECL
  219. #endif