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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. Use lh_entry_k() instead of accessing this directly.
  69. */
  70. const void *k;
  71. /**
  72. * A flag for users of linkhash to know whether or not they
  73. * need to free k.
  74. */
  75. int k_is_constant;
  76. /**
  77. * The value. Use lh_entry_v() instead of accessing this directly.
  78. */
  79. const void *v;
  80. /**
  81. * The next entry
  82. */
  83. struct lh_entry *next;
  84. /**
  85. * The previous entry.
  86. */
  87. struct lh_entry *prev;
  88. };
  89. /**
  90. * The hash table structure.
  91. */
  92. struct lh_table {
  93. /**
  94. * Size of our hash.
  95. */
  96. int size;
  97. /**
  98. * Numbers of entries.
  99. */
  100. int count;
  101. /**
  102. * The first entry.
  103. */
  104. struct lh_entry *head;
  105. /**
  106. * The last entry.
  107. */
  108. struct lh_entry *tail;
  109. struct lh_entry *table;
  110. /**
  111. * A pointer onto the function responsible for freeing an entry.
  112. */
  113. lh_entry_free_fn *free_fn;
  114. lh_hash_fn *hash_fn;
  115. lh_equal_fn *equal_fn;
  116. };
  117. /**
  118. * Convenience list iterator.
  119. */
  120. #define lh_foreach(table, entry) \
  121. for(entry = table->head; entry; entry = entry->next)
  122. /**
  123. * lh_foreach_safe allows calling of deletion routine while iterating.
  124. */
  125. #define lh_foreach_safe(table, entry, tmp) \
  126. for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  127. /**
  128. * Create a new linkhash table.
  129. * @param size initial table size. The table is automatically resized
  130. * although this incurs a performance penalty.
  131. * @param free_fn callback function used to free memory for entries
  132. * when lh_table_free or lh_table_delete is called.
  133. * If NULL is provided, then memory for keys and values
  134. * must be freed by the caller.
  135. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  136. * lh_ptr_hash and lh_char_hash for hashing pointer values
  137. * and C strings respectively.
  138. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  139. * lh_ptr_hash and lh_char_hash for comparing pointer values
  140. * and C strings respectively.
  141. * @return On success, a pointer to the new linkhash table is returned.
  142. * On error, a null pointer is returned.
  143. */
  144. extern struct lh_table* lh_table_new(int size,
  145. lh_entry_free_fn *free_fn,
  146. lh_hash_fn *hash_fn,
  147. lh_equal_fn *equal_fn);
  148. /**
  149. * Convenience function to create a new linkhash
  150. * table with char keys.
  151. * @param size initial table size.
  152. * @param free_fn callback function used to free memory for entries.
  153. * @return On success, a pointer to the new linkhash table is returned.
  154. * On error, a null pointer is returned.
  155. */
  156. extern struct lh_table* lh_kchar_table_new(int size,
  157. lh_entry_free_fn *free_fn);
  158. /**
  159. * Convenience function to create a new linkhash
  160. * table with ptr keys.
  161. * @param size initial table size.
  162. * @param free_fn callback function used to free memory for entries.
  163. * @return On success, a pointer to the new linkhash table is returned.
  164. * On error, a null pointer is returned.
  165. */
  166. extern struct lh_table* lh_kptr_table_new(int size,
  167. lh_entry_free_fn *free_fn);
  168. /**
  169. * Free a linkhash table.
  170. * If a callback free function is provided then it is called for all
  171. * entries in the table.
  172. * @param t table to free.
  173. */
  174. extern void lh_table_free(struct lh_table *t);
  175. /**
  176. * Insert a record into the table.
  177. * @param t the table to insert into.
  178. * @param k a pointer to the key to insert.
  179. * @param v a pointer to the value to insert.
  180. *
  181. * @return On success, <code>0</code> is returned.
  182. * On error, a negative value is returned.
  183. */
  184. extern int lh_table_insert(struct lh_table *t, const void *k, const void *v);
  185. /**
  186. * Insert a record into the table. This one accepts the key's hash in additon
  187. * to the key. This is an exension to support functions that need to calculate
  188. * the hash several times and allows them to do it just once and then pass
  189. * in the hash to all utility functions. Depending on use case, this can be a
  190. * very considerate performance improvement.
  191. * @param t the table to insert into.
  192. * @param k a pointer to the key to insert.
  193. * @param v a pointer to the value to insert.
  194. * @param h hash value of the key to insert
  195. * @param opts opts, a subset of JSON_OBJECT_ADD_* flags is supported
  196. */
  197. extern int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h, const unsigned opts);
  198. /**
  199. * Lookup a record into the table.
  200. * @param t the table to lookup
  201. * @param k a pointer to the key to lookup
  202. * @return a pointer to the record structure of the value or NULL if it does not exist.
  203. */
  204. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  205. /**
  206. * Lookup a record into the table. This one accepts the key's hash in additon
  207. * to the key. This is an exension to support functions that need to calculate
  208. * the hash several times and allows them to do it just once and then pass
  209. * in the hash to all utility functions. Depending on use case, this can be a
  210. * very considerate performance improvement.
  211. * @param t the table to lookup
  212. * @param k a pointer to the key to lookup
  213. * @param h hash value of the key to lookup
  214. * @return a pointer to the record structure of the value or NULL if it does not exist.
  215. */
  216. extern struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h);
  217. /**
  218. * Lookup a record into the table
  219. * @param t the table to lookup
  220. * @param k a pointer to the key to lookup
  221. * @return a pointer to the found value or NULL if it does not exist.
  222. * @deprecated Use lh_table_lookup_ex instead.
  223. */
  224. THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
  225. /**
  226. * Lookup a record in the table
  227. * @param t the table to lookup
  228. * @param k a pointer to the key to lookup
  229. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  230. * @return whether or not the key was found
  231. */
  232. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  233. /**
  234. * Delete a record from the table.
  235. * If a callback free function is provided then it is called for the
  236. * for the item being deleted.
  237. * @param t the table to delete from.
  238. * @param e a pointer to the entry to delete.
  239. * @return 0 if the item was deleted.
  240. * @return -1 if it was not found.
  241. */
  242. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  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 k a pointer to the key 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(struct lh_table *t, const void *k);
  253. extern int lh_table_length(struct lh_table *t);
  254. /**
  255. * Prints a message to <code>stdout</code>,
  256. * then exits the program with an exit code of <code>1</code>.
  257. *
  258. * @param msg Message format string, like for <code>printf</code>.
  259. * @param ... Format args.
  260. *
  261. * @deprecated Since it is not a good idea to exit the entire program
  262. * because of an internal library failure, json-c will no longer
  263. * use this function internally.
  264. * However, because its interface is public, it will remain part of
  265. * the API on the off chance of legacy software using it externally.
  266. */
  267. THIS_FUNCTION_IS_DEPRECATED(void lh_abort(const char *msg, ...));
  268. /**
  269. * Resizes the specified table.
  270. *
  271. * @param t Pointer to table to resize.
  272. * @param new_size New table size. Must be positive.
  273. *
  274. * @return On success, <code>0</code> is returned.
  275. * On error, a negative value is returned.
  276. */
  277. int lh_table_resize(struct lh_table *t, int new_size);
  278. #if !defined(_MSC_VER) || (_MSC_VER > 1800)
  279. /* VS2010 can't handle inline funcs, so skip it there */
  280. #define _LH_INLINE inline
  281. #else
  282. #define _LH_INLINE
  283. #endif
  284. /**
  285. * Calculate the hash of a key for a given table.
  286. * This is an exension to support functions that need to calculate
  287. * the hash several times and allows them to do it just once and then pass
  288. * in the hash to all utility functions. Depending on use case, this can be a
  289. * very considerate performance improvement.
  290. * @param t the table (used to obtain hash function)
  291. * @param k a pointer to the key to lookup
  292. * @return the key's hash
  293. */
  294. static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  295. {
  296. return t->hash_fn(k);
  297. }
  298. #undef _LH_INLINE
  299. /* Don't use this outside of linkhash.h: */
  300. #ifdef __UNCONST
  301. #define _LH_UNCONST(a) __UNCONST(a)
  302. #else
  303. #define _LH_UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
  304. #endif
  305. /**
  306. * Return a non-const version of lh_entry->k.
  307. * k is const to indicate and help ensure that linkhash itself doesn't modify
  308. * it, but callers are allowed to do what they want with it.
  309. * See also lh_entry->k_is_constant
  310. */
  311. #define lh_entry_k(entry) _LH_UNCONST((entry)->k)
  312. /**
  313. * Return a non-const version of lh_entry->v.
  314. * v is const to indicate and help ensure that linkhash itself doesn't modify
  315. * it, but callers are allowed to do what they want with it.
  316. */
  317. #define lh_entry_v(entry) _LH_UNCONST((entry)->v)
  318. #ifdef __cplusplus
  319. }
  320. #endif
  321. #endif