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

No Description

Contributors (1)