You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

linkhash.h 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * $Id: linkhash.h,v 1.6 2006/01/30 23:07:57 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. #ifndef _linkhash_h_
  13. #define _linkhash_h_
  14. #include "json_object.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * golden prime used in hash functions
  20. */
  21. #define LH_PRIME 0x9e370001UL
  22. /**
  23. * The fraction of filled hash buckets until an insert will cause the table
  24. * to be resized.
  25. * This can range from just above 0 up to 1.0.
  26. */
  27. #define LH_LOAD_FACTOR 0.66
  28. /**
  29. * sentinel pointer value for empty slots
  30. */
  31. #define LH_EMPTY (void*)-1
  32. /**
  33. * sentinel pointer value for freed slots
  34. */
  35. #define LH_FREED (void*)-2
  36. /**
  37. * default string hash function
  38. */
  39. #define JSON_C_STR_HASH_DFLT 0
  40. /**
  41. * perl-like string hash function
  42. */
  43. #define JSON_C_STR_HASH_PERLLIKE 1
  44. /**
  45. * This function sets the hash function to be used for strings.
  46. * Must be one of the JSON_C_STR_HASH_* values.
  47. * @returns 0 - ok, -1 if parameter was invalid
  48. */
  49. int json_global_set_string_hash(const int h);
  50. struct lh_entry;
  51. /**
  52. * callback function prototypes
  53. */
  54. typedef void (lh_entry_free_fn) (struct lh_entry *e);
  55. /**
  56. * callback function prototypes
  57. */
  58. typedef unsigned long (lh_hash_fn) (const void *k);
  59. /**
  60. * callback function prototypes
  61. */
  62. typedef int (lh_equal_fn) (const void *k1, const void *k2);
  63. /**
  64. * An entry in the hash table
  65. */
  66. struct lh_entry {
  67. /**
  68. * The key.
  69. */
  70. void *k;
  71. int k_is_constant;
  72. /**
  73. * The value.
  74. */
  75. const void *v;
  76. /**
  77. * The next entry
  78. */
  79. struct lh_entry *next;
  80. /**
  81. * The previous entry.
  82. */
  83. struct lh_entry *prev;
  84. };
  85. /**
  86. * The hash table structure.
  87. */
  88. struct lh_table {
  89. /**
  90. * Size of our hash.
  91. */
  92. int size;
  93. /**
  94. * Numbers of entries.
  95. */
  96. int count;
  97. /**
  98. * The first entry.
  99. */
  100. struct lh_entry *head;
  101. /**
  102. * The last entry.
  103. */
  104. struct lh_entry *tail;
  105. struct lh_entry *table;
  106. /**
  107. * A pointer onto the function responsible for freeing an entry.
  108. */
  109. lh_entry_free_fn *free_fn;
  110. lh_hash_fn *hash_fn;
  111. lh_equal_fn *equal_fn;
  112. };
  113. /**
  114. * Convenience list iterator.
  115. */
  116. #define lh_foreach(table, entry) \
  117. for(entry = table->head; entry; entry = entry->next)
  118. /**
  119. * lh_foreach_safe allows calling of deletion routine while iterating.
  120. */
  121. #define lh_foreach_safe(table, entry, tmp) \
  122. for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  123. /**
  124. * Create a new linkhash table.
  125. * @param size initial table size. The table is automatically resized
  126. * although this incurs a performance penalty.
  127. * @param free_fn callback function used to free memory for entries
  128. * when lh_table_free or lh_table_delete is called.
  129. * If NULL is provided, then memory for keys and values
  130. * must be freed by the caller.
  131. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  132. * lh_ptr_hash and lh_char_hash for hashing pointer values
  133. * and C strings respectively.
  134. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  135. * lh_ptr_hash and lh_char_hash for comparing pointer values
  136. * and C strings respectively.
  137. * @return a pointer onto the linkhash table.
  138. */
  139. extern struct lh_table* lh_table_new(int size,
  140. lh_entry_free_fn *free_fn,
  141. lh_hash_fn *hash_fn,
  142. lh_equal_fn *equal_fn);
  143. /**
  144. * Convenience function to create a new linkhash
  145. * table with char keys.
  146. * @param size initial table size.
  147. * @param name table name.
  148. * @param free_fn callback function used to free memory for entries.
  149. * @return a pointer onto the linkhash table.
  150. */
  151. extern struct lh_table* lh_kchar_table_new(int size,
  152. lh_entry_free_fn *free_fn);
  153. /**
  154. * Convenience function to create a new linkhash
  155. * table with ptr keys.
  156. * @param size initial table size.
  157. * @param name table name.
  158. * @param free_fn callback function used to free memory for entries.
  159. * @return a pointer onto the linkhash table.
  160. */
  161. extern struct lh_table* lh_kptr_table_new(int size,
  162. lh_entry_free_fn *free_fn);
  163. /**
  164. * Free a linkhash table.
  165. * If a callback free function is provided then it is called for all
  166. * entries in the table.
  167. * @param t table to free.
  168. */
  169. extern void lh_table_free(struct lh_table *t);
  170. /**
  171. * Insert a record into the table.
  172. * @param t the table to insert into.
  173. * @param k a pointer to the key to insert.
  174. * @param v a pointer to the value to insert.
  175. */
  176. extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
  177. /**
  178. * Insert a record into the table. This one accepts the key's hash in additon
  179. * to the key. This is an exension to support functions that need to calculate
  180. * the hash several times and allows them to do it just once and then pass
  181. * in the hash to all utility functions. Depending on use case, this can be a
  182. * very considerate performance improvement.
  183. * @param t the table to insert into.
  184. * @param k a pointer to the key to insert.
  185. * @param v a pointer to the value to insert.
  186. * @param h hash value of the key to insert
  187. * @param opts opts, a subset of JSON_OBJECT_ADD_* flags is supported
  188. */
  189. extern int lh_table_insert_w_hash(struct lh_table *t, void *k, const void *v, const unsigned long h, const unsigned opts);
  190. /**
  191. * Lookup a record into the table.
  192. * @param t the table to lookup
  193. * @param k a pointer to the key to lookup
  194. * @return a pointer to the record structure of the value or NULL if it does not exist.
  195. */
  196. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  197. /**
  198. * Lookup a record into the table. This one accepts the key's hash in additon
  199. * to the key. This is an exension to support functions that need to calculate
  200. * the hash several times and allows them to do it just once and then pass
  201. * in the hash to all utility functions. Depending on use case, this can be a
  202. * very considerate performance improvement.
  203. * @param t the table to lookup
  204. * @param k a pointer to the key to lookup
  205. * @param h hash value of the key to lookup
  206. * @return a pointer to the record structure of the value or NULL if it does not exist.
  207. */
  208. extern struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h);
  209. /**
  210. * Lookup a record into the table
  211. * @param t the table to lookup
  212. * @param k a pointer to the key to lookup
  213. * @return a pointer to the found value or NULL if it does not exist.
  214. * @deprecated Use lh_table_lookup_ex instead.
  215. */
  216. THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
  217. /**
  218. * Lookup a record in the table
  219. * @param t the table to lookup
  220. * @param k a pointer to the key to lookup
  221. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  222. * @return whether or not the key was found
  223. */
  224. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  225. /**
  226. * Delete a record from the table.
  227. * If a callback free function is provided then it is called for the
  228. * for the item being deleted.
  229. * @param t the table to delete from.
  230. * @param e a pointer to the entry to delete.
  231. * @return 0 if the item was deleted.
  232. * @return -1 if it was not found.
  233. */
  234. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  235. /**
  236. * Delete a record from the table.
  237. * If a callback free function is provided then it is called for the
  238. * for the item being deleted.
  239. * @param t the table to delete from.
  240. * @param k a pointer to the key to delete.
  241. * @return 0 if the item was deleted.
  242. * @return -1 if it was not found.
  243. */
  244. extern int lh_table_delete(struct lh_table *t, const void *k);
  245. extern int lh_table_length(struct lh_table *t);
  246. void lh_abort(const char *msg, ...);
  247. void lh_table_resize(struct lh_table *t, int new_size);
  248. /**
  249. * Calculate the hash of a key for a given table.
  250. * This is an exension to support functions that need to calculate
  251. * the hash several times and allows them to do it just once and then pass
  252. * in the hash to all utility functions. Depending on use case, this can be a
  253. * very considerate performance improvement.
  254. * @param t the table (used to obtain hash function)
  255. * @param k a pointer to the key to lookup
  256. * @return the key's hash
  257. */
  258. static inline unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  259. {
  260. return t->hash_fn(k);
  261. }
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif