linkhash.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 a pointer onto the linkhash table.
  155. */
  156. extern struct lh_table* lh_table_new(int size, const char *name,
  157. lh_entry_free_fn *free_fn,
  158. lh_hash_fn *hash_fn,
  159. lh_equal_fn *equal_fn);
  160. /**
  161. * Convenience function to create a new linkhash
  162. * table with char keys.
  163. * @param size initial table size.
  164. * @param name table name.
  165. * @param free_fn callback function used to free memory for entries.
  166. * @return a pointer onto the linkhash table.
  167. */
  168. extern struct lh_table* lh_kchar_table_new(int size, const char *name,
  169. lh_entry_free_fn *free_fn);
  170. /**
  171. * Convenience function to create a new linkhash
  172. * table with ptr keys.
  173. * @param size initial table size.
  174. * @param name table name.
  175. * @param free_fn callback function used to free memory for entries.
  176. * @return a pointer onto the linkhash table.
  177. */
  178. extern struct lh_table* lh_kptr_table_new(int size, const char *name,
  179. lh_entry_free_fn *free_fn);
  180. /**
  181. * Free a linkhash table.
  182. * If a callback free function is provided then it is called for all
  183. * entries in the table.
  184. * @param t table to free.
  185. */
  186. extern void lh_table_free(struct lh_table *t);
  187. /**
  188. * Insert a record into the table.
  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. */
  193. extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
  194. /**
  195. * Lookup a record into the table.
  196. * @param t the table to lookup
  197. * @param k a pointer to the key to lookup
  198. * @return a pointer to the record structure of the value or NULL if it does not exist.
  199. */
  200. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  201. /**
  202. * Lookup a record into the table
  203. * @param t the table to lookup
  204. * @param k a pointer to the key to lookup
  205. * @return a pointer to the found value or NULL if it does not exist.
  206. * @deprecated Use lh_table_lookup_ex instead.
  207. */
  208. THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
  209. /**
  210. * Lookup a record in the table
  211. * @param t the table to lookup
  212. * @param k a pointer to the key to lookup
  213. * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
  214. * @return whether or not the key was found
  215. */
  216. extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
  217. /**
  218. * Delete a record from the table.
  219. * If a callback free function is provided then it is called for the
  220. * for the item being deleted.
  221. * @param t the table to delete from.
  222. * @param e a pointer to the entry to delete.
  223. * @return 0 if the item was deleted.
  224. * @return -1 if it was not found.
  225. */
  226. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  227. /**
  228. * Delete a record from the table.
  229. * If a callback free function is provided then it is called for the
  230. * for the item being deleted.
  231. * @param t the table to delete from.
  232. * @param k a pointer to the key to delete.
  233. * @return 0 if the item was deleted.
  234. * @return -1 if it was not found.
  235. */
  236. extern int lh_table_delete(struct lh_table *t, const void *k);
  237. extern int lh_table_length(struct lh_table *t);
  238. void lh_abort(const char *msg, ...);
  239. void lh_table_resize(struct lh_table *t, int new_size);
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif