docs/Writerside/topics/hash_map.h.md

Sun, 14 Dec 2025 17:30:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Dec 2025 17:30:17 +0100
changeset 1605
55b13f583356
parent 1424
563033aa998c
permissions
-rw-r--r--

refactor the list and map construction functions and remove the simple macros

relates to #780
relates to #622

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
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
3 UCX provides a basic hash map implementation with a configurable number of buckets.
1141
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.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1228
diff changeset
5
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 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
7 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
8 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
9 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
10
1228
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
11 ```C
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
12 #include <cx/hash_map.h>
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
13
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
14 CxMap *cxHashMapCreate(const CxAllocator *allocator,
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
15 size_t itemsize, size_t buckets);
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
16
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
17 int cxMapRehash(CxMap *map);
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
18 ```
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 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
21 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
22
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
23 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
24 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
25 pointers instead of actual items.
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
27 If you pass zero for the number of `buckets`,
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
28 the map is initialized with a default of 16 buckets; otherwise the specified number of buckets is allocated.
1228
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
29
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
30 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
31 if the number of elements exceeds ¾ of the number of buckets.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
32 Otherwise, no action is performed, and this function simply returns 0.
1228
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
33 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
34
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
35 > 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
36 > 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
37 > `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
38
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
39 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
40 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
41 <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
42 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
43 </seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
44

mercurial