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

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

No Description

Contributors (1)