docs/Writerside/topics/hash_map.h.md

Mon, 24 Feb 2025 20:39:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 24 Feb 2025 20:39:29 +0100
changeset 1228
c231f6a259b5
parent 1190
a7b913d5d589
permissions
-rw-r--r--

add documentation for hash_map.h

relates to #451

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

mercurial