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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. struct lh_entry;
  37. /**
  38. * callback function prototypes
  39. */
  40. typedef void (lh_entry_free_fn) (struct lh_entry *e);
  41. /**
  42. * callback function prototypes
  43. */
  44. typedef unsigned long (lh_hash_fn) (const void *k);
  45. /**
  46. * callback function prototypes
  47. */
  48. typedef int (lh_equal_fn) (const void *k1, const void *k2);
  49. /**
  50. * An entry in the hash table
  51. */
  52. struct lh_entry {
  53. /**
  54. * The key.
  55. */
  56. void *k;
  57. int k_is_constant;
  58. /**
  59. * The value.
  60. */
  61. const void *v;
  62. /**
  63. * The next entry
  64. */
  65. struct lh_entry *next;
  66. /**
  67. * The previous entry.
  68. */
  69. struct lh_entry *prev;
  70. };
  71. /**
  72. * The hash table structure.
  73. */
  74. struct lh_table {
  75. /**
  76. * Size of our hash.
  77. */
  78. int size;
  79. /**
  80. * Numbers of entries.
  81. */
  82. int count;
  83. /**
  84. * Number of collisions.
  85. */
  86. int collisions;
  87. /**
  88. * Number of resizes.
  89. */
  90. int resizes;
  91. /**
  92. * Number of lookups.
  93. */
  94. int lookups;
  95. /**
  96. * Number of inserts.
  97. */
  98. int inserts;
  99. /**
  100. * Number of deletes.
  101. */
  102. int deletes;
  103. /**
  104. * Name of the hash table.
  105. */
  106. const char *name;
  107. /**
  108. * The first entry.
  109. */
  110. struct lh_entry *head;
  111. /**
  112. * The last entry.
  113. */
  114. struct lh_entry *tail;
  115. struct lh_entry *table;
  116. /**
  117. * A pointer onto the function responsible for freeing an entry.
  118. */
  119. lh_entry_free_fn *free_fn;
  120. lh_hash_fn *hash_fn;
  121. lh_equal_fn *equal_fn;
  122. };
  123. /**
  124. * Pre-defined hash and equality functions
  125. */
  126. extern unsigned long lh_ptr_hash(const void *k);
  127. extern int lh_ptr_equal(const void *k1, const void *k2);
  128. extern unsigned long lh_char_hash(const void *k);
  129. extern int lh_char_equal(const void *k1, const void *k2);
  130. /**
  131. * Convenience list iterator.
  132. */
  133. #define lh_foreach(table, entry) \
  134. for(entry = table->head; entry; entry = entry->next)
  135. /**
  136. * lh_foreach_safe allows calling of deletion routine while iterating.
  137. */
  138. #define lh_foreach_safe(table, entry, tmp) \
  139. for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  140. /**
  141. * Create a new linkhash table.
  142. * @param size initial table size. The table is automatically resized
  143. * although this incurs a performance penalty.
  144. * @param name the table name.
  145. * @param free_fn callback function used to free memory for entries
  146. * when lh_table_free or lh_table_delete is called.
  147. * If NULL is provided, then memory for keys and values
  148. * must be freed by the caller.
  149. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  150. * lh_ptr_hash and lh_char_hash for hashing pointer values
  151. * and C strings respectively.
  152. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  153. * lh_ptr_hash and lh_char_hash for comparing pointer values
  154. * and C strings respectively.
  155. * @return a pointer onto the linkhash table.
  156. */
  157. extern struct lh_table* lh_table_new(int size, const char *name,
  158. lh_entry_free_fn *free_fn,
  159. lh_hash_fn *hash_fn,
  160. lh_equal_fn *equal_fn);
  161. /**
  162. * Convenience function to create a new linkhash
  163. * table with char keys.
  164. * @param size initial table size.
  165. * @param name table name.
  166. * @param free_fn callback function used to free memory for entries.
  167. * @return a pointer onto the linkhash table.
  168. */
  169. extern struct lh_table* lh_kchar_table_new(int size, const char *name,
  170. lh_entry_free_fn *free_fn);
  171. /**
  172. * Convenience function to create a new linkhash
  173. * table with ptr keys.
  174. * @param size initial table size.
  175. * @param name table name.
  176. * @param free_fn callback function used to free memory for entries.
  177. * @return a pointer onto the linkhash table.
  178. */
  179. extern struct lh_table* lh_kptr_table_new(int size, const char *name,
  180. lh_entry_free_fn *free_fn);
  181. /**
  182. * Free a linkhash table.
  183. * If a callback free function is provided then it is called for all
  184. * entries in the table.
  185. * @param t table to free.
  186. */
  187. extern void lh_table_free(struct lh_table *t);
  188. /**
  189. * Insert a record into the table.
  190. * @param t the table to insert into.
  191. * @param k a pointer to the key to insert.
  192. * @param v a pointer to the value to insert.
  193. */
  194. extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
  195. /**
  196. * Insert a record into the table. This one accepts the key's hash in additon
  197. * to the key. This is an exension to support functions that need to calculate
  198. * the hash several times and allows them to do it just once and then pass
  199. * in the hash to all utility functions. Depending on use case, this can be a
  200. * very considerate performance improvement.
  201. * @param t the table to insert into.
  202. * @param k a pointer to the key to insert.
  203. * @param v a pointer to the value to insert.
  204. * @param h hash value of the key to insert
  205. * @param opts opts, a subset of JSON_OBJECT_ADD_* flags is supported
  206. */
  207. extern int lh_table_insert_w_hash(struct lh_table *t, void *k, const void *v, const unsigned long h, const unsigned opts);
  208. /**
  209. * Lookup a record into the table.
  210. * @param t the table to lookup
  211. * @param k a pointer to the key to lookup
  212. * @return a pointer to the record structure of the value or NULL if it does not exist.
  213. */
  214. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  215. /**
  216. * Lookup a record into the table. This one accepts the key's hash in additon
  217. * to the key. This is an exension to support functions that need to calculate
  218. * the hash several times and allows them to do it just once and then pass
  219. * in the hash to all utility functions. Depending on use case, this can be a
  220. * very considerate performance improvement.
  221. * @param t the table to lookup
  222. * @param k a pointer to the key to lookup
  223. * @param h hash value of the key to lookup
  224. * @return a pointer to the record structure of the value or NULL if it does not exist.
  225. */
  226. extern struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h);
  227. /**
  228. * Lookup a record into the table
  229. * @param t the table to lookup
  230. * @param k a pointer to the key to lookup
  231. * @return a pointer to the found value or NULL if it does not exist.
  232. * @deprecated Use lh_table_lookup_ex instead.
  233. */
  234. THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
  235. /**
  236. * Lookup a record in the table
  237. * @param t the table to lookup
  238. * @param k a pointer to the key to lookup
  239. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  240. * @return whether or not the key was found
  241. */
  242. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  243. /**
  244. * Delete a record from the table.
  245. * If a callback free function is provided then it is called for the
  246. * for the item being deleted.
  247. * @param t the table to delete from.
  248. * @param e a pointer to the entry to delete.
  249. * @return 0 if the item was deleted.
  250. * @return -1 if it was not found.
  251. */
  252. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  253. /**
  254. * Delete a record from the table.
  255. * If a callback free function is provided then it is called for the
  256. * for the item being deleted.
  257. * @param t the table to delete from.
  258. * @param k a pointer to the key to delete.
  259. * @return 0 if the item was deleted.
  260. * @return -1 if it was not found.
  261. */
  262. extern int lh_table_delete(struct lh_table *t, const void *k);
  263. extern int lh_table_length(struct lh_table *t);
  264. void lh_abort(const char *msg, ...);
  265. void lh_table_resize(struct lh_table *t, int new_size);
  266. /**
  267. * Calculate the hash of a key for a given table.
  268. * This is an exension to support functions that need to calculate
  269. * the hash several times and allows them to do it just once and then pass
  270. * in the hash to all utility functions. Depending on use case, this can be a
  271. * very considerate performance improvement.
  272. * @param t the table (used to obtain hash function)
  273. * @param k a pointer to the key to lookup
  274. * @return the key's hash
  275. */
  276. static inline unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  277. {
  278. return t->hash_fn(k);
  279. }
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif