docs/Writerside/topics/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 1604
68b75c091028
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 # Map Interface
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 Similar to the list interface, the map interface provides a common API for implementing maps.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
4
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
5 UCX is shipped with a [hash map](hash_map.h.md) implementation of this interface.
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
7 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
8 #include <cx/hash_map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
9
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
10 CxMap *cxHashMapCreate(const CxAllocator *allocator,
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
11 size_t itemsize, size_t buckets);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
13
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
14 The function `cxHashMapCreate()` creates a new map where both the map structure
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
15 and the contained buckets are allocated by the specified `allocator`.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
16
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
17 The map will store items of size `itemsize`.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
18 You can use the `CX_STORE_POINTERS` macro for `itemsize` to indicate that the map shall store
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
19 pointers instead of actual items.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
20
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1604
diff changeset
21 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: 1390
diff changeset
22 the map is initialized with a default of 16 buckets; otherwise the specified number of buckets is allocated.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
23
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
24 > If you want to lazy-initialize maps, you can use the global `cxEmptyMap` symbol as a placeholder instead of using a `NULL`-pointer.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
25 > While you *must not* insert elements into that map, you can safely access this map or create iterators.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26 > This allows you to write clean code without checking for `NULL`-pointer everywhere.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
27 > You still need to make sure that the placeholder is replaced with an actual map before inserting elements.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
28
1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents: 1344
diff changeset
29 > All functions working with keys are implemented as generics, so that the following types are supported:
1250
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
30 > `CxHashKey`, `cxstring`, `cxmutstr`, `const char*`, and `char*`.
1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents: 1344
diff changeset
31 > When we write `KeyType`, we mean any of these types.
1250
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
32 > {style="note"}
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
33
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
34 ## Examples
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
35
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
36 ```C
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
37 #include <stdio.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
38 #include <cx/hash_map.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
39
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
40 int main() {
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1604
diff changeset
41 CxMap *contacts = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
42
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
43 // Build a small phone book
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
44 cxMapPut(contacts, "John", "123-0815");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
45 cxMapPut(contacts, "Jane", "987-4711");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
46 cxMapPut(contacts, "Michelle", "555-3141");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
47 cxMapPut(contacts, "Oliver", "000-9999");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
48
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
49 // Retrieve a phone number
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
50 const char *janes_phone = cxMapGet(contacts, "Jane");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
51 printf("The number of Jane is: %s\n", janes_phone);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
52
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
53 // Update number
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
54 cxMapPut(contacts, "Jane", "987-1337");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
55
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
56 // Remove and retrieve number
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
57 cxMapRemoveAndGet(contacts, "Jane", &janes_phone);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
58 printf("The number of Jane was: %s\n", janes_phone);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
59 janes_phone = cxMapGet(contacts, "Jane");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
60 if (janes_phone == NULL) printf("Jane's number was deleted.\n");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
61
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
62 // Iterate through the contact list
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
63 CxMapIterator iter = cxMapIterator(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
64 cx_foreach(CxMapEntry *, entry, iter) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
65 cxstring name = cx_strn(entry->key->data, entry->key->len);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
66 const char *phone = entry->value;
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
67 printf("%" CX_PRIstr ": %s\n", CX_SFMT(name), phone);
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
68 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
69
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
70 cxMapFree(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
71
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
72 return 0;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
73 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
74 ```
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
75
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
76 The above example illustrates basic operations with a map.
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
77
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
78 In the first part we add several entries to the map.
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
79 Then the example shows retrieval, updating, and removal of information.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
80 The last part shows how to iterate over the pairs inside the map and how to recover the string from the key.
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
81
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
82 In real-world situations, however, it is quite unlikely that you will use a map to store string literals.
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
83 The next example shows a more realistic program, where it is necessary to store strings based on user input.
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
84
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
85 ```C
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
86 #include <stdio.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
87 #include <string.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
88 #include <cx/hash_map.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
89
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
90 int main() {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
91 // store strings in the map...
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1604
diff changeset
92 CxMap *contacts = cxHashMapCreate(NULL, sizeof(cxmutstr), 0);
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
93 // ...which are automatically freed when removed
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1604
diff changeset
94 cxSetDestructor(contacts, cx_strfree);
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
95
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
96 // build a small interactive program
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
97 const unsigned buffer_size = 256;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
98 char input[buffer_size];
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
99 bool running = true;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
100
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
101 while (running) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
102 puts("\n*** Contacts - Menu ***");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
103 puts("1) Add entry");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
104 puts("2) Look up entry");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
105 puts("3) Remove entry");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
106 puts("4) Show all entries");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
107 puts("0) Exit");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
108 fputs("> ", stdout);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
110 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
111 if (input[1] != '\n') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
112 puts("Please select a menu item.\n");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
113 } else if (input[0] == '1') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
114 fputs("Name > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
115 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
116 // Copy the name (alternative: use 2nd input buf)
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
117 cxmutstr name = cx_strdup(
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
118 cx_strn(input, strcspn(input, "\n")));
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
119 fputs("Phone > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
120 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
121 // add the entry, note that only the contents
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
122 // of the cxmutstr are copied, not the string
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
123 // data - therefore we have to call cx_strdup
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
124 cxmutstr phone = cx_strdup(
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
125 cx_strn(input, strcspn(input, "\n")));
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
126 // note, that we can simply use the cxmutstr
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
127 // for the name as key, because cxMapPut uses
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
128 // generic selection
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
129 cxMapPut(contacts, name, &phone);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
130 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
131 cx_strfree(&name);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
132 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
133 } else if (input[0] == '2') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
134 fputs("Name > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
135 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
136 // Look up the name
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
137 input[strcspn(input, "\n")] = '\0';
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
138 cxmutstr *phone = cxMapGet(contacts, input);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
139 if (phone == NULL) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
140 puts("No record.");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
141 } else {
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
142 printf("Result: %" CX_PRIstr "\n",
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
143 CX_SFMT(*phone));
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
144 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
145 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
146 } else if (input[0] == '3') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
147 fputs("Name > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
148 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
149 // Remove the entry
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
150 // Since we registered cx_strfree as destructor,
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
151 // the memory is automatically freed
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
152 input[strcspn(input, "\n")] = '\0';
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
153 if (cxMapRemove(contacts, input)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
154 puts("No such record.");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
155 } else {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
156 puts("Removed.");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
157 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
158 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
159 } else if (input[0] == '4') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
160 // Almost the same iteration loop as above ...
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
161 CxMapIterator iter = cxMapIterator(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
162 cx_foreach(CxMapEntry *, entry, iter) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
163 cxstring name = cx_strn(entry->key->data,
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
164 entry->key->len);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
165 // ... except that here we get cxmutstr*
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
166 cxmutstr *phone = entry->value;
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
167 printf("%" CX_PRIstr ": %" CX_PRIstr "\n",
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
168 CX_SFMT(name), CX_SFMT(*phone));
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
169 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
170 } else if (input[0] == '0') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
171 running = false;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
172 } else {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
173 puts("Please select a menu item.\n");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
174 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
175 } else {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
176 running = false;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
177 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
178 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
179
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
180 // this will free all remaining strings that are in the map
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
181 cxMapFree(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
182
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
183 return 0;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
184 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
185 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
186
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
187 ## Insert
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
188
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
189 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
190 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
191
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
192 int cxMapPut(CxMap *map, KeyType key, void *value);
1341
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
193
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
194 void *cxMapEmplace(CxMap *map, KeyType key);
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
195 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
196
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
197 The function `cxMapPut()` stores the specified `key` / `value` pair
1341
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
198 and returns zero if the element was successfully put into the map and non-zero otherwise.
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
199
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
200 The key is always copied.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
201 The behavior for the value is dependent on whether the map is storing pointers.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
202 If it is storing pointers, the `value` pointer is directly stored in the map.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
203 Otherwise, the memory pointed to by `value` is copied, using the element size of the collection.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
204
1341
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
205 The function `cxMapEmplace()` is similar to `cxMapPut()`, but instead of adding an existing value to the map,
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
206 it returns a pointer to newly allocated memory for the value.
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
207 The memory is allocated using the allocator of the map.
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
208 If the map is storing pointers, the allocated memory will only be that for a pointer.
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
209 Otherwise, the memory is allocated using the known element size.
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
210
dc88d2ece7e4 add cxMapEmplace()
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
211 Both functions, if an element is already associated with the specified key, replace the existing element.
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
212 If [destructor functions](collection.h.md#destructor-functions) are registered,
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
213 they are invoked for the old element before it is replaced.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
214
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
215 ## Access
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
216
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
217 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
219
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220 void *cxMapGet(CxMap *map, KeyType key);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
221
1445
e8089a590b71 add implementations for map difference
Mike Becker <universe@uap-core.de>
parents: 1444
diff changeset
222 bool cxMapContains(CxMap *map, KeyType key);
e8089a590b71 add implementations for map difference
Mike Becker <universe@uap-core.de>
parents: 1444
diff changeset
223
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224 size_t cxMapSize(const CxMap *map);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
227 With the function `cxMapGet()` you can retrieve a value stored under the specified `key`.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
228 If there is no such value, this function returns `NULL`.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
229
1445
e8089a590b71 add implementations for map difference
Mike Becker <universe@uap-core.de>
parents: 1444
diff changeset
230 The function `cxMapContains()` returns `true` if the map contains an element with the specified `key`,
e8089a590b71 add implementations for map difference
Mike Becker <universe@uap-core.de>
parents: 1444
diff changeset
231 and `false` otherwise.
e8089a590b71 add implementations for map difference
Mike Becker <universe@uap-core.de>
parents: 1444
diff changeset
232
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
233 The function `cxMapSize()` returns how many key/value-pairs are currently stored in the map.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
234
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
235 ## Remove
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
236
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
237 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
238 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
239
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
240 int cxMapRemove(CxMap *map, KeyType key);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
241
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
242 int cxMapRemoveAndGet(CxMap *map, KeyType key, void* targetbuf);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
244 void cxMapClear(CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
245 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
246
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
247 The function `cxMapRemove()` retrieves and removes a value stored under the specified `key`.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
248 If [destructor functions](collection.h.md#destructor-functions) are registered, they are called before removal.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
249
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
250 On the other hand, `cxMapRemoveAndGet()` does not invoke the destructor functions
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
251 and instead copies the value into the `targetbuf`, which must be large enough to hold the value.
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
252
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
253 In either case, the functions return zero when an element was found under the specified key, and non-zero otherwise.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
254
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
255 The function `cxMapClear()` removes all elements from the map, invoking the destructor functions for each of them.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
256
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
257 ## Iterators
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
258
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
259 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
260 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
261
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
262 CxMapIterator cxMapIterator(const CxMap *map);
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
263
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
264 CxMapIterator cxMapIteratorKeys(const CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
265
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
266 CxMapIterator cxMapIteratorValues(const CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
267 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
268
1429
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
269 The above functions create iterators over the
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
270 pairs, keys, or values of the specified `map`, respectively.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
271
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
272 Iterators over pairs yield elements of the type `CxMapEntry*` which is a struct containing a pointer to the `key` and the value, respectively.
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
273
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
274 Iterators over keys yield elements of the type `const CxHashKey*` (cf. [CxHashKey documentation](hash_key.h.md)).
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
275
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
276 The behavior of iterators over values depends on the concrete implementation.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
277 Implementations are encouraged to support `CX_STORE_POINTERS`.
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
278 If used, the `void*` elements the iterator yields shall be directly the stored pointers.
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
279 Otherwise, the iterator shall yield pointers to the map's memory where the value is stored.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
280
1344
8afaeb395b3c add support for NULL in map iterators
Mike Becker <universe@uap-core.de>
parents: 1342
diff changeset
281 It is always safe to call the above functions on a `NULL`-pointer.
8afaeb395b3c add support for NULL in map iterators
Mike Becker <universe@uap-core.de>
parents: 1342
diff changeset
282 In that case, the returned iterator will behave like an iterator over an empty map.
8afaeb395b3c add support for NULL in map iterators
Mike Becker <universe@uap-core.de>
parents: 1342
diff changeset
283
1579
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
284 ## Compare
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
285
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
286 ```C
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
287 #include <cx/map.h>
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
288
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
289 int cxMapCompare(const CxMap *map, const CxMap *other);
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
290 ```
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
291
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
292 Arbitrary maps can be compared with `cxMapCompare()`.
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
293 That means, for example, you can compare a [hash map](hash_map.h.md) with the map aspect of a [key/value list](kv_list.h.md).
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
294
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
295 The return value of `cxMapCompare()` is zero if the lists contain the same keys and their values are pairwise equivalent, according to the map's compare function.
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
296 If the `map` contains fewer keys than the `other` map, the function returns a negative value, and if it contains more values, it returns a positive value, respectively.
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
297 When both maps contain the same number of keys but differ in either a key or a value, the return value is non-zero, but otherwise unspecified.
0393c67556ec add cxMapCompare() - resolves #784
Mike Becker <universe@uap-core.de>
parents: 1489
diff changeset
298
1440
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
299 ## Clone
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
300
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
301 ```C
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
302 #include <cx/allocator.h>
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
303
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
304 typedef void*(cx_clone_func)(
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
305 void *target, const void *source,
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
306 const CxAllocator *allocator,
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
307 void *data);
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
308
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
309 #include <cx/map.h>
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
310
1444
dd9dcbb39c2f make clone functions return int instead of size_t
Mike Becker <universe@uap-core.de>
parents: 1440
diff changeset
311 int cxMapClone(CxMap *dst, const CxMap *src,
1440
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
312 cx_clone_func clone_func,
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
313 const CxAllocator *clone_allocator,
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
314 void *data);
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
315
1604
68b75c091028 rename the "simple" cloning functions from Simple to Shallow
Mike Becker <universe@uap-core.de>
parents: 1587
diff changeset
316 int cxMapCloneShallow(CxMap *dst, const CxMap *src);
1447
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
317
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
318 int cxMapDifference(CxMap *dst,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
319 const CxMap *minuend, const CxMap *subtrahend,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
320 cx_clone_func clone_func,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
321 const CxAllocator *clone_allocator,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
322 void *data);
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
323
1604
68b75c091028 rename the "simple" cloning functions from Simple to Shallow
Mike Becker <universe@uap-core.de>
parents: 1587
diff changeset
324 int cxMapDifferenceShallow(CxMap *dst,
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
325 const CxMap *minuend, const CxMap *subtrahend);
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
326
1447
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
327 int cxMapListDifference(CxMap *dst,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
328 const CxMap *src, const CxList *keys,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
329 cx_clone_func clone_func,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
330 const CxAllocator *clone_allocator,
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
331 void *data);
1465
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
332
1604
68b75c091028 rename the "simple" cloning functions from Simple to Shallow
Mike Becker <universe@uap-core.de>
parents: 1587
diff changeset
333 int cxMapListDifferenceShallow(CxMap *dst,
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
334 const CxMap *src, const CxList *keys);
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
335
1465
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
336 int cxMapIntersection(CxMap *dst,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
337 const CxMap *src, const CxMap *other,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
338 cx_clone_func clone_func,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
339 const CxAllocator *clone_allocator,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
340 void *data);
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
341
1604
68b75c091028 rename the "simple" cloning functions from Simple to Shallow
Mike Becker <universe@uap-core.de>
parents: 1587
diff changeset
342 int cxMapIntersectionShallow(CxMap *dst,
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
343 const CxMap *src, const CxMap *other);
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
344
1465
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
345 int cxMapListIntersection(CxMap *dst,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
346 const CxMap *src, const CxList *keys,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
347 cx_clone_func clone_func,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
348 const CxAllocator *clone_allocator,
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
349 void *data);
1474
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
350
1604
68b75c091028 rename the "simple" cloning functions from Simple to Shallow
Mike Becker <universe@uap-core.de>
parents: 1587
diff changeset
351 int cxMapListIntersectionShallow(CxMap *dst,
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
352 const CxMap *src, const CxList *keys);
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
353
1474
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
354 int cxMapUnion(CxMap *dst, const CxMap *src,
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
355 cx_clone_func clone_func,
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
356 const CxAllocator *clone_allocator,
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
357 void *data);
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
358
1604
68b75c091028 rename the "simple" cloning functions from Simple to Shallow
Mike Becker <universe@uap-core.de>
parents: 1587
diff changeset
359 int cxMapUnionShallow(CxMap *dst, const CxMap *src);
1440
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
360 ```
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
361
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
362 With `cxMapClone()` you can create deep copies of the values in one map and insert them into another map.
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
363 The destination map does not need to be empty.
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
364 But when a key already exists in the destination map, the value is overwritten with the clone from the source map.
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
365 If the destination map has destructors defined, they are called for the replaced element.
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
366
1447
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
367 The functions `cxMapDifference()` and `cxMapListDifference()` are similar to `cxMapClone()`
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
368 except that they only clone an element from the source map, when the key is _not_ contained in the
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
369 other map (or list, respectively).
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
370 This is equivalent to computing the set difference for the set of keys.
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
371 Likewise, `cxMapIntersection()` and `cxMapListIntersection()` only clone an element from the source map
1465
dc886f1a6155 specify the intersection functions
Mike Becker <universe@uap-core.de>
parents: 1447
diff changeset
372 when the key is contained in _both_ collections.
1474
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
373 The function `cxMapUnion()` only clones elements from `src` to `dst` when their key is not present in `dst`.
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
374
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
375 > The function `cxMapUnion()` does not operate on three maps for the sake of simplicity.
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
376 > If you want to combine two maps without modifying either of them, you can first use `cxMapClone()`
84de0ba791af implement cxMapUnion() - resolves #756
Mike Becker <universe@uap-core.de>
parents: 1465
diff changeset
377 > to clone the first map into a new, empty, map, and then use `cxMapUnion()`.
1447
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
378
1440
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
379 Refer to the documentation of the [clone-function callback](allocator.h.md#clone-function) to learn how to implement it.
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
380
1479
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
381 The _simple_ versions of the above functions use an internal shallow clone function
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
382 which uses `memcpy()` to copy the values.
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
383 If the destination map is storing pointers, this internal clone function uses the current default allocator to allocate the memory.
ac1baaed2fd7 implement simple versions of the clone functions
Mike Becker <universe@uap-core.de>
parents: 1478
diff changeset
384
1447
aaf85b3e9601 add documentation for cxMapDifference() and cxMapListDifference()
Mike Becker <universe@uap-core.de>
parents: 1445
diff changeset
385 The functions return zero if and only if all clone operations were successful.
1440
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
386
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
387 > It is perfectly possible to clone items into a map of a different type.
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
388 > For example, you can clone entries from a map that is just storing pointers (`CX_STORE_POINTERS`) to a map that
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
389 > allocates the memory for the objects (and vice versa).
0d1430668271 add documentation for cxMapClone() - resolves #743
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
390
1478
bba2e5efdca0 add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents: 1474
diff changeset
391 > The maps passed to the above functions must all be different.
bba2e5efdca0 add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents: 1474
diff changeset
392 > Passing the same pointer for different map arguments may result in erroneous behavior.
bba2e5efdca0 add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents: 1474
diff changeset
393 >{style="warning"}
bba2e5efdca0 add warning, not to pass the same pointer multiple times to the clone functions
Mike Becker <universe@uap-core.de>
parents: 1474
diff changeset
394
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
395 ## Dispose
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
396
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
397 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
398 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
399
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
400 void cxMapFree(CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
401 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
402
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
403 The function `cxMapFree()` invokes the [destructor functions](collection.h.md#destructor-functions) for all elements
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
404 and then deallocates the entire memory for the map.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
405
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
406 ## Implement own Map Structures
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
407
1274
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
408 If the UCX [hash map](hash_map.h.md) implementation does not suit your needs,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
409 you can also implement your own map.
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
410 To be compatible with the `CxMap` interface, it needs to store key/value pairs of the following form.
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
411
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
412 ```C
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
413 typedef struct {
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
414 const CxHashKey *key;
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
415 void *value;
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
416 } CxMapEntry;
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
417 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
418
1489
185dc2a4b45c fix various typos in the docs
Mike Becker <universe@uap-core.de>
parents: 1479
diff changeset
419 When you are declaring your map structure, a `CxMap` member must be embedded as the first member of this structure.
1274
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
420 Secondly, you need to implement the `cx_map_class` and assign it when allocating your map.
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
421
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
422 ```C
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
423 #include <cx/map.h>
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
424
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
425 typedef struct {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
426 CxMap base;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
427 // ... data members for storing CxMapEntry elements go here ...
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
428 } MyMap;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
429
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
430 // declare the class - implement the functions somewhere
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
431 static cx_map_class my_map_class = {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
432 my_map_destructor,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
433 my_map_clear,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
434 my_map_put,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
435 my_map_get,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
436 my_map_remove,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
437 my_map_iterator,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
438 };
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
439
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
440 // this function will create the map
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
441 CxMap *myMapCreate(const CxAllocator *allocator, size_t itemsize) {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
442 if (allocator == NULL) {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
443 allocator = cxDefaultAllocator;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
444 }
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
445
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
446 // allocate memory
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
447 MyMap *map = cxCalloc(allocator, 1, sizeof(MyMap));
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
448 if (map == NULL) return NULL;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
449
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
450 // initialize base members
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
451 map->base.cl = &my_map_class; // <--- assign class here
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
452 map->base.collection.allocator = allocator;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
453 if (itemsize == CX_STORE_POINTERS) {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
454 map->base.collection.elem_size = sizeof(void *);
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
455 map->base.collection.store_pointer = true;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
456 } else {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
457 map->base.collection.elem_size = itemsize;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
458 }
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
459
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
460 // ... initialization of data members go here ...
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
461
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
462 return (CxMap *) map;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
463 }
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
464 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
465
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
466 The required behavior for the implementations is described in the following table.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
467 You can always look at the source code of the UCX hash map to get inspiration.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
468
1587
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
469 | Function | Description |
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
470 |--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
471 | `clear` | Invoke [destructor functions](collection.h.md#destructor-functions) on all elements and remove them from the map. |
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
472 | `deallocate` | Invoke destructor functions on all elements and deallocate the entire map memory. |
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
473 | `put` | Store an element in the map. If an element is already stored, invoke the destructor functions on that element and replace it with the new element. When the value pointer is `NULL`, only allocate memory without copying an existing value. Return `CxMapEntry` where the `key` is `NULL` when allocation fails. |
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
474 | `get` | Look up the specified key and return the associated value (or `NULL` if the key was not found). |
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
475 | `remove` | Remove an element from the map. If a target buffer is specified, copy the elements to that buffer. Otherwise, invoke the destructor functions for the element. If the key was not found in the map, return non-zero. |
7156d6699410 fix critical UAF because kv-list stored pointers to the wrong key data
Mike Becker <universe@uap-core.de>
parents: 1579
diff changeset
476 | `iterator` | Return an iterator over the pairs, the keys, or the values, depending on the iterator type passed with the last argument. |
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
477
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
478 > In contrast to the list interface, there is no `cx_map_init()` function which automatically
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
479 > configures a wrapping mechanism when `CX_STORE_POINTERS` is used.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
480 > That means, in the implementation of the above functions you must take care of distinguishing between
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
481 > maps that are storing pointers and that are storing elements yourself (if you want to support both).
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
482 >{style="note"}
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
483
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
484 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
485 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
486 <a href="https://ucx.sourceforge.io/api/map_8h.html">map.h</a>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
487 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
488 </seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
489

mercurial