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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. /**
  13. * @file
  14. * @brief Internal methods for working with json_type_object objects. Although
  15. * this is exposed by the json_object_get_object() function and within the
  16. * json_object_iter type, it is not recommended for direct use.
  17. */
  18. #ifndef _linkhash_h_
  19. #define _linkhash_h_
  20. #include "json_object.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * golden prime used in hash functions
  26. */
  27. #define LH_PRIME 0x9e370001UL
  28. /**
  29. * The fraction of filled hash buckets until an insert will cause the table
  30. * to be resized.
  31. * This can range from just above 0 up to 1.0.
  32. */
  33. #define LH_LOAD_FACTOR 0.66
  34. /**
  35. * sentinel pointer value for empty slots
  36. */
  37. #define LH_EMPTY (void*)-1
  38. /**
  39. * sentinel pointer value for freed slots
  40. */
  41. #define LH_FREED (void*)-2
  42. /**
  43. * default string hash function
  44. */
  45. #define JSON_C_STR_HASH_DFLT 0
  46. /**
  47. * perl-like string hash function
  48. */
  49. #define JSON_C_STR_HASH_PERLLIKE 1
  50. /**
  51. * This function sets the hash function to be used for strings.
  52. * Must be one of the JSON_C_STR_HASH_* values.
  53. * @returns 0 - ok, -1 if parameter was invalid
  54. */
  55. int json_global_set_string_hash(const int h);
  56. struct lh_entry;
  57. /**
  58. * callback function prototypes
  59. */
  60. typedef void (lh_entry_free_fn) (struct lh_entry *e);
  61. /**
  62. * callback function prototypes
  63. */
  64. typedef unsigned long (lh_hash_fn) (const void *k);
  65. /**
  66. * callback function prototypes
  67. */
  68. typedef int (lh_equal_fn) (const void *k1, const void *k2);
  69. /**
  70. * An entry in the hash table
  71. */
  72. struct lh_entry {
  73. /**
  74. * The key. Use lh_entry_k() instead of accessing this directly.
  75. */
  76. const void *k;
  77. /**
  78. * A flag for users of linkhash to know whether or not they
  79. * need to free k.
  80. */
  81. int k_is_constant;
  82. /**
  83. * The value. Use lh_entry_v() instead of accessing this directly.
  84. */
  85. const void *v;
  86. /**
  87. * The next entry
  88. */
  89. struct lh_entry *next;
  90. /**
  91. * The previous entry.
  92. */
  93. struct lh_entry *prev;
  94. };
  95. /**
  96. * The hash table structure.
  97. */
  98. struct lh_table {
  99. /**
  100. * Size of our hash.
  101. */
  102. int size;
  103. /**
  104. * Numbers of entries.
  105. */
  106. int count;
  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. * Convenience list iterator.
  125. */
  126. #define lh_foreach(table, entry) \
  127. for(entry = table->head; entry; entry = entry->next)
  128. /**
  129. * lh_foreach_safe allows calling of deletion routine while iterating.
  130. */
  131. #define lh_foreach_safe(table, entry, tmp) \
  132. for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  133. /**
  134. * Create a new linkhash table.
  135. * @param size initial table size. The table is automatically resized
  136. * although this incurs a performance penalty.
  137. * @param free_fn callback function used to free memory for entries
  138. * when lh_table_free or lh_table_delete is called.
  139. * If NULL is provided, then memory for keys and values
  140. * must be freed by the caller.
  141. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  142. * lh_ptr_hash and lh_char_hash for hashing pointer values
  143. * and C strings respectively.
  144. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  145. * lh_ptr_hash and lh_char_hash for comparing pointer values
  146. * and C strings respectively.
  147. * @return On success, a pointer to the new linkhash table is returned.
  148. * On error, a null pointer is returned.
  149. */
  150. extern struct lh_table* lh_table_new(int size,
  151. lh_entry_free_fn *free_fn,
  152. lh_hash_fn *hash_fn,
  153. lh_equal_fn *equal_fn);
  154. /**
  155. * Convenience function to create a new linkhash
  156. * table with char keys.
  157. * @param size initial table size.
  158. * @param free_fn callback function used to free memory for entries.
  159. * @return On success, a pointer to the new linkhash table is returned.
  160. * On error, a null pointer is returned.
  161. */
  162. extern struct lh_table* lh_kchar_table_new(int size,
  163. lh_entry_free_fn *free_fn);
  164. /**
  165. * Convenience function to create a new linkhash
  166. * table with ptr keys.
  167. * @param size initial table size.
  168. * @param free_fn callback function used to free memory for entries.
  169. * @return On success, a pointer to the new linkhash table is returned.
  170. * On error, a null pointer is returned.
  171. */
  172. extern struct lh_table* lh_kptr_table_new(int size,
  173. lh_entry_free_fn *free_fn);
  174. /**
  175. * Free a linkhash table.
  176. * If a callback free function is provided then it is called for all
  177. * entries in the table.
  178. * @param t table to free.
  179. */
  180. extern void lh_table_free(struct lh_table *t);
  181. /**
  182. * Insert a record into the table.
  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. *
  187. * @return On success, <code>0</code> is returned.
  188. * On error, a negative value is returned.
  189. */
  190. extern int lh_table_insert(struct lh_table *t, const void *k, const void *v);
  191. /**
  192. * Insert a record into the table. This one accepts the key's hash in additon
  193. * to the key. This is an exension to support functions that need to calculate
  194. * the hash several times and allows them to do it just once and then pass
  195. * in the hash to all utility functions. Depending on use case, this can be a
  196. * very considerate performance improvement.
  197. * @param t the table to insert into.
  198. * @param k a pointer to the key to insert.
  199. * @param v a pointer to the value to insert.
  200. * @param h hash value of the key to insert
  201. * @param opts opts, a subset of JSON_OBJECT_ADD_* flags is supported
  202. */
  203. extern int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h, const unsigned opts);
  204. /**
  205. * Lookup a record into the table.
  206. * @param t the table to lookup
  207. * @param k a pointer to the key to lookup
  208. * @return a pointer to the record structure of the value or NULL if it does not exist.
  209. */
  210. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  211. /**
  212. * Lookup a record into the table. This one accepts the key's hash in additon
  213. * to the key. This is an exension to support functions that need to calculate
  214. * the hash several times and allows them to do it just once and then pass
  215. * in the hash to all utility functions. Depending on use case, this can be a
  216. * very considerate performance improvement.
  217. * @param t the table to lookup
  218. * @param k a pointer to the key to lookup
  219. * @param h hash value of the key to lookup
  220. * @return a pointer to the record structure of the value or NULL if it does not exist.
  221. */
  222. extern struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h);
  223. /**
  224. * Lookup a record into the table
  225. * @param t the table to lookup
  226. * @param k a pointer to the key to lookup
  227. * @return a pointer to the found value or NULL if it does not exist.
  228. * @deprecated Use lh_table_lookup_ex instead.
  229. */
  230. THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
  231. /**
  232. * Lookup a record in the table
  233. * @param t the table to lookup
  234. * @param k a pointer to the key to lookup
  235. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  236. * @return whether or not the key was found
  237. */
  238. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  239. /**
  240. * Delete a record from the table.
  241. * If a callback free function is provided then it is called for the
  242. * for the item being deleted.
  243. * @param t the table to delete from.
  244. * @param e a pointer to the entry to delete.
  245. * @return 0 if the item was deleted.
  246. * @return -1 if it was not found.
  247. */
  248. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  249. /**
  250. * Delete a record from the table.
  251. * If a callback free function is provided then it is called for the
  252. * for the item being deleted.
  253. * @param t the table to delete from.
  254. * @param k a pointer to the key to delete.
  255. * @return 0 if the item was deleted.
  256. * @return -1 if it was not found.
  257. */
  258. extern int lh_table_delete(struct lh_table *t, const void *k);
  259. extern int lh_table_length(struct lh_table *t);
  260. /**
  261. * Prints a message to <code>stdout</code>,
  262. * then exits the program with an exit code of <code>1</code>.
  263. *
  264. * @param msg Message format string, like for <code>printf</code>.
  265. * @param ... Format args.
  266. *
  267. * @deprecated Since it is not a good idea to exit the entire program
  268. * because of an internal library failure, json-c will no longer
  269. * use this function internally.
  270. * However, because its interface is public, it will remain part of
  271. * the API on the off chance of legacy software using it externally.
  272. */
  273. THIS_FUNCTION_IS_DEPRECATED(void lh_abort(const char *msg, ...));
  274. /**
  275. * Resizes the specified table.
  276. *
  277. * @param t Pointer to table to resize.
  278. * @param new_size New table size. Must be positive.
  279. *
  280. * @return On success, <code>0</code> is returned.
  281. * On error, a negative value is returned.
  282. */
  283. int lh_table_resize(struct lh_table *t, int new_size);
  284. #if !defined(_MSC_VER) || (_MSC_VER > 1800)
  285. /* VS2010 can't handle inline funcs, so skip it there */
  286. #define _LH_INLINE inline
  287. #else
  288. #define _LH_INLINE
  289. #endif
  290. /**
  291. * Calculate the hash of a key for a given table.
  292. * This is an exension to support functions that need to calculate
  293. * the hash several times and allows them to do it just once and then pass
  294. * in the hash to all utility functions. Depending on use case, this can be a
  295. * very considerate performance improvement.
  296. * @param t the table (used to obtain hash function)
  297. * @param k a pointer to the key to lookup
  298. * @return the key's hash
  299. */
  300. static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  301. {
  302. return t->hash_fn(k);
  303. }
  304. #undef _LH_INLINE
  305. /* Don't use this outside of linkhash.h: */
  306. #ifdef __UNCONST
  307. #define _LH_UNCONST(a) __UNCONST(a)
  308. #else
  309. #define _LH_UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
  310. #endif
  311. /**
  312. * Return a non-const version of lh_entry->k.
  313. * k is const to indicate and help ensure that linkhash itself doesn't modify
  314. * it, but callers are allowed to do what they want with it.
  315. * See also lh_entry->k_is_constant
  316. */
  317. #define lh_entry_k(entry) _LH_UNCONST((entry)->k)
  318. /**
  319. * Return a non-const version of lh_entry->v.
  320. * v is const to indicate and help ensure that linkhash itself doesn't modify
  321. * it, but callers are allowed to do what they want with it.
  322. */
  323. #define lh_entry_v(entry) _LH_UNCONST((entry)->v)
  324. #ifdef __cplusplus
  325. }
  326. #endif
  327. #endif