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.1 kB

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