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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /**
  58. * The value.
  59. */
  60. const void *v;
  61. /**
  62. * The next entry
  63. */
  64. struct lh_entry *next;
  65. /**
  66. * The previous entry.
  67. */
  68. struct lh_entry *prev;
  69. };
  70. /**
  71. * The hash table structure.
  72. */
  73. struct lh_table {
  74. /**
  75. * Size of our hash.
  76. */
  77. int size;
  78. /**
  79. * Numbers of entries.
  80. */
  81. int count;
  82. /**
  83. * Number of collisions.
  84. */
  85. int collisions;
  86. /**
  87. * Number of resizes.
  88. */
  89. int resizes;
  90. /**
  91. * Number of lookups.
  92. */
  93. int lookups;
  94. /**
  95. * Number of inserts.
  96. */
  97. int inserts;
  98. /**
  99. * Number of deletes.
  100. */
  101. int deletes;
  102. /**
  103. * Name of the hash table.
  104. */
  105. const char *name;
  106. /**
  107. * The first entry.
  108. */
  109. struct lh_entry *head;
  110. /**
  111. * The last entry.
  112. */
  113. struct lh_entry *tail;
  114. struct lh_entry *table;
  115. /**
  116. * A pointer onto the function responsible for freeing an entry.
  117. */
  118. lh_entry_free_fn *free_fn;
  119. lh_hash_fn *hash_fn;
  120. lh_equal_fn *equal_fn;
  121. };
  122. /**
  123. * Pre-defined hash and equality functions
  124. */
  125. extern unsigned long lh_ptr_hash(const void *k);
  126. extern int lh_ptr_equal(const void *k1, const void *k2);
  127. extern unsigned long lh_char_hash(const void *k);
  128. extern int lh_char_equal(const void *k1, const void *k2);
  129. /**
  130. * Convenience list iterator.
  131. */
  132. #define lh_foreach(table, entry) \
  133. for(entry = table->head; entry; entry = entry->next)
  134. /**
  135. * lh_foreach_safe allows calling of deletion routine while iterating.
  136. */
  137. #define lh_foreach_safe(table, entry, tmp) \
  138. for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  139. /**
  140. * Create a new linkhash table.
  141. * @param size initial table size. The table is automatically resized
  142. * although this incurs a performance penalty.
  143. * @param name the table name.
  144. * @param free_fn callback function used to free memory for entries
  145. * when lh_table_free or lh_table_delete is called.
  146. * If NULL is provided, then memory for keys and values
  147. * must be freed by the caller.
  148. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  149. * lh_ptr_hash and lh_char_hash for hashing pointer values
  150. * and C strings respectively.
  151. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  152. * lh_ptr_hash and lh_char_hash for comparing pointer values
  153. * and C strings respectively.
  154. * @return On success, a pointer to the new linkhash table is returned.
  155. * On error, a null pointer is returned.
  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 On success, a pointer to the new linkhash table is returned.
  168. * On error, a null pointer is returned.
  169. */
  170. extern struct lh_table* lh_kchar_table_new(int size, const char *name,
  171. lh_entry_free_fn *free_fn);
  172. /**
  173. * Convenience function to create a new linkhash
  174. * table with ptr keys.
  175. * @param size initial table size.
  176. * @param name table name.
  177. * @param free_fn callback function used to free memory for entries.
  178. * @return On success, a pointer to the new linkhash table is returned.
  179. * On error, a null pointer is returned.
  180. */
  181. extern struct lh_table* lh_kptr_table_new(int size, const char *name,
  182. lh_entry_free_fn *free_fn);
  183. /**
  184. * Free a linkhash table.
  185. * If a callback free function is provided then it is called for all
  186. * entries in the table.
  187. * @param t table to free.
  188. */
  189. extern void lh_table_free(struct lh_table *t);
  190. /**
  191. * Insert a record into the table.
  192. * @param t the table to insert into.
  193. * @param k a pointer to the key to insert.
  194. * @param v a pointer to the value to insert.
  195. *
  196. * @return On success, <code>0</code> is returned.
  197. * On error, a negative value is returned.
  198. */
  199. extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
  200. /**
  201. * Lookup a record into the table.
  202. * @param t the table to lookup
  203. * @param k a pointer to the key to lookup
  204. * @return a pointer to the record structure of the value or NULL if it does not exist.
  205. */
  206. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  207. /**
  208. * Lookup a record into the table
  209. * @param t the table to lookup
  210. * @param k a pointer to the key to lookup
  211. * @return a pointer to the found value or NULL if it does not exist.
  212. * @deprecated Use lh_table_lookup_ex instead.
  213. */
  214. extern const void* lh_table_lookup(struct lh_table *t, const void *k);
  215. /**
  216. * Lookup a record in the table
  217. * @param t the table to lookup
  218. * @param k a pointer to the key to lookup
  219. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  220. * @return whether or not the key was found
  221. */
  222. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  223. /**
  224. * Delete a record from the table.
  225. * If a callback free function is provided then it is called for the
  226. * for the item being deleted.
  227. * @param t the table to delete from.
  228. * @param e a pointer to the entry to delete.
  229. * @return 0 if the item was deleted.
  230. * @return -1 if it was not found.
  231. */
  232. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  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 k a pointer to the key 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(struct lh_table *t, const void *k);
  243. /**
  244. * Prints a message to <code>stdout</code>,
  245. * then exits the program with an exit code of <code>1</code>.
  246. *
  247. * @param msg Message format string, like for <code>printf</code>.
  248. * @param ... Format args.
  249. *
  250. * @deprecated Since it is not a good idea to exit the entire program
  251. * because of an internal library failure, json-c will no longer
  252. * use this function internally.
  253. * However, because its interface is public, it will remain part of
  254. * the API on the off chance of legacy software using it externally.
  255. */
  256. void lh_abort(const char *msg, ...);
  257. /**
  258. * Resizes the specified table.
  259. *
  260. * @param t Pointer to table to resize.
  261. * @param new_size New table size. Must be positive.
  262. *
  263. * @return On success, <code>0</code> is returned.
  264. * On error, a negative value is returned.
  265. */
  266. int lh_table_resize(struct lh_table *t, int new_size);
  267. #ifdef __cplusplus
  268. }
  269. #endif
  270. #endif