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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 _json_c_linkhash_h_
  19. #define _json_c_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. Outside of linkhash.c, treat this as opaque.
  71. */
  72. struct lh_entry
  73. {
  74. /**
  75. * The key.
  76. * @deprecated Use lh_entry_k() instead of accessing this directly.
  77. */
  78. const void *k;
  79. /**
  80. * A flag for users of linkhash to know whether or not they
  81. * need to free k.
  82. * @deprecated use lh_entry_k_is_constant() instead.
  83. */
  84. int k_is_constant;
  85. /**
  86. * The value.
  87. * @deprecated Use lh_entry_v() instead of accessing this directly.
  88. */
  89. const void *v;
  90. /**
  91. * The next entry.
  92. * @deprecated Use lh_entry_next() instead of accessing this directly.
  93. */
  94. struct lh_entry *next;
  95. /**
  96. * The previous entry.
  97. * @deprecated Use lh_entry_prev() instead of accessing this directly.
  98. */
  99. struct lh_entry *prev;
  100. };
  101. /**
  102. * The hash table structure. Outside of linkhash.c, treat this as opaque.
  103. */
  104. struct lh_table
  105. {
  106. /**
  107. * Size of our hash.
  108. * @deprecated do not use outside of linkhash.c
  109. */
  110. int size;
  111. /**
  112. * Numbers of entries.
  113. * @deprecated Use lh_table_length() instead.
  114. */
  115. int count;
  116. /**
  117. * The first entry.
  118. * @deprecated Use lh_table_head() instead.
  119. */
  120. struct lh_entry *head;
  121. /**
  122. * The last entry.
  123. * @deprecated Do not use, may be removed in a future release.
  124. */
  125. struct lh_entry *tail;
  126. /**
  127. * Internal storage of the actual table of entries.
  128. * @deprecated do not use outside of linkhash.c
  129. */
  130. struct lh_entry *table;
  131. /**
  132. * A pointer to the function responsible for freeing an entry.
  133. * @deprecated do not use outside of linkhash.c
  134. */
  135. lh_entry_free_fn *free_fn;
  136. /**
  137. * @deprecated do not use outside of linkhash.c
  138. */
  139. lh_hash_fn *hash_fn;
  140. /**
  141. * @deprecated do not use outside of linkhash.c
  142. */
  143. lh_equal_fn *equal_fn;
  144. };
  145. typedef struct lh_table lh_table;
  146. /**
  147. * Convenience list iterator.
  148. */
  149. #define lh_foreach(table, entry) for (entry = lh_table_head(table); entry; entry = lh_entry_next(entry))
  150. /**
  151. * lh_foreach_safe allows calling of deletion routine while iterating.
  152. *
  153. * @param table a struct lh_table * to iterate over
  154. * @param entry a struct lh_entry * variable to hold each element
  155. * @param tmp a struct lh_entry * variable to hold a temporary pointer to the next element
  156. */
  157. #define lh_foreach_safe(table, entry, tmp) \
  158. for (entry = lh_table_head(table); entry && ((tmp = lh_entry_next(entry)) || 1); entry = tmp)
  159. /**
  160. * Create a new linkhash table.
  161. *
  162. * @param size initial table size. The table is automatically resized
  163. * although this incurs a performance penalty.
  164. * @param free_fn callback function used to free memory for entries
  165. * when lh_table_free or lh_table_delete is called.
  166. * If NULL is provided, then memory for keys and values
  167. * must be freed by the caller.
  168. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  169. * lh_ptr_hash and lh_char_hash for hashing pointer values
  170. * and C strings respectively.
  171. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  172. * lh_ptr_hash and lh_char_hash for comparing pointer values
  173. * and C strings respectively.
  174. * @return On success, a pointer to the new linkhash table is returned.
  175. * On error, a null pointer is returned.
  176. */
  177. extern struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn,
  178. lh_equal_fn *equal_fn);
  179. /**
  180. * Convenience function to create a new linkhash table with char keys.
  181. *
  182. * @param size initial table size.
  183. * @param free_fn callback function used to free memory for entries.
  184. * @return On success, a pointer to the new linkhash table is returned.
  185. * On error, a null pointer is returned.
  186. */
  187. extern struct lh_table *lh_kchar_table_new(int size, lh_entry_free_fn *free_fn);
  188. /**
  189. * Convenience function to create a new linkhash table with ptr keys.
  190. *
  191. * @param size initial table size.
  192. * @param free_fn callback function used to free memory for entries.
  193. * @return On success, a pointer to the new linkhash table is returned.
  194. * On error, a null pointer is returned.
  195. */
  196. extern struct lh_table *lh_kptr_table_new(int size, lh_entry_free_fn *free_fn);
  197. /**
  198. * Free a linkhash table.
  199. *
  200. * If a lh_entry_free_fn callback free function was provided then it is
  201. * called for all entries in the table.
  202. *
  203. * @param t table to free.
  204. */
  205. extern void lh_table_free(struct lh_table *t);
  206. /**
  207. * Insert a record into the table.
  208. *
  209. * @param t the table to insert into.
  210. * @param k a pointer to the key to insert.
  211. * @param v a pointer to the value to insert.
  212. *
  213. * @return On success, <code>0</code> is returned.
  214. * On error, a negative value is returned.
  215. */
  216. extern int lh_table_insert(struct lh_table *t, const void *k, const void *v);
  217. /**
  218. * Insert a record into the table using a precalculated key hash.
  219. *
  220. * The hash h, which should be calculated with lh_get_hash() on k, is provided by
  221. * the caller, to allow for optimization when multiple operations with the same
  222. * key are known to be needed.
  223. *
  224. * @param t the table to insert into.
  225. * @param k a pointer to the key to insert.
  226. * @param v a pointer to the value to insert.
  227. * @param h hash value of the key to insert
  228. * @param opts if set to JSON_C_OBJECT_ADD_CONSTANT_KEY, sets lh_entry.k_is_constant
  229. * so t's free function knows to avoid freeing the key.
  230. */
  231. extern int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v,
  232. const unsigned long h, const unsigned opts);
  233. /**
  234. * Lookup a record in the table.
  235. *
  236. * @param t the table to lookup
  237. * @param k a pointer to the key to lookup
  238. * @return a pointer to the record structure of the value or NULL if it does not exist.
  239. */
  240. extern struct lh_entry *lh_table_lookup_entry(struct lh_table *t, const void *k);
  241. /**
  242. * Lookup a record in the table using a precalculated key hash.
  243. *
  244. * The hash h, which should be calculated with lh_get_hash() on k, is provided by
  245. * the caller, to allow for optimization when multiple operations with the same
  246. * key are known to be needed.
  247. *
  248. * @param t the table to lookup
  249. * @param k a pointer to the key to lookup
  250. * @param h hash value of the key to lookup
  251. * @return a pointer to the record structure of the value or NULL if it does not exist.
  252. */
  253. extern struct lh_entry *lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k,
  254. const unsigned long h);
  255. /**
  256. * Lookup a record in the table.
  257. *
  258. * @param t the table to lookup
  259. * @param k a pointer to the key to lookup
  260. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  261. * @return whether or not the key was found
  262. */
  263. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  264. /**
  265. * Delete a record from the table.
  266. *
  267. * If a callback free function is provided then it is called for the
  268. * for the item being deleted.
  269. * @param t the table to delete from.
  270. * @param e a pointer to the entry to delete.
  271. * @return 0 if the item was deleted.
  272. * @return -1 if it was not found.
  273. */
  274. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  275. /**
  276. * Delete a record from the table.
  277. *
  278. * If a callback free function is provided then it is called for the
  279. * for the item being deleted.
  280. * @param t the table to delete from.
  281. * @param k a pointer to the key to delete.
  282. * @return 0 if the item was deleted.
  283. * @return -1 if it was not found.
  284. */
  285. extern int lh_table_delete(struct lh_table *t, const void *k);
  286. /**
  287. * Return the number of entries in the table.
  288. */
  289. extern int lh_table_length(struct lh_table *t);
  290. /**
  291. * Resizes the specified table.
  292. *
  293. * @param t Pointer to table to resize.
  294. * @param new_size New table size. Must be positive.
  295. *
  296. * @return On success, <code>0</code> is returned.
  297. * On error, a negative value is returned.
  298. */
  299. int lh_table_resize(struct lh_table *t, int new_size);
  300. /**
  301. * @deprecated Don't use this outside of linkhash.h:
  302. */
  303. #if !defined (__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
  304. /* C89 compilers like VS2010 can't handle inline funcs, so skip it there,
  305. note: this also applies to -std=c89 in GCC! */
  306. #define _LH_INLINE
  307. #else
  308. #define _LH_INLINE inline
  309. #endif
  310. /**
  311. * Return the first entry in the lh_table.
  312. * @see lh_entry_next()
  313. */
  314. static _LH_INLINE struct lh_entry *lh_table_head(const lh_table *t)
  315. {
  316. return t->head;
  317. }
  318. /**
  319. * Calculate the hash of a key for a given table.
  320. *
  321. * This is an extension to support functions that need to calculate
  322. * the hash several times and allows them to do it just once and then pass
  323. * in the hash to all utility functions. Depending on use case, this can be a
  324. * considerable performance improvement.
  325. * @param t the table (used to obtain hash function)
  326. * @param k a pointer to the key to lookup
  327. * @return the key's hash
  328. */
  329. static _LH_INLINE unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  330. {
  331. return t->hash_fn(k);
  332. }
  333. /**
  334. * @deprecated Don't use this outside of linkhash.h:
  335. */
  336. #ifdef __UNCONST
  337. #define _LH_UNCONST(a) __UNCONST(a)
  338. #else
  339. #define _LH_UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
  340. #endif
  341. /**
  342. * Return a non-const version of lh_entry.k.
  343. *
  344. * lh_entry.k is const to indicate and help ensure that linkhash itself doesn't modify
  345. * it, but callers are allowed to do what they want with it.
  346. * @see lh_entry_k_is_constant()
  347. */
  348. static _LH_INLINE void *lh_entry_k(const struct lh_entry *e)
  349. {
  350. return _LH_UNCONST(e->k);
  351. }
  352. /**
  353. * Returns 1 if the key for the given entry is constant, and thus
  354. * does not need to be freed when the lh_entry is freed.
  355. * @see lh_table_insert_w_hash()
  356. */
  357. static _LH_INLINE int lh_entry_k_is_constant(const struct lh_entry *e)
  358. {
  359. return e->k_is_constant;
  360. }
  361. /**
  362. * Return a non-const version of lh_entry.v.
  363. *
  364. * v is const to indicate and help ensure that linkhash itself doesn't modify
  365. * it, but callers are allowed to do what they want with it.
  366. */
  367. static _LH_INLINE void *lh_entry_v(const struct lh_entry *e)
  368. {
  369. return _LH_UNCONST(e->v);
  370. }
  371. /**
  372. * Change the value for an entry. The caller is responsible for freeing
  373. * the previous value.
  374. */
  375. static _LH_INLINE void lh_entry_set_val(struct lh_entry *e, void *newval)
  376. {
  377. e->v = newval;
  378. }
  379. /**
  380. * Return the next element, or NULL if there is no next element.
  381. * @see lh_table_head()
  382. * @see lh_entry_prev()
  383. */
  384. static _LH_INLINE struct lh_entry *lh_entry_next(const struct lh_entry *e)
  385. {
  386. return e->next;
  387. }
  388. /**
  389. * Return the previous element, or NULL if there is no previous element.
  390. * @see lh_table_head()
  391. * @see lh_entry_next()
  392. */
  393. static _LH_INLINE struct lh_entry *lh_entry_prev(const struct lh_entry *e)
  394. {
  395. return e->prev;
  396. }
  397. #undef _LH_INLINE
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. #endif