docs/Writerside/topics/map.h.md

Thu, 17 Apr 2025 20:47:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Apr 2025 20:47:43 +0200
changeset 1298
0597f1f20ea9
parent 1274
03e9360e98f5
permissions
-rw-r--r--

use new string formatting macros in documentation

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);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
12
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
13 CxMap *cxHashMapCreateSimple(size_t itemsize);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
15
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
16 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
17 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
18 The default stdlib allocator is used in `cxHashMapCreateSimple()`.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
19
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
20 The map will store items of size `itemsize`.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
21 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
22 pointers instead of actual items.
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 pass zero for the number of `buckets`, or use `cxHashMapSimple()`,
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
25 the map is initialized with a default of 16 buckets, otherwise the specified number of buckets is allocated.
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
27 > 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
28 > 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
29 > 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
30 > 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
31
1250
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
32 > In the Sections below, the type for the key is denoted `KeyType`.
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
33 > All functions are implemented as generics, so that the following types are supported:
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
34 > `CxHashKey`, `cxstring`, `cxmutstr`, `const char*`, and `char*`.
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
35 > {style="note"}
80a1d63ede19 move the note about the key type to the top
Mike Becker <universe@uap-core.de>
parents: 1249
diff changeset
36
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
37 ## Examples
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
38
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
39 ```C
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
40 #include <stdio.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
41 #include <cx/hash_map.h>
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 int main() {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
44 CxMap *contacts = cxHashMapCreateSimple(CX_STORE_POINTERS);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
45
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
46 // Build a small phone book
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
47 cxMapPut(contacts, "John", "123-0815");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
48 cxMapPut(contacts, "Jane", "987-4711");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
49 cxMapPut(contacts, "Michelle", "555-3141");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
50 cxMapPut(contacts, "Oliver", "000-9999");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
51
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
52 // Retrieve a phone number
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
53 const char *janes_phone = cxMapGet(contacts, "Jane");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
54 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
55
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
56 // Update number
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
57 cxMapPut(contacts, "Jane", "987-1337");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
58
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
59 // Remove and retrieve number
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
60 cxMapRemoveAndGet(contacts, "Jane", &janes_phone);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
61 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
62 janes_phone = cxMapGet(contacts, "Jane");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
63 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
64
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
65 // Iterate through the contact list
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
66 CxMapIterator iter = cxMapIterator(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
67 cx_foreach(CxMapEntry *, entry, iter) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
68 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
69 const char *phone = entry->value;
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
70 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
71 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
72
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
73 cxMapFree(contacts);
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 return 0;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
76 }
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
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
79 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
80
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
81 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
82 Then the example shows retrieval, updating, and removal of information.
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
83 The last part shows how to iterate over the pairs of the map and how to recover the string from the key.
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
84
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
85 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
86 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
87
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
88 ```C
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
89 #include <stdio.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
90 #include <string.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
91 #include <cx/hash_map.h>
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
92
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
93 int main() {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
94 // store strings in the map...
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
95 CxMap *contacts = cxHashMapCreateSimple(sizeof(cxmutstr));
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
96 // ...which are automatically freed when removed
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
97 cxDefineDestructor(contacts, cx_strfree);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
98
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
99 // build a small interactive program
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
100 const unsigned buffer_size = 256;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
101 char input[buffer_size];
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
102 bool running = true;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
103
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
104 while (running) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
105 puts("\n*** Contacts - Menu ***");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
106 puts("1) Add entry");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
107 puts("2) Look up entry");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
108 puts("3) Remove entry");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
109 puts("4) Show all entries");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
110 puts("0) Exit");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
111 fputs("> ", stdout);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
113 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
114 if (input[1] != '\n') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
115 puts("Please select a menu item.\n");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
116 } else if (input[0] == '1') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
117 fputs("Name > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
118 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
119 // 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
120 cxmutstr name = cx_strdup(
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
121 cx_strn(input, strcspn(input, "\n")));
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
122 fputs("Phone > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
123 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
124 // 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
125 // 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
126 // 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
127 cxmutstr phone = cx_strdup(
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
128 cx_strn(input, strcspn(input, "\n")));
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
129 // 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
130 // 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
131 // generic selection
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
132 cxMapPut(contacts, name, &phone);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
133 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
134 cx_strfree(&name);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
135 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
136 } else if (input[0] == '2') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
137 fputs("Name > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
138 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
139 // Look up the name
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
140 input[strcspn(input, "\n")] = '\0';
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
141 cxmutstr *phone = cxMapGet(contacts, input);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
142 if (phone == NULL) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
143 puts("No record.");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
144 } else {
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
145 printf("Result: %" CX_PRIstr "\n",
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
146 CX_SFMT(*phone));
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
147 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
148 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
149 } else if (input[0] == '3') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
150 fputs("Name > ", stdout);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
151 if (fgets(input, buffer_size, stdin)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
152 // Remove the entry
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
153 // Since we registered cx_strfree as destructor,
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
154 // the memory is automatically freed
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
155 input[strcspn(input, "\n")] = '\0';
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
156 if (cxMapRemove(contacts, input)) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
157 puts("No such record.");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
158 } else {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
159 puts("Removed.");
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
160 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
161 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
162 } else if (input[0] == '4') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
163 // Almost the same iteration loop as above ...
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
164 CxMapIterator iter = cxMapIterator(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
165 cx_foreach(CxMapEntry *, entry, iter) {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
166 cxstring name = cx_strn(entry->key->data,
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
167 entry->key->len);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
168 // ... except that here we get cxmutstr*
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
169 cxmutstr *phone = entry->value;
1298
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
170 printf("%" CX_PRIstr ": %" CX_PRIstr "\n",
0597f1f20ea9 use new string formatting macros in documentation
Mike Becker <universe@uap-core.de>
parents: 1274
diff changeset
171 CX_SFMT(name), CX_SFMT(*phone));
1249
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
172 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
173 } else if (input[0] == '0') {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
174 running = false;
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 puts("Please select a menu item.\n");
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 } else {
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
179 running = false;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
180 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
181 }
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 // 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
184 cxMapFree(contacts);
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
185
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
186 return 0;
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
187 }
aad755d296a7 add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
188 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
189
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
190 ## Insert
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 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
193 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
194
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
195 int cxMapPut(CxMap *map, KeyType key, void *value);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
196 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
197
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
198 The function `cxMapPut()` stores the specified `key` / `value` pair.
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
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
205 If an element is already associated with the specified key, it is replaced.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
206 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
207 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
208
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
209 This function returns zero if the element was successfully put into the map and non-zero otherwise.
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
210
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
211 ## Access
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
212
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
213 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
214 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
215
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
216 void *cxMapGet(CxMap *map, KeyType key);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
217
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
218 size_t cxMapSize(const CxMap *map);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
220
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
221 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
222 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
223
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
224 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
225
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226 ## Remove
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
230
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
231 int cxMapRemove(CxMap *map, KeyType key);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233 int cxMapRemoveAndGet(CxMap *map, KeyType key, void* targetbuf);
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 void cxMapClear(CxMap *map);
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
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
238 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
239 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
240
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
241 On the other hand, `cxMapRemoveAndGet()` does not invoke the destructor functions,
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
242 and instead copies the value into the `targetbuf` which must be sufficiently large to hold the value.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
243
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
244 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
245
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
246 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
247
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
248 ## Iterators
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
249
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
250 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
251 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
252
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
253 CxMapIterator cxMapIterator(const CxMap *map);
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
254
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
255 CxMapIterator cxMapIteratorKeys(const CxMap *map);
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 CxMapIterator cxMapIteratorValues(const CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
258
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
259 CxMapIterator cxMapMutIterator(CxMap *map);
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
260
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
261 CxMapIterator cxMapMutIteratorKeys(CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
262
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
263 CxMapIterator cxMapMutIteratorValues(CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
264 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
265
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
266 The above functions create a ([mutating](iterator.h.md#mutating-iterators)) iterator over the
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
267 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
268
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
269 Iterators over pairs yield elements of type `CxMapEntry*` which is a struct containing a pointer to the `key` and the value, respectively.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
270
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
271 Iterators over keys yield elements of type `const CxHashKey*` (cf. [CxHashKey documentation](hash_key.h.md)).
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
272
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
273 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
274 Implementations are encouraged to support `CX_STORE_POINTERS`.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
275 If used, the `void*` elements the iterator yields, shall be directly the stored pointers.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
276 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
277
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
278 ## Dispose
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
279
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
280 ```C
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
281 #include <cx/map.h>
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
282
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
283 void cxMapFree(CxMap *map);
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
284 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
285
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
286 The function `cxMapFree()` invokes the [destructor functions](collection.h.md#destructor-functions) for all elements,
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
287 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
288
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
289 ## Implement own Map Structures
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
290
1274
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
291 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
292 you can also implement your own map.
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
293 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
294
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
295 ```C
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
296 typedef struct {
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
297 const CxHashKey *key;
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
298 void *value;
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
299 } CxMapEntry;
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
300 ```
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
301
1274
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
302 When you are declaring your map structure, a `CxMap` member must be embedded as first member of this structure.
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
303 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
304
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
305 ```C
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
306 #include <cx/map.h>
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
307
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
308 typedef struct {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
309 CxMap base;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
310 // ... 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
311 } MyMap;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
312
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
313 // declare the class - implement the functions somewhere
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
314 static cx_map_class my_map_class = {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
315 my_map_destructor,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
316 my_map_clear,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
317 my_map_put,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
318 my_map_get,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
319 my_map_remove,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
320 my_map_iterator,
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
321 };
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
322
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
323 // this function will create the map
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
324 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
325 if (allocator == NULL) {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
326 allocator = cxDefaultAllocator;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
327 }
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
328
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
329 // allocate memory
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
330 MyMap *map = cxCalloc(allocator, 1, sizeof(MyMap));
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
331 if (map == NULL) return NULL;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
332
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
333 // initialize base members
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
334 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
335 map->base.collection.allocator = allocator;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
336 if (itemsize == CX_STORE_POINTERS) {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
337 map->base.collection.elem_size = sizeof(void *);
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
338 map->base.collection.store_pointer = true;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
339 } else {
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
340 map->base.collection.elem_size = itemsize;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
341 }
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
342
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
343 // ... initialization of data members go here ...
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
344
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
345 return (CxMap *) map;
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
346 }
03e9360e98f5 add example for implementing own map
Mike Becker <universe@uap-core.de>
parents: 1250
diff changeset
347 ```
1243
13e15cd529ae structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
348
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
349 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
350 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
351
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
352 | Function | Description |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
353 |--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
354 | `clear` | Invoke [destructor functions](collection.h.md#destructor-functions) on all elements and remove them from the map. |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
355 | `deallocate` | Invoke destructor functions on all elements and deallocate the entire map memory. |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
356 | `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. Return non-zero when allocating memory fails. |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
357 | `get` | Look up the specified key and return the associated value (or `NULL` if the key was not found). |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
358 | `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. |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
359 | `iterator` | Return an iterator over the pairs, the keys, or the values, depending on the iterator type passed with the last argument. |
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
360
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1243
diff changeset
361 > 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
362 > 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
363 > 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
364 > 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
365 >{style="note"}
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
366
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
367 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
368 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
369 <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
370 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
371 </seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
372

mercurial