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

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

No Description

Contributors (1)