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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #ifndef _linkhash_h_
  12. #define _linkhash_h_
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /**
  17. * golden prime used in hash functions
  18. */
  19. #define LH_PRIME 0x9e370001UL
  20. /**
  21. * The fraction of filled hash buckets until an insert will cause the table
  22. * to be resized.
  23. * This can range from just above 0 up to 1.0.
  24. */
  25. #define LH_LOAD_FACTOR 0.66
  26. /**
  27. * sentinel pointer value for empty slots
  28. */
  29. #define LH_EMPTY (void*)-1
  30. /**
  31. * sentinel pointer value for freed slots
  32. */
  33. #define LH_FREED (void*)-2
  34. struct lh_entry;
  35. /**
  36. * callback function prototypes
  37. */
  38. typedef void (lh_entry_free_fn) (struct lh_entry *e);
  39. /**
  40. * callback function prototypes
  41. */
  42. typedef unsigned long (lh_hash_fn) (const void *k);
  43. /**
  44. * callback function prototypes
  45. */
  46. typedef int (lh_equal_fn) (const void *k1, const void *k2);
  47. /**
  48. * An entry in the hash table
  49. */
  50. struct lh_entry {
  51. /**
  52. * The key.
  53. */
  54. void *k;
  55. /**
  56. * The value.
  57. */
  58. const void *v;
  59. /**
  60. * The next entry
  61. */
  62. struct lh_entry *next;
  63. /**
  64. * The previous entry.
  65. */
  66. struct lh_entry *prev;
  67. };
  68. /**
  69. * The hash table structure.
  70. */
  71. struct lh_table {
  72. /**
  73. * Size of our hash.
  74. */
  75. int size;
  76. /**
  77. * Numbers of entries.
  78. */
  79. int count;
  80. /**
  81. * Number of collisions.
  82. */
  83. int collisions;
  84. /**
  85. * Number of resizes.
  86. */
  87. int resizes;
  88. /**
  89. * Number of lookups.
  90. */
  91. int lookups;
  92. /**
  93. * Number of inserts.
  94. */
  95. int inserts;
  96. /**
  97. * Number of deletes.
  98. */
  99. int deletes;
  100. /**
  101. * Name of the hash table.
  102. */
  103. const char *name;
  104. /**
  105. * The first entry.
  106. */
  107. struct lh_entry *head;
  108. /**
  109. * The last entry.
  110. */
  111. struct lh_entry *tail;
  112. struct lh_entry *table;
  113. /**
  114. * A pointer onto the function responsible for freeing an entry.
  115. */
  116. lh_entry_free_fn *free_fn;
  117. lh_hash_fn *hash_fn;
  118. lh_equal_fn *equal_fn;
  119. };
  120. /**
  121. * Pre-defined hash and equality functions
  122. */
  123. extern unsigned long lh_ptr_hash(const void *k);
  124. extern int lh_ptr_equal(const void *k1, const void *k2);
  125. extern unsigned long lh_char_hash(const void *k);
  126. extern int lh_char_equal(const void *k1, const void *k2);
  127. /**
  128. * Convenience list iterator.
  129. */
  130. #define lh_foreach(table, entry) \
  131. for(entry = table->head; entry; entry = entry->next)
  132. /**
  133. * lh_foreach_safe allows calling of deletion routine while iterating.
  134. */
  135. #define lh_foreach_safe(table, entry, tmp) \
  136. for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
  137. /**
  138. * Create a new linkhash table.
  139. * @param size initial table size. The table is automatically resized
  140. * although this incurs a performance penalty.
  141. * @param name the table name.
  142. * @param free_fn callback function used to free memory for entries
  143. * when lh_table_free or lh_table_delete is called.
  144. * If NULL is provided, then memory for keys and values
  145. * must be freed by the caller.
  146. * @param hash_fn function used to hash keys. 2 standard ones are defined:
  147. * lh_ptr_hash and lh_char_hash for hashing pointer values
  148. * and C strings respectively.
  149. * @param equal_fn comparison function to compare keys. 2 standard ones defined:
  150. * lh_ptr_hash and lh_char_hash for comparing pointer values
  151. * and C strings respectively.
  152. * @return a pointer onto the linkhash table.
  153. */
  154. extern struct lh_table* lh_table_new(int size, const char *name,
  155. lh_entry_free_fn *free_fn,
  156. lh_hash_fn *hash_fn,
  157. lh_equal_fn *equal_fn);
  158. /**
  159. * Convenience function to create a new linkhash
  160. * table with char keys.
  161. * @param size initial table size.
  162. * @param name table name.
  163. * @param free_fn callback function used to free memory for entries.
  164. * @return a pointer onto the linkhash table.
  165. */
  166. extern struct lh_table* lh_kchar_table_new(int size, const char *name,
  167. lh_entry_free_fn *free_fn);
  168. /**
  169. * Convenience function to create a new linkhash
  170. * table with ptr keys.
  171. * @param size initial table size.
  172. * @param name table name.
  173. * @param free_fn callback function used to free memory for entries.
  174. * @return a pointer onto the linkhash table.
  175. */
  176. extern struct lh_table* lh_kptr_table_new(int size, const char *name,
  177. lh_entry_free_fn *free_fn);
  178. /**
  179. * Free a linkhash table.
  180. * If a callback free function is provided then it is called for all
  181. * entries in the table.
  182. * @param t table to free.
  183. */
  184. extern void lh_table_free(struct lh_table *t);
  185. /**
  186. * Insert a record into the table.
  187. * @param t the table to insert into.
  188. * @param k a pointer to the key to insert.
  189. * @param v a pointer to the value to insert.
  190. */
  191. extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
  192. /**
  193. * Lookup a record into the table.
  194. * @param t the table to lookup
  195. * @param k a pointer to the key to lookup
  196. * @return a pointer to the record structure of the value or NULL if it does not exist.
  197. */
  198. extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
  199. /**
  200. * Lookup a record into the table
  201. * @param t the table to lookup
  202. * @param k a pointer to the key to lookup
  203. * @return a pointer to the found value or NULL if it does not exist.
  204. */
  205. extern const void* lh_table_lookup(struct lh_table *t, const void *k);
  206. /**
  207. * Delete a record from the table.
  208. * If a callback free function is provided then it is called for the
  209. * for the item being deleted.
  210. * @param t the table to delete from.
  211. * @param e a pointer to the entry to delete.
  212. * @return 0 if the item was deleted.
  213. * @return -1 if it was not found.
  214. */
  215. extern int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e);
  216. /**
  217. * Delete a record from the table.
  218. * If a callback free function is provided then it is called for the
  219. * for the item being deleted.
  220. * @param t the table to delete from.
  221. * @param k a pointer to the key to delete.
  222. * @return 0 if the item was deleted.
  223. * @return -1 if it was not found.
  224. */
  225. extern int lh_table_delete(struct lh_table *t, const void *k);
  226. void lh_abort(const char *msg, ...);
  227. void lh_table_resize(struct lh_table *t, int new_size);
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif