hash_map.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*!
  2. Temelia - Hash map implementation source file.
  3. Hash map maps keys to values; it's implemented using red-black trees, because
  4. those are the fastest data structures at general operations: search, insert and remove.
  5. Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).
  6. @author Dascalu Laurentiu
  7. This program is free software; you can redistribute it and
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 3
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "include/common.h"
  20. #include "include/hash_map.h"
  21. #include "include/red_black_tree.h"
  22. #include "include/pair.h"
  23. #include <stdlib.h>
  24. struct _hash_map_t
  25. {
  26. /*!
  27. * Hash map's red black tree root
  28. */
  29. red_black_tree_t root;
  30. int size;
  31. };
  32. static void _hash_map_node_delete(void *key, void *context)
  33. {
  34. pair_delete(key);
  35. }
  36. static int _hash_map_compare_keys(void *node1, void *node2, void *context)
  37. {
  38. pair_t context_pair = (pair_t) context;
  39. int (*user_compare)(void *key1, void *key2, void *context) = pair_get_key(
  40. context_pair);
  41. void *user_context = pair_get_value(context_pair);
  42. return user_compare(pair_get_key(node1), pair_get_key(node2), user_context);
  43. }
  44. hash_map_t hash_map_new()
  45. {
  46. hash_map_t hash_map = _new(sizeof(struct _hash_map_t));
  47. if (hash_map)
  48. {
  49. hash_map->root = NULL;
  50. hash_map->size = 0;
  51. }
  52. return hash_map;
  53. }
  54. void hash_map_clear(hash_map_t hash_map)
  55. {
  56. _ASSERT(hash_map, ==, NULL, NULL_POINTER,);
  57. if (hash_map->root)
  58. {
  59. red_black_tree_inorder(hash_map->root, _hash_map_node_delete, NULL);
  60. red_black_tree_delete(hash_map->root);
  61. }
  62. hash_map->root = NULL;
  63. hash_map->size = 0;
  64. }
  65. void hash_map_delete(hash_map_t hash_map)
  66. {
  67. _ASSERT(hash_map, ==, NULL, NULL_POINTER,);
  68. hash_map_clear(hash_map);
  69. _delete(hash_map);
  70. }
  71. static red_black_tree_t _hash_map_do_search(hash_map_t hash_map, void *key,
  72. int compare_keys(void *x, void *y, void *context), void *context)
  73. {
  74. pair_t _key, _context;
  75. red_black_tree_t result;
  76. _ASSERT(hash_map, ==, NULL, NULL_POINTER, NULL);
  77. _ASSERT(compare_keys, ==, NULL, NULL_POINTER, NULL);
  78. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT, NULL);
  79. _key = pair_new(key, NULL);
  80. _context = pair_new(compare_keys, context);
  81. _ASSERT(_key, ==, NULL, NULL_POINTER, NULL);
  82. _ASSERT(_context, ==, NULL, NULL_POINTER, NULL);
  83. result = red_black_tree_search(hash_map->root, _key,
  84. _hash_map_compare_keys, _context);
  85. pair_delete(_key);
  86. pair_delete(_context);
  87. return result;
  88. }
  89. int hash_map_contains_key(hash_map_t hash_map, void *key, int compare_keys(
  90. void *x, void *y, void *context), void *context)
  91. {
  92. return _hash_map_do_search(hash_map, key, compare_keys, context) != NULL;
  93. }
  94. void *hash_map_get_key_value(hash_map_t hash_map, void *key, int compare_keys(
  95. void *x, void *y, void *context), void *context)
  96. {
  97. return pair_get_value(red_black_tree_get_key(_hash_map_do_search(hash_map,
  98. key, compare_keys, context)));
  99. }
  100. static volatile int _result;
  101. static volatile void *_key, *_value;
  102. static int (*_compare)(void *x, void *y, void *context);
  103. static void _hash_map_search_value(void *key, void *context)
  104. {
  105. if (_result)
  106. return;
  107. if (!_compare((void *) _value, pair_get_value(key), context))
  108. _result = 1;
  109. }
  110. int hash_map_contains_value(hash_map_t hash_map, void *value,
  111. int compare_values(void *x, void *y, void *context), void *context)
  112. {
  113. int result;
  114. _ASSERT(hash_map, ==, NULL, NULL_POINTER, -1);
  115. _ASSERT(compare_values, ==, NULL, NULL_POINTER, -1);
  116. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT, -1);
  117. _result = 0;
  118. _value = value;
  119. _compare = compare_values;
  120. red_black_tree_inorder(hash_map->root, _hash_map_search_value, context);
  121. _value = NULL;
  122. _compare = NULL;
  123. result = _result;
  124. _result = 0;
  125. return result;
  126. }
  127. int hash_map_is_empty(hash_map_t hash_map)
  128. {
  129. _ASSERT(hash_map, ==, NULL, NULL_POINTER, -1);
  130. return hash_map->size == 0;
  131. }
  132. int hash_map_get_size(hash_map_t hash_map)
  133. {
  134. _ASSERT(hash_map, ==, NULL, NULL_POINTER, -1);
  135. return hash_map->size;
  136. }
  137. void *hash_map_put(hash_map_t hash_map, void *key, void *value,
  138. int compare_keys(void *x, void *y, void *context), void *context)
  139. {
  140. pair_t _key, _context, _pair;
  141. void *old_value;
  142. red_black_tree_t node;
  143. _ASSERT(hash_map, ==, NULL, NULL_POINTER, NULL);
  144. if (hash_map->root == NULL)
  145. {
  146. hash_map->root = red_black_tree_new(pair_new(key, value));
  147. hash_map->size++;
  148. return NULL;
  149. }
  150. _ASSERT(compare_keys, ==, NULL, NULL_POINTER, NULL);
  151. _key = pair_new(key, value);
  152. _context = pair_new(compare_keys, context);
  153. _ASSERT(_key, ==, NULL, NULL_POINTER, NULL);
  154. _ASSERT(_context, ==, NULL, NULL_POINTER, NULL);
  155. node = red_black_tree_search(hash_map->root, _key, _hash_map_compare_keys,
  156. _context);
  157. if (node == NULL)
  158. {
  159. _pair = _key;
  160. red_black_tree_insert(&hash_map->root, _pair, _hash_map_compare_keys,
  161. _context, 1);
  162. hash_map->size++;
  163. pair_delete(_context);
  164. return NULL;
  165. }
  166. else
  167. {
  168. pair_delete(_context);
  169. pair_delete(_key);
  170. _pair = (pair_t) red_black_tree_get_key(node);
  171. if (_pair)
  172. {
  173. old_value = pair_get_value(_pair);
  174. pair_set_value(_pair, value);
  175. return old_value;
  176. }
  177. }
  178. return NULL;
  179. }
  180. int hash_map_remove_key(hash_map_t hash_map, void *key, int compare_keys(
  181. void *x, void *y, void *context), void *context)
  182. {
  183. pair_t _key, _context, _aux;
  184. red_black_tree_t result;
  185. int removed;
  186. _ASSERT(hash_map, ==, NULL, NULL_POINTER, -1);
  187. _ASSERT(compare_keys, ==, NULL, NULL_POINTER, -1);
  188. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT, -1);
  189. if (hash_map->size == 1)
  190. {
  191. if (!compare_keys(pair_get_key(red_black_tree_get_key(hash_map->root)),
  192. key, context))
  193. {
  194. pair_delete(red_black_tree_get_key(hash_map->root));
  195. red_black_tree_delete_node(hash_map->root);
  196. hash_map->root = NULL;
  197. hash_map->size = 0;
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. _key = pair_new(key, NULL);
  203. _context = pair_new(compare_keys, context);
  204. // Remove the node containing key
  205. result = red_black_tree_search(hash_map->root, _key,
  206. _hash_map_compare_keys, _context);
  207. if (result)
  208. {
  209. _aux = red_black_tree_get_key(result);
  210. removed = red_black_tree_remove(&hash_map->root, _key,
  211. _hash_map_compare_keys, _context, 1);
  212. if (removed > 0)
  213. {
  214. hash_map->size--;
  215. pair_delete(_aux);
  216. }
  217. }
  218. pair_delete(_key);
  219. pair_delete(_context);
  220. return result != NULL;
  221. }
  222. static void _hash_map_remove_value(void *key, void *context)
  223. {
  224. if (_result)
  225. return;
  226. if (!_compare((void *) _value, pair_get_value(key), context))
  227. {
  228. _key = pair_get_key(key);
  229. _result = 1;
  230. }
  231. }
  232. int hash_map_remove_value(hash_map_t hash_map, void *value, int compare_values(
  233. void *x, void *y, void *context), void *context)
  234. {
  235. int result;
  236. _ASSERT(hash_map, ==, NULL, NULL_POINTER, -1);
  237. _ASSERT(compare_values, ==, NULL, NULL_POINTER, -1);
  238. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT, -1);
  239. _result = 0;
  240. _key = NULL;
  241. _value = value;
  242. _compare = compare_values;
  243. red_black_tree_inorder(hash_map->root, _hash_map_remove_value, context);
  244. if (_result && _key)
  245. hash_map_remove_key(hash_map, (void *) _key, compare_values, context);
  246. _key = NULL;
  247. _value = NULL;
  248. _compare = NULL;
  249. result = _result;
  250. _result = 0;
  251. return result;
  252. }
  253. void *hash_map_get_internal_representation(hash_map_t hash_map)
  254. {
  255. _ASSERT(hash_map, ==, NULL, NULL_POINTER, NULL);
  256. return hash_map->root;
  257. }
  258. static void _hash_map_iterate_keys_handler(void *key, void *context)
  259. {
  260. pair_t context_pair = (pair_t) context;
  261. int (*user_key_handler)(void *key, void *context) = pair_get_key(
  262. context_pair);
  263. void *user_context = pair_get_value(context_pair);
  264. user_key_handler(pair_get_key(key), user_context);
  265. }
  266. void hash_map_iterate_keys(hash_map_t hash_map, void handler(void *key,
  267. void *context), void *context)
  268. {
  269. pair_t context_pair;
  270. _ASSERT(hash_map, ==, NULL, NULL_POINTER,);
  271. _ASSERT(handler, ==, NULL, NULL_POINTER,);
  272. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT,);
  273. context_pair = pair_new(handler, context);
  274. red_black_tree_inorder(hash_map->root, _hash_map_iterate_keys_handler,
  275. context_pair);
  276. pair_delete(context_pair);
  277. }
  278. static void _hash_map_iterate_values_handler(void *key, void *context)
  279. {
  280. pair_t context_pair = (pair_t) context;
  281. int (*user_key_handler)(void *key, void *context) = pair_get_key(
  282. context_pair);
  283. void *user_context = pair_get_value(context_pair);
  284. user_key_handler(pair_get_value(key), user_context);
  285. }
  286. void hash_map_iterate_values(hash_map_t hash_map, void handler(void *value,
  287. void *context), void *context)
  288. {
  289. pair_t context_pair;
  290. _ASSERT(hash_map, ==, NULL, NULL_POINTER,);
  291. _ASSERT(handler, ==, NULL, NULL_POINTER,);
  292. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT,);
  293. context_pair = pair_new(handler, context);
  294. red_black_tree_inorder(hash_map->root, _hash_map_iterate_values_handler,
  295. context_pair);
  296. pair_delete(context_pair);
  297. }
  298. static void _hash_map_iterate_handler(void *key, void *context)
  299. {
  300. pair_t context_pair = (pair_t) context;
  301. int (*user_key_handler)(void *key, void *value, void *context) =
  302. pair_get_key(context_pair);
  303. void *user_context = pair_get_value(context_pair);
  304. user_key_handler(pair_get_key(key), pair_get_value(key), user_context);
  305. }
  306. void hash_map_iterate(hash_map_t hash_map, void handler(void *key, void *value,
  307. void *context), void *context)
  308. {
  309. pair_t context_pair;
  310. _ASSERT(hash_map, ==, NULL, NULL_POINTER,);
  311. _ASSERT(handler, ==, NULL, NULL_POINTER,);
  312. _ASSERT(hash_map->size, <=, 0, INVALID_INPUT,);
  313. context_pair = pair_new(handler, context);
  314. red_black_tree_inorder(hash_map->root, _hash_map_iterate_handler,
  315. context_pair);
  316. pair_delete(context_pair);
  317. }