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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 On success, a pointer to the new linkhash table is returned.
  138. * On error, a null pointer is returned.
  139. */
  140. extern struct lh_table* lh_table_new(int size,
  141. lh_entry_free_fn *free_fn,
  142. lh_hash_fn *hash_fn,
  143. lh_equal_fn *equal_fn);
  144. /**
  145. * Convenience function to create a new linkhash
  146. * table with char keys.
  147. * @param size initial table size.
  148. * @param name table name.
  149. * @param free_fn callback function used to free memory for entries.
  150. * @return On success, a pointer to the new linkhash table is returned.
  151. * On error, a null pointer is returned.
  152. */
  153. extern struct lh_table* lh_kchar_table_new(int size,
  154. lh_entry_free_fn *free_fn);
  155. /**
  156. * Convenience function to create a new linkhash
  157. * table with ptr keys.
  158. * @param size initial table size.
  159. * @param name table name.
  160. * @param free_fn callback function used to free memory for entries.
  161. * @return On success, a pointer to the new linkhash table is returned.
  162. * On error, a null pointer is returned.
  163. */
  164. extern struct lh_table* lh_kptr_table_new(int size,
  165. lh_entry_free_fn *free_fn);
  166. /**
  167. * Free a linkhash table.
  168. * If a callback free function is provided then it is called for all
  169. * entries in the table.
  170. * @param t table to free.
  171. */
  172. extern void lh_table_free(struct lh_table *t);
  173. /**
  174. * Insert a record into the table.
  175. * @param t the table to insert into.
  176. * @param k a pointer to the key to insert.
  177. * @param v a pointer to the value to insert.
  178. *
  179. * @return On success, <code>0</code> is returned.
  180. * On error, a negative value is returned.
  181. */
  182. extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
  183. /**
  184. * Insert a record into the table. This one accepts the key's hash in additon
  185. * to the key. This is an exension to support functions that need to calculate
  186. * the hash several times and allows them to do it just once and then pass
  187. * in the hash to all utility functions. Depending on use case, this can be a
  188. * very considerate performance improvement.
  189. * @param t the table to insert into.
  190. * @param k a pointer to the key to insert.
  191. * @param v a pointer to the value to insert.
  192. * @param h hash value of the key to insert
  193. * @param opts opts, a subset of JSON_OBJECT_ADD_* flags is supported
  194. */
  195. extern int lh_table_insert_w_hash(struct lh_table *t, void *k, const void *v, const unsigned long h, const unsigned opts);
  196. /**
  197. * Lookup a record into the table.
  198. * @param t the table to lookup
  199. * @param k a pointer to the key to lookup
  200. * @return a pointer to the record structure of the value or NULL if it does not exist.
  201. */
  202. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  203. /**
  204. * Lookup a record into the table. This one accepts the key's hash in additon
  205. * to the key. This is an exension to support functions that need to calculate
  206. * the hash several times and allows them to do it just once and then pass
  207. * in the hash to all utility functions. Depending on use case, this can be a
  208. * very considerate performance improvement.
  209. * @param t the table to lookup
  210. * @param k a pointer to the key to lookup
  211. * @param h hash value of 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_w_hash(struct lh_table *t, const void *k, const unsigned long h);
  215. /**
  216. * Lookup a record into the table
  217. * @param t the table to lookup
  218. * @param k a pointer to the key to lookup
  219. * @return a pointer to the found value or NULL if it does not exist.
  220. * @deprecated Use lh_table_lookup_ex instead.
  221. */
  222. THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
  223. /**
  224. * Lookup a record in the table
  225. * @param t the table to lookup
  226. * @param k a pointer to the key to lookup
  227. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  228. * @return whether or not the key was found
  229. */
  230. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  231. /**
  232. * Delete a record from the table.
  233. * If a callback free function is provided then it is called for the
  234. * for the item being deleted.
  235. * @param t the table to delete from.
  236. * @param e a pointer to the entry to delete.
  237. * @return 0 if the item was deleted.
  238. * @return -1 if it was not found.
  239. */
  240. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  241. /**
  242. * Delete a record from the table.
  243. * If a callback free function is provided then it is called for the
  244. * for the item being deleted.
  245. * @param t the table to delete from.
  246. * @param k a pointer to the key to delete.
  247. * @return 0 if the item was deleted.
  248. * @return -1 if it was not found.
  249. */
  250. extern int lh_table_delete(struct lh_table *t, const void *k);
  251. extern int lh_table_length(struct lh_table *t);
  252. /**
  253. * Prints a message to <code>stdout</code>,
  254. * then exits the program with an exit code of <code>1</code>.
  255. *
  256. * @param msg Message format string, like for <code>printf</code>.
  257. * @param ... Format args.
  258. *
  259. * @deprecated Since it is not a good idea to exit the entire program
  260. * because of an internal library failure, json-c will no longer
  261. * use this function internally.
  262. * However, because its interface is public, it will remain part of
  263. * the API on the off chance of legacy software using it externally.
  264. */
  265. void lh_abort(const char *msg, ...);
  266. /**
  267. * Resizes the specified table.
  268. *
  269. * @param t Pointer to table to resize.
  270. * @param new_size New table size. Must be positive.
  271. *
  272. * @return On success, <code>0</code> is returned.
  273. * On error, a negative value is returned.
  274. */
  275. int lh_table_resize(struct lh_table *t, int new_size);
  276. /**
  277. * Calculate the hash of a key for a given table.
  278. * This is an exension to support functions that need to calculate
  279. * the hash several times and allows them to do it just once and then pass
  280. * in the hash to all utility functions. Depending on use case, this can be a
  281. * very considerate performance improvement.
  282. * @param t the table (used to obtain hash function)
  283. * @param k a pointer to the key to lookup
  284. * @return the key's hash
  285. */
  286. static inline unsigned long lh_get_hash(const struct lh_table *t, const void *k)
  287. {
  288. return t->hash_fn(k);
  289. }
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif