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