Mon, 13 Oct 2025 21:31:58 +0200
update ucx
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | /** | |
| 440 | 29 | * @file map.h |
| 30 | * @brief Interface for map implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 34 | */ |
| 35 | ||
| 36 | #ifndef UCX_MAP_H | |
| 37 | #define UCX_MAP_H | |
| 38 | ||
| 39 | #include "common.h" | |
| 40 | #include "collection.h" | |
| 41 | #include "string.h" | |
| 42 | #include "hash_key.h" | |
| 43 | ||
| 44 | #ifdef __cplusplus | |
| 45 | extern "C" { | |
| 46 | #endif | |
| 47 | ||
| 48 | /** Type for the UCX map. */ | |
| 49 | typedef struct cx_map_s CxMap; | |
| 50 | ||
| 51 | /** Type for a map entry. */ | |
| 52 | typedef struct cx_map_entry_s CxMapEntry; | |
| 53 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
54 | /** Type for a map iterator. */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
55 | typedef struct cx_map_iterator_s CxMapIterator; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
56 | |
| 174 | 57 | /** Type for map class definitions. */ |
| 58 | typedef struct cx_map_class_s cx_map_class; | |
| 59 | ||
| 60 | /** Structure for the UCX map. */ | |
| 61 | struct cx_map_s { | |
| 324 | 62 | /** |
| 63 | * Base attributes. | |
| 64 | */ | |
| 65 | CX_COLLECTION_BASE; | |
| 174 | 66 | /** The map class definition. */ |
| 67 | cx_map_class *cl; | |
| 68 | }; | |
| 69 | ||
| 70 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
71 | * A map entry. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
72 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
73 | struct cx_map_entry_s { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
74 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
75 | * A pointer to the key. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
76 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
77 | const CxHashKey *key; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
78 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
79 | * A pointer to the value. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
80 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
81 | void *value; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
82 | }; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
83 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
84 | /** |
| 174 | 85 | * The type of iterator for a map. |
| 86 | */ | |
| 87 | enum cx_map_iterator_type { | |
| 88 | /** | |
| 89 | * Iterates over key/value pairs. | |
| 90 | */ | |
| 91 | CX_MAP_ITERATOR_PAIRS, | |
| 92 | /** | |
| 93 | * Iterates over keys only. | |
| 94 | */ | |
| 95 | CX_MAP_ITERATOR_KEYS, | |
| 96 | /** | |
| 97 | * Iterates over values only. | |
| 98 | */ | |
| 99 | CX_MAP_ITERATOR_VALUES | |
| 100 | }; | |
| 101 | ||
| 102 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
103 | * Internal iterator struct - use CxMapIterator. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
104 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
105 | struct cx_map_iterator_s { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
106 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
107 | * Inherited common data for all iterators. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
108 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
109 | CX_ITERATOR_BASE; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
110 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
111 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
112 | * Handle for the source map. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
113 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
114 | union { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
115 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
116 | * Access for mutating iterators. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
117 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
118 | CxMap *m; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
119 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
120 | * Access for normal iterators. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
121 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
122 | const CxMap *c; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
123 | } map; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
124 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
125 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
126 | * Handle for the current element. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
127 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
128 | * @attention Depends on the map implementation, do not assume a type (better: do not use!). |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
129 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
130 | void *elem; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
131 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
132 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
133 | * Reserved memory for a map entry. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
134 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
135 | * If a map implementation uses an incompatible layout, the iterator needs something |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
136 | * to point to during iteration which @em is compatible. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
137 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
138 | CxMapEntry entry; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
139 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
140 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
141 | * Field for storing the current slot number. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
142 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
143 | * (Used internally) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
144 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
145 | size_t slot; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
146 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
147 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
148 | * Counts the elements successfully. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
149 | * It usually does not denote a stable index within the map as it would be for arrays. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
150 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
151 | size_t index; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
152 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
153 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
154 | * The size of a value stored in this map. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
155 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
156 | size_t elem_size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
157 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
158 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
159 | * May contain the total number of elements, if known. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
160 | * Set to @c SIZE_MAX when the total number is unknown during iteration. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
161 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
162 | * @remark The UCX implementations of #CxMap always know the number of elements they store. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
163 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
164 | size_t elem_count; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
165 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
166 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
167 | * The type of this iterator. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
168 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
169 | enum cx_map_iterator_type type; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
170 | }; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
171 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
172 | /** |
| 174 | 173 | * The class definition for arbitrary maps. |
| 174 | */ | |
| 175 | struct cx_map_class_s { | |
| 176 | /** | |
| 177 | * Deallocates the entire memory. | |
| 178 | */ | |
| 440 | 179 | void (*deallocate)(struct cx_map_s *map); |
| 174 | 180 | |
| 181 | /** | |
| 182 | * Removes all elements. | |
| 183 | */ | |
| 184 | void (*clear)(struct cx_map_s *map); | |
| 185 | ||
| 186 | /** | |
| 187 | * Add or overwrite an element. | |
| 845 | 188 | * If the @p value is @c NULL, the implementation |
| 189 | * shall only allocate memory instead of adding an existing value to the map. | |
| 190 | * Returns a pointer to the allocated memory or @c NULL if allocation fails. | |
| 174 | 191 | */ |
| 845 | 192 | void *(*put)( |
| 174 | 193 | CxMap *map, |
| 194 | CxHashKey key, | |
| 195 | void *value | |
| 196 | ); | |
| 197 | ||
| 198 | /** | |
| 199 | * Returns an element. | |
| 200 | */ | |
| 201 | void *(*get)( | |
| 324 | 202 | const CxMap *map, |
| 174 | 203 | CxHashKey key |
| 204 | ); | |
| 205 | ||
| 206 | /** | |
| 207 | * Removes an element. | |
| 440 | 208 | * |
| 209 | * Implementations SHALL check if @p targetbuf is set and copy the elements | |
| 210 | * to the buffer without invoking any destructor. | |
| 211 | * When @p targetbuf is not set, the destructors SHALL be invoked. | |
| 212 | * | |
| 213 | * The function SHALL return zero when the @p key was found and | |
| 214 | * non-zero, otherwise. | |
| 174 | 215 | */ |
| 440 | 216 | int (*remove)( |
| 174 | 217 | CxMap *map, |
| 218 | CxHashKey key, | |
| 440 | 219 | void *targetbuf |
| 174 | 220 | ); |
| 221 | ||
| 222 | /** | |
| 223 | * Creates an iterator for this map. | |
| 224 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
225 | CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); |
| 174 | 226 | }; |
| 227 | ||
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
228 | /** |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
229 | * A shared instance of an empty map. |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
230 | * |
| 440 | 231 | * Writing to that map is not allowed. |
| 232 | * | |
| 845 | 233 | * You can use this as a placeholder for initializing CxMap pointers |
| 440 | 234 | * for which you do not want to reserve memory right from the beginning. |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
235 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
236 | cx_attr_export |
| 174 | 237 | extern CxMap *const cxEmptyMap; |
| 238 | ||
| 239 | /** | |
| 240 | * Deallocates the memory of the specified map. | |
| 241 | * | |
| 440 | 242 | * Also calls the content destructor functions for each element, if specified. |
| 243 | * | |
| 244 | * @param map the map to be freed | |
| 174 | 245 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
246 | cx_attr_export |
| 440 | 247 | void cxMapFree(CxMap *map); |
| 174 | 248 | |
| 249 | ||
| 250 | /** | |
| 251 | * Clears a map by removing all elements. | |
| 252 | * | |
| 440 | 253 | * Also calls the content destructor functions for each element, if specified. |
| 254 | * | |
| 174 | 255 | * @param map the map to be cleared |
| 256 | */ | |
| 440 | 257 | cx_attr_nonnull |
| 174 | 258 | static inline void cxMapClear(CxMap *map) { |
| 259 | map->cl->clear(map); | |
| 260 | } | |
| 261 | ||
| 324 | 262 | /** |
| 263 | * Returns the number of elements in this map. | |
| 264 | * | |
| 265 | * @param map the map | |
| 266 | * @return the number of stored elements | |
| 267 | */ | |
| 440 | 268 | cx_attr_nonnull |
| 324 | 269 | static inline size_t cxMapSize(const CxMap *map) { |
| 270 | return map->collection.size; | |
| 271 | } | |
| 272 | ||
| 174 | 273 | /** |
| 274 | * Creates a value iterator for a map. | |
| 275 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
276 | * When the map is storing pointers, those pointers are returned. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
277 | * Otherwise, the iterator iterates over pointers to the memory within the map where the |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
278 | * respective elements are stored. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
279 | * |
| 440 | 280 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 281 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 282 | * | |
| 845 | 283 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 284 | * @return an iterator for the currently stored values |
| 285 | */ | |
| 440 | 286 | cx_attr_nodiscard |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
287 | static inline CxMapIterator cxMapIteratorValues(const CxMap *map) { |
| 845 | 288 | if (map == NULL) map = cxEmptyMap; |
| 174 | 289 | return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
| 290 | } | |
| 291 | ||
| 292 | /** | |
| 293 | * Creates a key iterator for a map. | |
| 294 | * | |
| 845 | 295 | * The elements of the iterator are keys of type CxHashKey, and the pointer returned |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
296 | * during iterator shall be treated as @c const @c CxHashKey* . |
| 174 | 297 | * |
| 440 | 298 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 299 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 300 | * | |
| 845 | 301 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 302 | * @return an iterator for the currently stored keys |
| 303 | */ | |
| 440 | 304 | cx_attr_nodiscard |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
305 | static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) { |
| 845 | 306 | if (map == NULL) map = cxEmptyMap; |
| 174 | 307 | return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); |
| 308 | } | |
| 309 | ||
| 310 | /** | |
| 311 | * Creates an iterator for a map. | |
| 312 | * | |
| 845 | 313 | * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
314 | * during iterator shall be treated as @c const @c CxMapEntry* . |
| 174 | 315 | * |
| 440 | 316 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 317 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 318 | * | |
| 845 | 319 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 320 | * @return an iterator for the currently stored entries |
| 321 | * @see cxMapIteratorKeys() | |
| 322 | * @see cxMapIteratorValues() | |
| 323 | */ | |
| 440 | 324 | cx_attr_nodiscard |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
325 | static inline CxMapIterator cxMapIterator(const CxMap *map) { |
| 845 | 326 | if (map == NULL) map = cxEmptyMap; |
| 174 | 327 | return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
| 328 | } | |
| 329 | ||
| 330 | ||
| 331 | /** | |
| 332 | * Creates a mutating iterator over the values of a map. | |
| 333 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
334 | * When the map is storing pointers, those pointers are returned. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
335 | * Otherwise, the iterator iterates over pointers to the memory within the map where the |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
336 | * respective elements are stored. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
337 | * |
| 440 | 338 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 339 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 340 | * | |
| 845 | 341 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 342 | * @return an iterator for the currently stored values |
| 343 | */ | |
| 440 | 344 | cx_attr_nodiscard |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
345 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
346 | CxMapIterator cxMapMutIteratorValues(CxMap *map); |
| 174 | 347 | |
| 348 | /** | |
| 349 | * Creates a mutating iterator over the keys of a map. | |
| 350 | * | |
| 845 | 351 | * The elements of the iterator are keys of type CxHashKey, and the pointer returned |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
352 | * during iterator shall be treated as @c const @c CxHashKey* . |
| 174 | 353 | * |
| 440 | 354 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 355 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 356 | * | |
| 845 | 357 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 358 | * @return an iterator for the currently stored keys |
| 359 | */ | |
| 440 | 360 | cx_attr_nodiscard |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
361 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
362 | CxMapIterator cxMapMutIteratorKeys(CxMap *map); |
| 174 | 363 | |
| 364 | /** | |
| 365 | * Creates a mutating iterator for a map. | |
| 366 | * | |
| 845 | 367 | * The elements of the iterator are key/value pairs of type CxMapEntry, and the pointer returned |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
368 | * during iterator shall be treated as @c const @c CxMapEntry* . |
| 174 | 369 | * |
| 440 | 370 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 371 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 372 | * | |
| 845 | 373 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 374 | * @return an iterator for the currently stored entries |
| 375 | * @see cxMapMutIteratorKeys() | |
| 376 | * @see cxMapMutIteratorValues() | |
| 377 | */ | |
| 440 | 378 | cx_attr_nodiscard |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
379 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
380 | CxMapIterator cxMapMutIterator(CxMap *map); |
| 174 | 381 | |
| 382 | /** | |
| 845 | 383 | * Puts a key/value-pair into the map. |
| 384 | * | |
| 385 | * A possible existing value will be overwritten. | |
| 386 | * If destructor functions are specified, they are called for | |
| 387 | * the overwritten element. | |
| 388 | * | |
| 389 | * If this map is storing pointers, the @p value pointer is written | |
| 390 | * to the map. Otherwise, the memory is copied from @p value with | |
| 391 | * memcpy(). | |
| 392 | * | |
| 393 | * The @p key is always copied. | |
| 394 | * | |
| 395 | * @param map the map | |
| 396 | * @param key the key | |
| 397 | * @param value the value | |
| 398 | * @retval zero success | |
| 399 | * @retval non-zero value on memory allocation failure | |
| 400 | * @see cxMapPut() | |
| 174 | 401 | */ |
| 440 | 402 | cx_attr_nonnull |
| 174 | 403 | static inline int cx_map_put( |
| 404 | CxMap *map, | |
| 405 | CxHashKey key, | |
| 406 | void *value | |
| 407 | ) { | |
| 845 | 408 | return map->cl->put(map, key, value) == NULL; |
| 174 | 409 | } |
| 410 | ||
| 411 | /** | |
| 412 | * Puts a key/value-pair into the map. | |
| 413 | * | |
| 440 | 414 | * A possible existing value will be overwritten. |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
415 | * If destructor functions are specified, they are called for |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
416 | * the overwritten element. |
| 440 | 417 | * |
| 418 | * If this map is storing pointers, the @p value pointer is written | |
| 419 | * to the map. Otherwise, the memory is copied from @p value with | |
| 420 | * memcpy(). | |
| 421 | * | |
| 422 | * The @p key is always copied. | |
| 423 | * | |
| 424 | * @param map (@c CxMap*) the map | |
| 845 | 425 | * @param key (any supported key type) the key |
| 440 | 426 | * @param value (@c void*) the value |
| 427 | * @retval zero success | |
| 428 | * @retval non-zero value on memory allocation failure | |
| 845 | 429 | * @see CX_HASH_KEY() |
| 174 | 430 | */ |
| 845 | 431 | #define cxMapPut(map, key, value) cx_map_put(map, CX_HASH_KEY(key), value) |
| 432 | ||
| 433 | /** | |
| 434 | * Allocates memory for a value in the map associated with the specified key. | |
| 435 | * | |
| 436 | * A possible existing value will be overwritten. | |
| 437 | * If destructor functions are specified, they are called for | |
| 438 | * the overwritten element. | |
| 439 | * | |
| 440 | * If the map is storing pointers, this function returns a @c void** pointer, | |
| 441 | * meaning a pointer to that pointer. | |
| 442 | * | |
| 443 | * The @p key is always copied. | |
| 444 | * | |
| 445 | * @param map the map | |
| 446 | * @param key the key | |
| 447 | * @return the pointer to the allocated memory or @c NULL if allocation fails | |
| 448 | * @retval zero success | |
| 449 | * @retval non-zero value on memory allocation failure | |
| 450 | * @see cxMapEmplace() | |
| 451 | */ | |
| 452 | cx_attr_nonnull | |
| 453 | static inline void *cx_map_emplace( | |
| 454 | CxMap *map, | |
| 455 | CxHashKey key | |
| 456 | ) { | |
| 457 | return map->cl->put(map, key, NULL); | |
| 458 | } | |
| 174 | 459 | |
| 460 | /** | |
| 845 | 461 | * Allocates memory for a value in the map associated with the specified key. |
| 462 | * | |
| 463 | * A possible existing value will be overwritten. | |
| 464 | * If destructor functions are specified, they are called for | |
| 465 | * the overwritten element. | |
| 466 | * | |
| 467 | * If the map is storing pointers, this function returns a @c void** pointer, | |
| 468 | * meaning a pointer to that pointer. | |
| 469 | * | |
| 470 | * The @p key is always copied. | |
| 471 | * | |
| 472 | * @param map (@c CxMap*) the map | |
| 473 | * @param key (any supported key type) the key | |
| 474 | * @return the pointer to the allocated memory or @c NULL if allocation fails | |
| 475 | * @retval zero success | |
| 476 | * @retval non-zero value on memory allocation failure | |
| 477 | * @see CX_HASH_KEY() | |
| 478 | */ | |
| 479 | #define cxMapEmplace(map, key) cx_map_emplace(map, CX_HASH_KEY(key)) | |
| 480 | ||
| 481 | /** | |
| 482 | * Retrieves a value by using a key. | |
| 483 | * | |
| 484 | * If this map is storing pointers, the stored pointer is returned. | |
| 485 | * Otherwise, a pointer to the element within the map's memory | |
| 486 | * is returned (which is valid as long as the element stays in the map). | |
| 487 | * | |
| 488 | * @param map the map | |
| 489 | * @param key the key | |
| 490 | * @return the value | |
| 491 | * @see cxMapGet() | |
| 174 | 492 | */ |
| 440 | 493 | cx_attr_nonnull |
| 494 | cx_attr_nodiscard | |
| 174 | 495 | static inline void *cx_map_get( |
| 324 | 496 | const CxMap *map, |
| 174 | 497 | CxHashKey key |
| 498 | ) { | |
| 499 | return map->cl->get(map, key); | |
| 500 | } | |
| 501 | ||
| 502 | /** | |
| 503 | * Retrieves a value by using a key. | |
| 504 | * | |
| 440 | 505 | * If this map is storing pointers, the stored pointer is returned. |
| 506 | * Otherwise, a pointer to the element within the map's memory | |
| 507 | * is returned (which is valid as long as the element stays in the map). | |
| 508 | * | |
| 509 | * @param map (@c CxMap*) the map | |
| 845 | 510 | * @param key (any supported key type) the key |
| 440 | 511 | * @return (@c void*) the value |
| 845 | 512 | * @see CX_HASH_KEY() |
| 440 | 513 | */ |
| 845 | 514 | #define cxMapGet(map, key) cx_map_get(map, CX_HASH_KEY(key)) |
| 174 | 515 | |
| 516 | /** | |
| 517 | * Removes a key/value-pair from the map by using the key. | |
| 518 | * | |
| 845 | 519 | * Invokes the destructor functions, if any, on the removed element if and only if the |
| 520 | * @p targetbuf is @c NULL. | |
| 174 | 521 | * |
| 845 | 522 | * @param map the map |
| 523 | * @param key the key | |
| 524 | * @param targetbuf the optional buffer where the removed element shall be copied to | |
| 440 | 525 | * @retval zero success |
| 526 | * @retval non-zero the key was not found | |
| 845 | 527 | * |
| 528 | * @see cxMapRemove() | |
| 174 | 529 | * @see cxMapRemoveAndGet() |
| 530 | */ | |
| 845 | 531 | cx_attr_nonnull_arg(1) |
| 532 | static inline int cx_map_remove( | |
| 174 | 533 | CxMap *map, |
| 440 | 534 | CxHashKey key, |
| 535 | void *targetbuf | |
| 174 | 536 | ) { |
| 440 | 537 | return map->cl->remove(map, key, targetbuf); |
| 174 | 538 | } |
| 539 | ||
| 540 | /** | |
| 845 | 541 | * Removes a key/value-pair from the map by using the key. |
| 542 | * | |
| 543 | * Always invokes the destructor functions, if any, on the removed element. | |
| 544 | * | |
| 545 | * @param map (@c CxMap*) the map | |
| 546 | * @param key (any supported key type) the key | |
| 547 | * @retval zero success | |
| 548 | * @retval non-zero the key was not found | |
| 549 | * | |
| 550 | * @see cxMapRemoveAndGet() | |
| 551 | * @see CX_HASH_KEY() | |
| 174 | 552 | */ |
| 845 | 553 | #define cxMapRemove(map, key) cx_map_remove(map, CX_HASH_KEY(key), NULL) |
| 174 | 554 | |
| 555 | /** | |
| 556 | * Removes a key/value-pair from the map by using the key. | |
| 557 | * | |
| 440 | 558 | * This function will copy the contents of the removed element |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
559 | * to the target buffer, which must be guaranteed to be large enough |
| 440 | 560 | * to hold the element (the map's element size). |
| 561 | * The destructor functions, if any, will @em not be called. | |
| 174 | 562 | * |
| 440 | 563 | * If this map is storing pointers, the element is the pointer itself |
| 564 | * and not the object it points to. | |
| 174 | 565 | * |
| 440 | 566 | * @param map (@c CxMap*) the map |
| 845 | 567 | * @param key (any supported key type) the key |
| 440 | 568 | * @param targetbuf (@c void*) the buffer where the element shall be copied to |
| 569 | * @retval zero success | |
| 570 | * @retval non-zero the key was not found | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
571 | * |
| 440 | 572 | * @see cxMapRemove() |
| 845 | 573 | * @see CX_HASH_KEY() |
| 174 | 574 | */ |
| 845 | 575 | #define cxMapRemoveAndGet(map, key, targetbuf) cx_map_remove(map, CX_HASH_KEY(key), targetbuf) |
| 174 | 576 | |
| 845 | 577 | #ifdef __cplusplus |
| 578 | } // extern "C" | |
| 579 | #endif | |
| 174 | 580 | |
| 581 | #endif // UCX_MAP_H |