1 # Hash Map |
1 # Hash Map |
2 |
|
3 <warning> |
|
4 Outdated Section - will be updated soon! |
|
5 </warning> |
|
6 |
2 |
7 UCX provides a basic hash map implementation with a configurable amount of buckets. |
3 UCX provides a basic hash map implementation with a configurable amount of buckets. |
8 If you do not specify the number of buckets, a default of 16 buckets will be used. |
4 If you do not specify the number of buckets, a default of 16 buckets will be used. |
9 You can always rehash the map with `cxMapRehash()` to change the number of buckets to something more efficient, |
5 You can always rehash the map with `cxMapRehash()` to change the number of buckets to something more efficient, |
10 but you need to be careful, because when you use this function you are effectively locking into using this |
6 but you need to be careful, because when you use this function you are effectively locking into using this |
11 specific hash map implementation, and you would need to remove all calls to this function when you want to |
7 specific hash map implementation, and you would need to remove all calls to this function when you want to |
12 exchange the concrete map implementation with something different. |
8 exchange the concrete map implementation with something different. |
13 |
9 |
14 <!-- |
10 ```C |
15 ## Undocumented Symbols (TODO) |
11 #include <cx/hash_map.h> |
16 ### cxHashMapCreate |
12 |
17 ### cxMapRehash |
13 CxMap *cxHashMapCreate(const CxAllocator *allocator, |
18 --> |
14 size_t itemsize, size_t buckets); |
|
15 |
|
16 CxMap *cxHashMapCreateSimple(size_t itemsize); |
|
17 |
|
18 int cxMapRehash(CxMap *map); |
|
19 ``` |
|
20 |
|
21 The function `cxHashMapCreate()` creates a new [map](map.h.md) where both the map structure |
|
22 and the contained buckets are allocated by the specified `allocator`. |
|
23 The default stdlib allocator is used in `cxHashMapCreateSimple()`. |
|
24 |
|
25 The map will store items of size `itemsize`. |
|
26 You can use the `CX_STORE_POINTERS` macro for `itemsize` to indicate that the map shall store |
|
27 pointers instead of actual items. |
|
28 |
|
29 If you pass zero for the number of `buckets`, or use `cxHashMapSimple()`, |
|
30 the map is initialized with a default of 16 buckets, otherwise the specified number of buckets is allocated. |
|
31 |
|
32 The function `cxMapRehash()` allocates a new array of buckets and re-distributes all elements, |
|
33 if the number of elements exceeds ¾ of the number of buckets. |
|
34 Otherwise, no action is performed and this function simply returns 0. |
|
35 After rehashing, the number of buckets is at least 2½ times the number of elements. |
|
36 |
|
37 > Advice if you want to create your own hash map structures: |
|
38 > Calling `cxMapRehash()` on a map is only defined, when the map is based on the |
|
39 > `struct cx_hash_map_s` structure. |
19 |
40 |
20 <seealso> |
41 <seealso> |
21 <category ref="apidoc"> |
42 <category ref="apidoc"> |
22 <a href="https://ucx.sourceforge.io/api/hash__map_8h.html">hash_map.h</a> |
43 <a href="https://ucx.sourceforge.io/api/hash__map_8h.html">hash_map.h</a> |
23 </category> |
44 </category> |