docs/Writerside/topics/hash_map.h.md

Fri, 23 May 2025 12:44:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 May 2025 12:44:24 +0200
changeset 1327
ed75dc1db503
parent 1318
12fa1d37fe48
permissions
-rw-r--r--

make test-compile depend on both static and shared

the shared lib is not needed for the tests,
but when run with coverage, gcov will be confused
when outdated line information is available from
a previous shared build

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.
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 CxMap *cxHashMapCreateSimple(size_t itemsize);
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 int cxMapRehash(CxMap *map);
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
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
22 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
23 and the contained buckets are allocated by the specified `allocator`.
1318
12fa1d37fe48 allow changing the cxDefaultAllocator - resolves #669
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
24 The [default allocator](allocator.h.md#default-allocator) is used in `cxHashMapCreateSimple()`.
1228
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
25
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26 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
27 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
28 pointers instead of actual items.
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 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
31 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
32
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
33 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
34 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
35 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
36 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
37
c231f6a259b5 add documentation for hash_map.h
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
38 > 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
39 > 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
40 > `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
41
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
42 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
43 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
44 <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
45 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
46 </seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
47

mercurial