HashTable.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @file HashTable.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Hash table using separate chaining.
  25. */
  26. #ifndef BADVPN_STRUCTURE_HASHTABLE_H
  27. #define BADVPN_STRUCTURE_HASHTABLE_H
  28. #include <stdint.h>
  29. #include <stddef.h>
  30. #include <misc/debug.h>
  31. #include <system/DebugObject.h>
  32. /**
  33. * Handler function used to compare values.
  34. * For any two values, the comparator must always return the same result.
  35. * Values are obtained like that:
  36. * - The value of a node in the table, or a node that is being inserted is:
  37. * (uint8_t *)node + offset.
  38. * - The value being looked up is the same as given to the lookup function.
  39. *
  40. * @param val1 first value
  41. * @param val2 second value
  42. * @return 1 if values are equal, 0 if not
  43. */
  44. typedef int (*HashTable_comparator) (void *val1, void *val2);
  45. /**
  46. * Handler function used to compute the hash of a value, modulo some number.
  47. * The value is obtained the same way as in {@link HashTable_comparator}.
  48. *
  49. * @param val value whose hash function is to be computed
  50. * @param modulo an integer modulo which the hash must be taken before returning it
  51. * @return hash of value modulo parameter
  52. */
  53. typedef int (*HashTable_hash_function) (void *val, int modulo);
  54. struct HashTableNode_t;
  55. /**
  56. * Hash table using separate chaining.
  57. */
  58. typedef struct {
  59. DebugObject d_obj;
  60. int offset;
  61. HashTable_comparator comparator;
  62. HashTable_hash_function hash_function;
  63. int num_buckets;
  64. struct HashTableNode_t **buckets;
  65. #ifndef NDEBUG
  66. int in_handler;
  67. #endif
  68. } HashTable;
  69. /**
  70. * Hash table node.
  71. */
  72. typedef struct HashTableNode_t {
  73. struct HashTableNode_t *next;
  74. } HashTableNode;
  75. /**
  76. * Initializes the hash table.
  77. *
  78. * @param t the object
  79. * @param offset offset of a value from its node
  80. * @param comparator value comparator handler function
  81. * @param hash_function value hash function handler function
  82. * @param size number of buckets in the hash table. Must be >0.
  83. * @return 1 on success, 0 on failure
  84. */
  85. static int HashTable_Init (HashTable *t, int offset, HashTable_comparator comparator, HashTable_hash_function hash_function, int size) WARN_UNUSED;
  86. /**
  87. * Frees the hash table.
  88. * Must not be called from handler functions.
  89. *
  90. * @param t the object
  91. */
  92. static void HashTable_Free (HashTable *t);
  93. /**
  94. * Inserts a node into the hash table.
  95. * Must not be called from handler functions.
  96. *
  97. * @param t the object
  98. * @param node uninitialized node to insert. Must have a valid value (its value
  99. * may be passed to the comparator or hash function during insertion).
  100. * @return 1 on success, 0 if an element with an equal value is already in the table
  101. */
  102. static int HashTable_Insert (HashTable *t, HashTableNode *node);
  103. /**
  104. * Removes a node from the table by value.
  105. * The node must be in the hash table.
  106. * Must not be called from handler functions.
  107. *
  108. * @param t the object
  109. * @param val value of the node to be removed.
  110. * @return 1 on success, 0 if there is no node with the given value
  111. */
  112. static int HashTable_Remove (HashTable *t, void *val);
  113. /**
  114. * Looks up the node of the value given.
  115. * Must not be called from handler functions.
  116. *
  117. * @param t the object
  118. * @param val value to look up
  119. * @param node if not NULL, will be set to the node pointer on success
  120. * @return 1 on success, 0 if there is no node with the given value
  121. */
  122. static int HashTable_Lookup (HashTable *t, void *val, HashTableNode **node);
  123. static int _HashTable_compare_values (HashTable *t, void *v1, void *v2)
  124. {
  125. #ifndef NDEBUG
  126. t->in_handler = 1;
  127. #endif
  128. int res = t->comparator(v1, v2);
  129. #ifndef NDEBUG
  130. t->in_handler = 0;
  131. #endif
  132. ASSERT(res == 0 || res == 1)
  133. return res;
  134. }
  135. static int _HashTable_compute_hash (HashTable *t, void *v)
  136. {
  137. #ifndef NDEBUG
  138. t->in_handler = 1;
  139. #endif
  140. int res = t->hash_function(v, t->num_buckets);
  141. #ifndef NDEBUG
  142. t->in_handler = 0;
  143. #endif
  144. ASSERT(res >= 0)
  145. ASSERT(res < t->num_buckets)
  146. return res;
  147. }
  148. int HashTable_Init (HashTable *t, int offset, HashTable_comparator comparator, HashTable_hash_function hash_function, int size)
  149. {
  150. ASSERT(size > 0)
  151. // init arguments
  152. t->offset = offset;
  153. t->comparator = comparator;
  154. t->hash_function = hash_function;
  155. t->num_buckets = size;
  156. // allocate buckets
  157. t->buckets = (HashTableNode **)malloc(t->num_buckets * sizeof(HashTableNode *));
  158. if (!t->buckets) {
  159. return 0;
  160. }
  161. // zero buckets
  162. int i;
  163. for (i = 0; i < t->num_buckets; i++) {
  164. t->buckets[i] = NULL;
  165. }
  166. // init debugging
  167. #ifndef NDEBUG
  168. t->in_handler = 0;
  169. #endif
  170. // init debug object
  171. DebugObject_Init(&t->d_obj);
  172. return 1;
  173. }
  174. void HashTable_Free (HashTable *t)
  175. {
  176. // free debug object
  177. DebugObject_Free(&t->d_obj);
  178. ASSERT(!t->in_handler)
  179. // free buckets
  180. free(t->buckets);
  181. }
  182. int HashTable_Insert (HashTable *t, HashTableNode *node)
  183. {
  184. ASSERT(!t->in_handler)
  185. // obtain value
  186. void *val = (uint8_t *)node + t->offset;
  187. // obtain bucket index
  188. int index = _HashTable_compute_hash(t, val);
  189. // look for existing entries with an equal value
  190. HashTableNode *cur = t->buckets[index];
  191. while (cur) {
  192. void *cur_val = (uint8_t *)cur + t->offset;
  193. if (_HashTable_compare_values(t, cur_val, val)) {
  194. return 0;
  195. }
  196. cur = cur->next;
  197. }
  198. // prepend to linked list
  199. node->next = t->buckets[index];
  200. t->buckets[index] = node;
  201. return 1;
  202. }
  203. int HashTable_Remove (HashTable *t, void *val)
  204. {
  205. ASSERT(!t->in_handler)
  206. // obtain bucket index
  207. int index = _HashTable_compute_hash(t, val);
  208. // find node with an equal value
  209. HashTableNode *prev = NULL;
  210. HashTableNode *cur = t->buckets[index];
  211. while (cur) {
  212. void *cur_val = (uint8_t *)cur + t->offset;
  213. if (_HashTable_compare_values(t, cur_val, val)) {
  214. // remove node from lined list
  215. if (prev) {
  216. prev->next = cur->next;
  217. } else {
  218. t->buckets[index] = cur->next;
  219. }
  220. return 1;
  221. }
  222. prev = cur;
  223. cur = cur->next;
  224. }
  225. return 0;
  226. }
  227. int HashTable_Lookup (HashTable *t, void *val, HashTableNode **node)
  228. {
  229. ASSERT(!t->in_handler)
  230. int index = _HashTable_compute_hash(t, val);
  231. // find node with an equal value
  232. HashTableNode *cur = t->buckets[index];
  233. while (cur) {
  234. void *cur_val = (uint8_t *)cur + t->offset;
  235. if (_HashTable_compare_values(t, cur_val, val)) {
  236. if (node) {
  237. *node = cur;
  238. }
  239. return 1;
  240. }
  241. cur = cur->next;
  242. }
  243. return 0;
  244. }
  245. #endif