hash_map.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #include "bt_common.h"
  19. #include "osi/list.h"
  20. #include "osi/hash_map.h"
  21. #include "osi/allocator.h"
  22. struct hash_map_t;
  23. typedef struct hash_map_bucket_t {
  24. list_t *list;
  25. } hash_map_bucket_t;
  26. typedef struct hash_map_t {
  27. hash_map_bucket_t *bucket;
  28. size_t num_bucket;
  29. size_t hash_size;
  30. hash_index_fn hash_fn;
  31. key_free_fn key_fn;
  32. data_free_fn data_fn;
  33. key_equality_fn keys_are_equal;
  34. } hash_map_t;
  35. // Hidden constructor for list, only to be used by us.
  36. list_t *list_new_internal(list_free_cb callback);
  37. static void bucket_free_(void *data);
  38. static bool default_key_equality(const void *x, const void *y);
  39. static hash_map_entry_t *find_bucket_entry_(list_t *hash_bucket_list,
  40. const void *key);
  41. // Hidden constructor, only to be used by the allocation tracker. Behaves the same as
  42. // |hash_map_new|, except you get to specify the allocator.
  43. hash_map_t *hash_map_new_internal(
  44. size_t num_bucket,
  45. hash_index_fn hash_fn,
  46. key_free_fn key_fn,
  47. data_free_fn data_fn,
  48. key_equality_fn equality_fn)
  49. {
  50. assert(hash_fn != NULL);
  51. assert(num_bucket > 0);
  52. hash_map_t *hash_map = osi_calloc(sizeof(hash_map_t));
  53. if (hash_map == NULL) {
  54. return NULL;
  55. }
  56. hash_map->hash_fn = hash_fn;
  57. hash_map->key_fn = key_fn;
  58. hash_map->data_fn = data_fn;
  59. hash_map->keys_are_equal = equality_fn ? equality_fn : default_key_equality;
  60. hash_map->num_bucket = num_bucket;
  61. hash_map->bucket = osi_calloc(sizeof(hash_map_bucket_t) * num_bucket);
  62. if (hash_map->bucket == NULL) {
  63. osi_free(hash_map);
  64. return NULL;
  65. }
  66. return hash_map;
  67. }
  68. hash_map_t *hash_map_new(
  69. size_t num_bucket,
  70. hash_index_fn hash_fn,
  71. key_free_fn key_fn,
  72. data_free_fn data_fn,
  73. key_equality_fn equality_fn)
  74. {
  75. return hash_map_new_internal(num_bucket, hash_fn, key_fn, data_fn, equality_fn);
  76. }
  77. void hash_map_free(hash_map_t *hash_map)
  78. {
  79. if (hash_map == NULL) {
  80. return;
  81. }
  82. hash_map_clear(hash_map);
  83. osi_free(hash_map->bucket);
  84. osi_free(hash_map);
  85. }
  86. /*
  87. bool hash_map_is_empty(const hash_map_t *hash_map) {
  88. assert(hash_map != NULL);
  89. return (hash_map->hash_size == 0);
  90. }
  91. size_t hash_map_size(const hash_map_t *hash_map) {
  92. assert(hash_map != NULL);
  93. return hash_map->hash_size;
  94. }
  95. size_t hash_map_num_buckets(const hash_map_t *hash_map) {
  96. assert(hash_map != NULL);
  97. return hash_map->num_bucket;
  98. }
  99. */
  100. bool hash_map_has_key(const hash_map_t *hash_map, const void *key)
  101. {
  102. assert(hash_map != NULL);
  103. hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
  104. list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
  105. hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
  106. return (hash_map_entry != NULL);
  107. }
  108. bool hash_map_set(hash_map_t *hash_map, const void *key, void *data)
  109. {
  110. assert(hash_map != NULL);
  111. assert(data != NULL);
  112. hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
  113. if (hash_map->bucket[hash_key].list == NULL) {
  114. hash_map->bucket[hash_key].list = list_new_internal(bucket_free_);
  115. if (hash_map->bucket[hash_key].list == NULL) {
  116. return false;
  117. }
  118. }
  119. list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
  120. hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
  121. if (hash_map_entry) {
  122. // Calls hash_map callback to delete the hash_map_entry.
  123. bool rc = list_remove(hash_bucket_list, hash_map_entry);
  124. assert(rc == true);
  125. } else {
  126. hash_map->hash_size++;
  127. }
  128. hash_map_entry = osi_calloc(sizeof(hash_map_entry_t));
  129. if (hash_map_entry == NULL) {
  130. return false;
  131. }
  132. hash_map_entry->key = key;
  133. hash_map_entry->data = data;
  134. hash_map_entry->hash_map = hash_map;
  135. return list_append(hash_bucket_list, hash_map_entry);
  136. }
  137. bool hash_map_erase(hash_map_t *hash_map, const void *key)
  138. {
  139. assert(hash_map != NULL);
  140. hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
  141. list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
  142. hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
  143. if (hash_map_entry == NULL) {
  144. return false;
  145. }
  146. hash_map->hash_size--;
  147. bool remove = list_remove(hash_bucket_list, hash_map_entry);
  148. if(list_is_empty(hash_map->bucket[hash_key].list)) {
  149. list_free(hash_map->bucket[hash_key].list);
  150. hash_map->bucket[hash_key].list = NULL;
  151. }
  152. return remove;
  153. }
  154. void *hash_map_get(const hash_map_t *hash_map, const void *key)
  155. {
  156. assert(hash_map != NULL);
  157. hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
  158. list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
  159. hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
  160. if (hash_map_entry != NULL) {
  161. return hash_map_entry->data;
  162. }
  163. return NULL;
  164. }
  165. void hash_map_clear(hash_map_t *hash_map)
  166. {
  167. assert(hash_map != NULL);
  168. for (hash_index_t i = 0; i < hash_map->num_bucket; i++) {
  169. if (hash_map->bucket[i].list == NULL) {
  170. continue;
  171. }
  172. list_free(hash_map->bucket[i].list);
  173. hash_map->bucket[i].list = NULL;
  174. }
  175. }
  176. void hash_map_foreach(hash_map_t *hash_map, hash_map_iter_cb callback, void *context)
  177. {
  178. assert(hash_map != NULL);
  179. assert(callback != NULL);
  180. for (hash_index_t i = 0; i < hash_map->num_bucket; ++i) {
  181. if (hash_map->bucket[i].list == NULL) {
  182. continue;
  183. }
  184. for (const list_node_t *iter = list_begin(hash_map->bucket[i].list);
  185. iter != list_end(hash_map->bucket[i].list);
  186. iter = list_next(iter)) {
  187. hash_map_entry_t *hash_map_entry = (hash_map_entry_t *)list_node(iter);
  188. if (!callback(hash_map_entry, context)) {
  189. return;
  190. }
  191. }
  192. }
  193. }
  194. static void bucket_free_(void *data)
  195. {
  196. assert(data != NULL);
  197. hash_map_entry_t *hash_map_entry = (hash_map_entry_t *)data;
  198. const hash_map_t *hash_map = hash_map_entry->hash_map;
  199. if (hash_map->key_fn) {
  200. hash_map->key_fn((void *)hash_map_entry->key);
  201. }
  202. if (hash_map->data_fn) {
  203. hash_map->data_fn(hash_map_entry->data);
  204. }
  205. osi_free(hash_map_entry);
  206. }
  207. static hash_map_entry_t *find_bucket_entry_(list_t *hash_bucket_list,
  208. const void *key)
  209. {
  210. if (hash_bucket_list == NULL) {
  211. return NULL;
  212. }
  213. for (const list_node_t *iter = list_begin(hash_bucket_list);
  214. iter != list_end(hash_bucket_list);
  215. iter = list_next(iter)) {
  216. hash_map_entry_t *hash_map_entry = (hash_map_entry_t *)list_node(iter);
  217. if (hash_map_entry->hash_map->keys_are_equal(hash_map_entry->key, key)) {
  218. return hash_map_entry;
  219. }
  220. }
  221. return NULL;
  222. }
  223. static bool default_key_equality(const void *x, const void *y)
  224. {
  225. return x == y;
  226. }