Tue, 18 Mar 2025 19:03:18 +0100
basic structure for tree.h documentation
relates to #451
1143
0559812df10c
assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents:
1142
diff
changeset
|
1 | # Map Interface |
1141 | 2 | |
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 | 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 | 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; |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
70 | printf("%.*s: %s\n", (int) name.length, name.ptr, phone); |
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 | |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
85 | In real world situations, however, it is quite unlikely that you will use a map to store string literals. |
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 | 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 { |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
145 | printf("Result: %.*s\n", |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
146 | (int) phone->length, phone->ptr); |
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; |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
170 | printf("%.*s: %.*s\n", |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
171 | (int) name.length, name.ptr, |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
172 | (int) phone->length, phone->ptr); |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
173 | } |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
174 | } else if (input[0] == '0') { |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
175 | running = false; |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
176 | } else { |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
177 | puts("Please select a menu item.\n"); |
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 | } else { |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
180 | running = false; |
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 | |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
184 | // 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
|
185 | cxMapFree(contacts); |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
186 | |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
187 | return 0; |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
188 | } |
aad755d296a7
add examples to map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1245
diff
changeset
|
189 | ``` |
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
190 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
191 | ## Insert |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
192 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
193 | ```C |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
194 | #include <cx/map.h> |
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 | int cxMapPut(CxMap *map, KeyType key, void *value); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
197 | ``` |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
198 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
199 | 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
|
200 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
201 | The key is always copied. |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
202 | 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
|
203 | 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
|
204 | 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
|
205 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
206 | 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
|
207 | 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
|
208 | 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
|
209 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
210 | 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
|
211 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
212 | ## Access |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
213 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
214 | ```C |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
215 | #include <cx/map.h> |
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 | void *cxMapGet(CxMap *map, KeyType key); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
218 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
219 | size_t cxMapSize(const CxMap *map); |
1141 | 220 | ``` |
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
221 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
222 | 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
|
223 | 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
|
224 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
225 | 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
|
226 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
227 | ## Remove |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
228 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
229 | ```C |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
230 | #include <cx/map.h> |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
231 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
232 | int cxMapRemove(CxMap *map, KeyType key); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
233 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
234 | int cxMapRemoveAndGet(CxMap *map, KeyType key, void* targetbuf); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
235 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
236 | void cxMapClear(CxMap *map); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
237 | ``` |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
238 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
239 | 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
|
240 | 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
|
241 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
242 | 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
|
243 | 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
|
244 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
245 | 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
|
246 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
247 | 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
|
248 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
249 | ## Iterators |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
250 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
251 | ```C |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
252 | #include <cx/map.h> |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
253 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
254 | CxMapIterator cxMapIterator(const CxMap *map); |
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
255 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
256 | CxMapIterator cxMapIteratorKeys(const CxMap *map); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
257 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
258 | CxMapIterator cxMapIteratorValues(const CxMap *map); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
259 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
260 | CxMapIterator cxMapMutIterator(CxMap *map); |
1142
9437530176bc
add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents:
1141
diff
changeset
|
261 | |
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
262 | CxMapIterator cxMapMutIteratorKeys(CxMap *map); |
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 cxMapMutIteratorValues(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 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
267 | 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
|
268 | 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
|
269 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
270 | 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
|
271 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
272 | 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
|
273 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
274 | 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
|
275 | 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
|
276 | 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
|
277 | 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
|
278 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
279 | ## Dispose |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
280 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
281 | ```C |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
282 | #include <cx/map.h> |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
283 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
284 | void cxMapFree(CxMap *map); |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
285 | ``` |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
286 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
287 | 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
|
288 | 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
|
289 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
290 | ## Implement own Map Structures |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
291 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
292 | ```C |
1245
721e2032fa25
define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents:
1244
diff
changeset
|
293 | typedef struct { |
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
294 | const CxHashKey *key; |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
295 | void *value; |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
296 | } CxMapEntry; |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
297 | ``` |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
298 | |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
299 | <warning> |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
300 | TODO: example |
1243
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
301 | </warning> |
13e15cd529ae
structur for map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1190
diff
changeset
|
302 | |
1244
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
303 | 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
|
304 | 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
|
305 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
306 | | Function | Description | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
307 | |--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
308 | | `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
|
309 | | `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
|
310 | | `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
|
311 | | `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
|
312 | | `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
|
313 | | `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
|
314 | |
9a8e781258ac
complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents:
1243
diff
changeset
|
315 | > 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
|
316 | > 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
|
317 | > 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
|
318 | > 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
|
319 | >{style="note"} |
1190
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
320 | |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
321 | <seealso> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
322 | <category ref="apidoc"> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
323 | <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
|
324 | </category> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
325 | </seealso> |
a7b913d5d589
bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents:
1146
diff
changeset
|
326 |