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

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

No Description

Contributors (1)