UNIXworkcode

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