Tue, 21 Oct 2025 16:20:51 +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 | */ |
| 870 | 114 | CxMap *map; |
|
471
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 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
117 | * 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
|
118 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
119 | * @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
|
120 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
121 | 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
|
122 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
123 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
124 | * 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
|
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 | * 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
|
127 | * 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
|
128 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
129 | 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
|
130 | |
|
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 | * 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
|
133 | * |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
134 | * (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
|
135 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
136 | 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
|
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 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
139 | * 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
|
140 | * 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
|
141 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
142 | 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
|
143 | |
|
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 | * 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
|
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 | 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
|
148 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
149 | /** |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
150 | * 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
|
151 | * 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
|
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 | * @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
|
154 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
155 | 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
|
156 | |
|
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 | * 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
|
159 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
160 | 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
|
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 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
163 | /** |
| 174 | 164 | * The class definition for arbitrary maps. |
| 165 | */ | |
| 166 | struct cx_map_class_s { | |
| 167 | /** | |
| 168 | * Deallocates the entire memory. | |
| 169 | */ | |
| 440 | 170 | void (*deallocate)(struct cx_map_s *map); |
| 174 | 171 | |
| 172 | /** | |
| 173 | * Removes all elements. | |
| 174 | */ | |
| 175 | void (*clear)(struct cx_map_s *map); | |
| 176 | ||
| 177 | /** | |
| 178 | * Add or overwrite an element. | |
| 845 | 179 | * If the @p value is @c NULL, the implementation |
| 180 | * shall only allocate memory instead of adding an existing value to the map. | |
| 181 | * Returns a pointer to the allocated memory or @c NULL if allocation fails. | |
| 174 | 182 | */ |
| 870 | 183 | void *(*put)(CxMap *map, CxHashKey key, void *value); |
| 174 | 184 | |
| 185 | /** | |
| 186 | * Returns an element. | |
| 187 | */ | |
| 870 | 188 | void *(*get)(const CxMap *map, CxHashKey key); |
| 174 | 189 | |
| 190 | /** | |
| 191 | * Removes an element. | |
| 440 | 192 | * |
| 193 | * Implementations SHALL check if @p targetbuf is set and copy the elements | |
| 194 | * to the buffer without invoking any destructor. | |
| 195 | * When @p targetbuf is not set, the destructors SHALL be invoked. | |
| 196 | * | |
| 197 | * The function SHALL return zero when the @p key was found and | |
| 198 | * non-zero, otherwise. | |
| 174 | 199 | */ |
| 870 | 200 | int (*remove)(CxMap *map, CxHashKey key, void *targetbuf); |
| 174 | 201 | |
| 202 | /** | |
| 203 | * Creates an iterator for this map. | |
| 204 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
205 | CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); |
| 174 | 206 | }; |
| 207 | ||
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
208 | /** |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
209 | * A shared instance of an empty map. |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
210 | * |
| 440 | 211 | * Writing to that map is not allowed. |
| 212 | * | |
| 845 | 213 | * You can use this as a placeholder for initializing CxMap pointers |
| 440 | 214 | * 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
|
215 | */ |
| 870 | 216 | CX_EXPORT extern CxMap *const cxEmptyMap; |
| 174 | 217 | |
| 218 | /** | |
| 219 | * Deallocates the memory of the specified map. | |
| 220 | * | |
| 440 | 221 | * Also calls the content destructor functions for each element, if specified. |
| 222 | * | |
| 223 | * @param map the map to be freed | |
| 174 | 224 | */ |
| 870 | 225 | CX_EXPORT void cxMapFree(CxMap *map); |
| 174 | 226 | |
| 227 | ||
| 228 | /** | |
| 229 | * Clears a map by removing all elements. | |
| 230 | * | |
| 440 | 231 | * Also calls the content destructor functions for each element, if specified. |
| 232 | * | |
| 174 | 233 | * @param map the map to be cleared |
| 234 | */ | |
| 440 | 235 | cx_attr_nonnull |
| 870 | 236 | CX_EXPORT void cxMapClear(CxMap *map); |
| 174 | 237 | |
| 324 | 238 | /** |
| 239 | * Returns the number of elements in this map. | |
| 240 | * | |
| 241 | * @param map the map | |
| 242 | * @return the number of stored elements | |
| 243 | */ | |
| 440 | 244 | cx_attr_nonnull |
| 870 | 245 | CX_EXPORT size_t cxMapSize(const CxMap *map); |
| 324 | 246 | |
| 174 | 247 | /** |
| 248 | * Creates a value iterator for a map. | |
| 249 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
250 | * 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
|
251 | * 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
|
252 | * 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
|
253 | * |
| 440 | 254 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 255 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 256 | * | |
| 845 | 257 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 258 | * @return an iterator for the currently stored values |
| 259 | */ | |
| 440 | 260 | cx_attr_nodiscard |
| 870 | 261 | CX_EXPORT CxMapIterator cxMapIteratorValues(const CxMap *map); |
| 174 | 262 | |
| 263 | /** | |
| 264 | * Creates a key iterator for a map. | |
| 265 | * | |
| 845 | 266 | * 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
|
267 | * during iterator shall be treated as @c const @c CxHashKey* . |
| 174 | 268 | * |
| 440 | 269 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 270 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 271 | * | |
| 845 | 272 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 273 | * @return an iterator for the currently stored keys |
| 274 | */ | |
| 440 | 275 | cx_attr_nodiscard |
| 870 | 276 | CX_EXPORT CxMapIterator cxMapIteratorKeys(const CxMap *map); |
| 174 | 277 | |
| 278 | /** | |
| 279 | * Creates an iterator for a map. | |
| 280 | * | |
| 845 | 281 | * 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
|
282 | * during iterator shall be treated as @c const @c CxMapEntry* . |
| 174 | 283 | * |
| 440 | 284 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 285 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 286 | * | |
| 845 | 287 | * @param map the map to create the iterator for (can be @c NULL) |
| 174 | 288 | * @return an iterator for the currently stored entries |
| 289 | * @see cxMapIteratorKeys() | |
| 290 | * @see cxMapIteratorValues() | |
| 291 | */ | |
| 440 | 292 | cx_attr_nodiscard |
| 870 | 293 | CX_EXPORT CxMapIterator cxMapIterator(const CxMap *map); |
| 174 | 294 | |
| 295 | /** | |
| 845 | 296 | * Puts a key/value-pair into the map. |
| 297 | * | |
| 298 | * A possible existing value will be overwritten. | |
| 299 | * If destructor functions are specified, they are called for | |
| 300 | * the overwritten element. | |
| 301 | * | |
| 302 | * If this map is storing pointers, the @p value pointer is written | |
| 303 | * to the map. Otherwise, the memory is copied from @p value with | |
| 304 | * memcpy(). | |
| 305 | * | |
| 306 | * The @p key is always copied. | |
| 307 | * | |
| 308 | * @param map the map | |
| 309 | * @param key the key | |
| 310 | * @param value the value | |
| 311 | * @retval zero success | |
| 312 | * @retval non-zero value on memory allocation failure | |
| 313 | * @see cxMapPut() | |
| 174 | 314 | */ |
| 440 | 315 | cx_attr_nonnull |
| 870 | 316 | CX_EXPORT int cx_map_put(CxMap *map, CxHashKey key, void *value); |
| 174 | 317 | |
| 318 | /** | |
| 319 | * Puts a key/value-pair into the map. | |
| 320 | * | |
| 440 | 321 | * 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
|
322 | * 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
|
323 | * the overwritten element. |
| 440 | 324 | * |
| 325 | * If this map is storing pointers, the @p value pointer is written | |
| 326 | * to the map. Otherwise, the memory is copied from @p value with | |
| 327 | * memcpy(). | |
| 328 | * | |
| 329 | * The @p key is always copied. | |
| 330 | * | |
| 331 | * @param map (@c CxMap*) the map | |
| 845 | 332 | * @param key (any supported key type) the key |
| 440 | 333 | * @param value (@c void*) the value |
| 334 | * @retval zero success | |
| 335 | * @retval non-zero value on memory allocation failure | |
| 845 | 336 | * @see CX_HASH_KEY() |
| 174 | 337 | */ |
| 845 | 338 | #define cxMapPut(map, key, value) cx_map_put(map, CX_HASH_KEY(key), value) |
| 339 | ||
| 340 | /** | |
| 341 | * Allocates memory for a value in the map associated with the specified key. | |
| 342 | * | |
| 343 | * A possible existing value will be overwritten. | |
| 344 | * If destructor functions are specified, they are called for | |
| 345 | * the overwritten element. | |
| 346 | * | |
| 347 | * If the map is storing pointers, this function returns a @c void** pointer, | |
| 348 | * meaning a pointer to that pointer. | |
| 349 | * | |
| 350 | * The @p key is always copied. | |
| 351 | * | |
| 352 | * @param map the map | |
| 353 | * @param key the key | |
| 354 | * @return the pointer to the allocated memory or @c NULL if allocation fails | |
| 355 | * @retval zero success | |
| 356 | * @retval non-zero value on memory allocation failure | |
| 357 | * @see cxMapEmplace() | |
| 358 | */ | |
| 359 | cx_attr_nonnull | |
| 870 | 360 | CX_EXPORT void *cx_map_emplace(CxMap *map, CxHashKey key); |
| 174 | 361 | |
| 362 | /** | |
| 845 | 363 | * Allocates memory for a value in the map associated with the specified key. |
| 364 | * | |
| 365 | * A possible existing value will be overwritten. | |
| 366 | * If destructor functions are specified, they are called for | |
| 367 | * the overwritten element. | |
| 368 | * | |
| 369 | * If the map is storing pointers, this function returns a @c void** pointer, | |
| 370 | * meaning a pointer to that pointer. | |
| 371 | * | |
| 372 | * The @p key is always copied. | |
| 373 | * | |
| 374 | * @param map (@c CxMap*) the map | |
| 375 | * @param key (any supported key type) the key | |
| 376 | * @return the pointer to the allocated memory or @c NULL if allocation fails | |
| 377 | * @retval zero success | |
| 378 | * @retval non-zero value on memory allocation failure | |
| 379 | * @see CX_HASH_KEY() | |
| 380 | */ | |
| 381 | #define cxMapEmplace(map, key) cx_map_emplace(map, CX_HASH_KEY(key)) | |
| 382 | ||
| 383 | /** | |
| 384 | * Retrieves a value by using a key. | |
| 385 | * | |
| 386 | * If this map is storing pointers, the stored pointer is returned. | |
| 387 | * Otherwise, a pointer to the element within the map's memory | |
| 388 | * is returned (which is valid as long as the element stays in the map). | |
| 389 | * | |
| 390 | * @param map the map | |
| 391 | * @param key the key | |
| 392 | * @return the value | |
| 393 | * @see cxMapGet() | |
| 174 | 394 | */ |
| 870 | 395 | cx_attr_nonnull cx_attr_nodiscard |
| 396 | CX_EXPORT void *cx_map_get(const CxMap *map, CxHashKey key); | |
| 174 | 397 | |
| 398 | /** | |
| 399 | * Retrieves a value by using a key. | |
| 400 | * | |
| 440 | 401 | * If this map is storing pointers, the stored pointer is returned. |
| 402 | * Otherwise, a pointer to the element within the map's memory | |
| 403 | * is returned (which is valid as long as the element stays in the map). | |
| 404 | * | |
| 405 | * @param map (@c CxMap*) the map | |
| 845 | 406 | * @param key (any supported key type) the key |
| 440 | 407 | * @return (@c void*) the value |
| 845 | 408 | * @see CX_HASH_KEY() |
| 440 | 409 | */ |
| 845 | 410 | #define cxMapGet(map, key) cx_map_get(map, CX_HASH_KEY(key)) |
| 174 | 411 | |
| 412 | /** | |
| 413 | * Removes a key/value-pair from the map by using the key. | |
| 414 | * | |
| 845 | 415 | * Invokes the destructor functions, if any, on the removed element if and only if the |
| 416 | * @p targetbuf is @c NULL. | |
| 174 | 417 | * |
| 845 | 418 | * @param map the map |
| 419 | * @param key the key | |
| 420 | * @param targetbuf the optional buffer where the removed element shall be copied to | |
| 440 | 421 | * @retval zero success |
| 422 | * @retval non-zero the key was not found | |
| 845 | 423 | * |
| 424 | * @see cxMapRemove() | |
| 174 | 425 | * @see cxMapRemoveAndGet() |
| 426 | */ | |
| 845 | 427 | cx_attr_nonnull_arg(1) |
| 870 | 428 | CX_EXPORT int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf); |
| 174 | 429 | |
| 430 | /** | |
| 845 | 431 | * Removes a key/value-pair from the map by using the key. |
| 432 | * | |
| 433 | * Always invokes the destructor functions, if any, on the removed element. | |
| 434 | * | |
| 435 | * @param map (@c CxMap*) the map | |
| 436 | * @param key (any supported key type) the key | |
| 437 | * @retval zero success | |
| 438 | * @retval non-zero the key was not found | |
| 439 | * | |
| 440 | * @see cxMapRemoveAndGet() | |
| 441 | * @see CX_HASH_KEY() | |
| 174 | 442 | */ |
| 845 | 443 | #define cxMapRemove(map, key) cx_map_remove(map, CX_HASH_KEY(key), NULL) |
| 174 | 444 | |
| 445 | /** | |
| 446 | * Removes a key/value-pair from the map by using the key. | |
| 447 | * | |
| 440 | 448 | * 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
|
449 | * to the target buffer, which must be guaranteed to be large enough |
| 440 | 450 | * to hold the element (the map's element size). |
| 451 | * The destructor functions, if any, will @em not be called. | |
| 174 | 452 | * |
| 440 | 453 | * If this map is storing pointers, the element is the pointer itself |
| 454 | * and not the object it points to. | |
| 174 | 455 | * |
| 440 | 456 | * @param map (@c CxMap*) the map |
| 845 | 457 | * @param key (any supported key type) the key |
| 440 | 458 | * @param targetbuf (@c void*) the buffer where the element shall be copied to |
| 459 | * @retval zero success | |
| 460 | * @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
|
461 | * |
| 440 | 462 | * @see cxMapRemove() |
| 845 | 463 | * @see CX_HASH_KEY() |
| 174 | 464 | */ |
| 845 | 465 | #define cxMapRemoveAndGet(map, key, targetbuf) cx_map_remove(map, CX_HASH_KEY(key), targetbuf) |
| 174 | 466 | |
| 845 | 467 | #ifdef __cplusplus |
| 468 | } // extern "C" | |
| 469 | #endif | |
| 174 | 470 | |
| 471 | #endif // UCX_MAP_H |