Fri, 03 Oct 2025 09:03:59 +0200
implement spinbox width arg (GTK)
| 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. | |
| 188 | */ | |
| 189 | int (*put)( | |
| 190 | CxMap *map, | |
| 191 | CxHashKey key, | |
| 192 | void *value | |
| 193 | ); | |
| 194 | ||
| 195 | /** | |
| 196 | * Returns an element. | |
| 197 | */ | |
| 198 | void *(*get)( | |
| 324 | 199 | const CxMap *map, |
| 174 | 200 | CxHashKey key |
| 201 | ); | |
| 202 | ||
| 203 | /** | |
| 204 | * Removes an element. | |
| 440 | 205 | * |
| 206 | * Implementations SHALL check if @p targetbuf is set and copy the elements | |
| 207 | * to the buffer without invoking any destructor. | |
| 208 | * When @p targetbuf is not set, the destructors SHALL be invoked. | |
| 209 | * | |
| 210 | * The function SHALL return zero when the @p key was found and | |
| 211 | * non-zero, otherwise. | |
| 174 | 212 | */ |
| 440 | 213 | int (*remove)( |
| 174 | 214 | CxMap *map, |
| 215 | CxHashKey key, | |
| 440 | 216 | void *targetbuf |
| 174 | 217 | ); |
| 218 | ||
| 219 | /** | |
| 220 | * Creates an iterator for this map. | |
| 221 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
222 | CxMapIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type); |
| 174 | 223 | }; |
| 224 | ||
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
225 | /** |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
226 | * A shared instance of an empty map. |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
227 | * |
| 440 | 228 | * Writing to that map is not allowed. |
| 229 | * | |
| 230 | * You can use this is a placeholder for initializing CxMap pointers | |
| 231 | * 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
|
232 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
233 | cx_attr_export |
| 174 | 234 | extern CxMap *const cxEmptyMap; |
| 235 | ||
| 236 | /** | |
| 237 | * Deallocates the memory of the specified map. | |
| 238 | * | |
| 440 | 239 | * Also calls the content destructor functions for each element, if specified. |
| 240 | * | |
| 241 | * @param map the map to be freed | |
| 174 | 242 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
243 | cx_attr_export |
| 440 | 244 | void cxMapFree(CxMap *map); |
| 174 | 245 | |
| 246 | ||
| 247 | /** | |
| 248 | * Clears a map by removing all elements. | |
| 249 | * | |
| 440 | 250 | * Also calls the content destructor functions for each element, if specified. |
| 251 | * | |
| 174 | 252 | * @param map the map to be cleared |
| 253 | */ | |
| 440 | 254 | cx_attr_nonnull |
| 174 | 255 | static inline void cxMapClear(CxMap *map) { |
| 256 | map->cl->clear(map); | |
| 257 | } | |
| 258 | ||
| 324 | 259 | /** |
| 260 | * Returns the number of elements in this map. | |
| 261 | * | |
| 262 | * @param map the map | |
| 263 | * @return the number of stored elements | |
| 264 | */ | |
| 440 | 265 | cx_attr_nonnull |
| 324 | 266 | static inline size_t cxMapSize(const CxMap *map) { |
| 267 | return map->collection.size; | |
| 268 | } | |
| 269 | ||
| 174 | 270 | /** |
| 271 | * Creates a value iterator for a map. | |
| 272 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
273 | * 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
|
274 | * 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
|
275 | * 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
|
276 | * |
| 440 | 277 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 278 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 279 | * | |
| 280 | * @param map the map to create the iterator for | |
| 281 | * @return an iterator for the currently stored values | |
| 282 | */ | |
| 440 | 283 | cx_attr_nonnull |
| 284 | 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
|
285 | static inline CxMapIterator cxMapIteratorValues(const CxMap *map) { |
| 174 | 286 | return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES); |
| 287 | } | |
| 288 | ||
| 289 | /** | |
| 290 | * Creates a key iterator for a map. | |
| 291 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
292 | * The elements of the iterator are keys of type CxHashKey and the pointer returned |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
293 | * during iterator shall be treated as @c const @c CxHashKey* . |
| 174 | 294 | * |
| 440 | 295 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 296 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 297 | * | |
| 298 | * @param map the map to create the iterator for | |
| 299 | * @return an iterator for the currently stored keys | |
| 300 | */ | |
| 440 | 301 | cx_attr_nonnull |
| 302 | 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
|
303 | static inline CxMapIterator cxMapIteratorKeys(const CxMap *map) { |
| 174 | 304 | return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS); |
| 305 | } | |
| 306 | ||
| 307 | /** | |
| 308 | * Creates an iterator for a map. | |
| 309 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
310 | * The elements of the iterator are key/value pairs of type CxMapEntry and the pointer returned |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
311 | * during iterator shall be treated as @c const @c CxMapEntry* . |
| 174 | 312 | * |
| 440 | 313 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 314 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 315 | * | |
| 316 | * @param map the map to create the iterator for | |
| 317 | * @return an iterator for the currently stored entries | |
| 318 | * @see cxMapIteratorKeys() | |
| 319 | * @see cxMapIteratorValues() | |
| 320 | */ | |
| 440 | 321 | cx_attr_nonnull |
| 322 | 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
|
323 | static inline CxMapIterator cxMapIterator(const CxMap *map) { |
| 174 | 324 | return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS); |
| 325 | } | |
| 326 | ||
| 327 | ||
| 328 | /** | |
| 329 | * Creates a mutating iterator over the values of a map. | |
| 330 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
331 | * 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
|
332 | * 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
|
333 | * 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
|
334 | * |
| 440 | 335 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 336 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 337 | * | |
| 338 | * @param map the map to create the iterator for | |
| 339 | * @return an iterator for the currently stored values | |
| 340 | */ | |
| 440 | 341 | cx_attr_nonnull |
| 342 | 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
|
343 | 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
|
344 | CxMapIterator cxMapMutIteratorValues(CxMap *map); |
| 174 | 345 | |
| 346 | /** | |
| 347 | * Creates a mutating iterator over the keys of a map. | |
| 348 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
349 | * The elements of the iterator are keys of type CxHashKey and the pointer returned |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
350 | * during iterator shall be treated as @c const @c CxHashKey* . |
| 174 | 351 | * |
| 440 | 352 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 353 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 354 | * | |
| 355 | * @param map the map to create the iterator for | |
| 356 | * @return an iterator for the currently stored keys | |
| 357 | */ | |
| 440 | 358 | cx_attr_nonnull |
| 359 | 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
|
360 | 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
|
361 | CxMapIterator cxMapMutIteratorKeys(CxMap *map); |
| 174 | 362 | |
| 363 | /** | |
| 364 | * Creates a mutating iterator for a map. | |
| 365 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
366 | * The elements of the iterator are key/value pairs of type CxMapEntry and the pointer returned |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
367 | * during iterator shall be treated as @c const @c CxMapEntry* . |
| 174 | 368 | * |
| 440 | 369 | * @note An iterator iterates over all elements successively. Therefore, the order |
| 174 | 370 | * highly depends on the map implementation and may change arbitrarily when the contents change. |
| 371 | * | |
| 372 | * @param map the map to create the iterator for | |
| 373 | * @return an iterator for the currently stored entries | |
| 374 | * @see cxMapMutIteratorKeys() | |
| 375 | * @see cxMapMutIteratorValues() | |
| 376 | */ | |
| 440 | 377 | cx_attr_nonnull |
| 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 | #ifdef __cplusplus | |
| 383 | } // end the extern "C" block here, because we want to start overloading | |
| 440 | 384 | cx_attr_nonnull |
| 174 | 385 | static inline int cxMapPut( |
| 386 | CxMap *map, | |
| 387 | CxHashKey const &key, | |
| 388 | void *value | |
| 389 | ) { | |
| 390 | return map->cl->put(map, key, value); | |
| 391 | } | |
| 392 | ||
| 440 | 393 | cx_attr_nonnull |
| 174 | 394 | static inline int cxMapPut( |
| 395 | CxMap *map, | |
| 396 | cxstring const &key, | |
| 397 | void *value | |
| 398 | ) { | |
| 399 | return map->cl->put(map, cx_hash_key_cxstr(key), value); | |
| 400 | } | |
| 401 | ||
| 440 | 402 | cx_attr_nonnull |
| 174 | 403 | static inline int cxMapPut( |
| 404 | CxMap *map, | |
| 405 | cxmutstr const &key, | |
| 406 | void *value | |
| 407 | ) { | |
| 408 | return map->cl->put(map, cx_hash_key_cxstr(key), value); | |
| 409 | } | |
| 410 | ||
| 440 | 411 | cx_attr_nonnull |
| 412 | cx_attr_cstr_arg(2) | |
| 174 | 413 | static inline int cxMapPut( |
| 414 | CxMap *map, | |
| 324 | 415 | const char *key, |
| 174 | 416 | void *value |
| 417 | ) { | |
| 418 | return map->cl->put(map, cx_hash_key_str(key), value); | |
| 419 | } | |
| 420 | ||
| 440 | 421 | cx_attr_nonnull |
| 422 | cx_attr_nodiscard | |
| 174 | 423 | static inline void *cxMapGet( |
| 324 | 424 | const CxMap *map, |
| 174 | 425 | CxHashKey const &key |
| 426 | ) { | |
| 427 | return map->cl->get(map, key); | |
| 428 | } | |
| 429 | ||
| 440 | 430 | cx_attr_nonnull |
| 431 | cx_attr_nodiscard | |
| 174 | 432 | static inline void *cxMapGet( |
| 324 | 433 | const CxMap *map, |
| 174 | 434 | cxstring const &key |
| 435 | ) { | |
| 436 | return map->cl->get(map, cx_hash_key_cxstr(key)); | |
| 437 | } | |
| 438 | ||
| 440 | 439 | cx_attr_nonnull |
| 440 | cx_attr_nodiscard | |
| 174 | 441 | static inline void *cxMapGet( |
| 324 | 442 | const CxMap *map, |
| 174 | 443 | cxmutstr const &key |
| 444 | ) { | |
| 445 | return map->cl->get(map, cx_hash_key_cxstr(key)); | |
| 446 | } | |
| 447 | ||
| 440 | 448 | cx_attr_nonnull |
| 449 | cx_attr_nodiscard | |
| 450 | cx_attr_cstr_arg(2) | |
| 174 | 451 | static inline void *cxMapGet( |
| 324 | 452 | const CxMap *map, |
| 453 | const char *key | |
| 174 | 454 | ) { |
| 455 | return map->cl->get(map, cx_hash_key_str(key)); | |
| 456 | } | |
| 457 | ||
| 440 | 458 | cx_attr_nonnull |
| 459 | static inline int cxMapRemove( | |
| 174 | 460 | CxMap *map, |
| 461 | CxHashKey const &key | |
| 462 | ) { | |
| 440 | 463 | return map->cl->remove(map, key, nullptr); |
| 174 | 464 | } |
| 465 | ||
| 440 | 466 | cx_attr_nonnull |
| 467 | static inline int cxMapRemove( | |
| 174 | 468 | CxMap *map, |
| 469 | cxstring const &key | |
| 470 | ) { | |
| 440 | 471 | return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr); |
| 174 | 472 | } |
| 473 | ||
| 440 | 474 | cx_attr_nonnull |
| 475 | static inline int cxMapRemove( | |
| 174 | 476 | CxMap *map, |
| 477 | cxmutstr const &key | |
| 478 | ) { | |
| 440 | 479 | return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr); |
| 174 | 480 | } |
| 481 | ||
| 440 | 482 | cx_attr_nonnull |
| 483 | cx_attr_cstr_arg(2) | |
| 484 | static inline int cxMapRemove( | |
| 174 | 485 | CxMap *map, |
| 324 | 486 | const char *key |
| 174 | 487 | ) { |
| 440 | 488 | return map->cl->remove(map, cx_hash_key_str(key), nullptr); |
| 174 | 489 | } |
| 490 | ||
| 440 | 491 | cx_attr_nonnull |
| 492 | cx_attr_access_w(3) | |
| 493 | static inline int cxMapRemoveAndGet( | |
| 174 | 494 | CxMap *map, |
| 440 | 495 | CxHashKey key, |
| 496 | void *targetbuf | |
| 174 | 497 | ) { |
| 440 | 498 | return map->cl->remove(map, key, targetbuf); |
| 174 | 499 | } |
| 500 | ||
| 440 | 501 | cx_attr_nonnull |
| 502 | cx_attr_access_w(3) | |
| 503 | static inline int cxMapRemoveAndGet( | |
| 174 | 504 | CxMap *map, |
| 440 | 505 | cxstring key, |
| 506 | void *targetbuf | |
| 174 | 507 | ) { |
| 440 | 508 | return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf); |
| 174 | 509 | } |
| 510 | ||
| 440 | 511 | cx_attr_nonnull |
| 512 | cx_attr_access_w(3) | |
| 513 | static inline int cxMapRemoveAndGet( | |
| 174 | 514 | CxMap *map, |
| 440 | 515 | cxmutstr key, |
| 516 | void *targetbuf | |
| 174 | 517 | ) { |
| 440 | 518 | return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf); |
| 174 | 519 | } |
| 520 | ||
| 440 | 521 | cx_attr_nonnull |
| 522 | cx_attr_access_w(3) | |
| 523 | cx_attr_cstr_arg(2) | |
| 524 | static inline int cxMapRemoveAndGet( | |
| 174 | 525 | CxMap *map, |
| 440 | 526 | const char *key, |
| 527 | void *targetbuf | |
| 174 | 528 | ) { |
| 440 | 529 | return map->cl->remove(map, cx_hash_key_str(key), targetbuf); |
| 174 | 530 | } |
| 531 | ||
| 532 | #else // __cplusplus | |
| 533 | ||
| 534 | /** | |
| 440 | 535 | * @copydoc cxMapPut() |
| 174 | 536 | */ |
| 440 | 537 | cx_attr_nonnull |
| 174 | 538 | static inline int cx_map_put( |
| 539 | CxMap *map, | |
| 540 | CxHashKey key, | |
| 541 | void *value | |
| 542 | ) { | |
| 543 | return map->cl->put(map, key, value); | |
| 544 | } | |
| 545 | ||
| 546 | /** | |
| 440 | 547 | * @copydoc cxMapPut() |
| 174 | 548 | */ |
| 440 | 549 | cx_attr_nonnull |
| 174 | 550 | static inline int cx_map_put_cxstr( |
| 551 | CxMap *map, | |
| 552 | cxstring key, | |
| 553 | void *value | |
| 554 | ) { | |
| 555 | return map->cl->put(map, cx_hash_key_cxstr(key), value); | |
| 556 | } | |
| 557 | ||
| 558 | /** | |
| 440 | 559 | * @copydoc cxMapPut() |
| 174 | 560 | */ |
| 440 | 561 | cx_attr_nonnull |
| 174 | 562 | static inline int cx_map_put_mustr( |
| 563 | CxMap *map, | |
| 564 | cxmutstr key, | |
| 565 | void *value | |
| 566 | ) { | |
| 567 | return map->cl->put(map, cx_hash_key_cxstr(key), value); | |
| 568 | } | |
| 569 | ||
| 570 | /** | |
| 440 | 571 | * @copydoc cxMapPut() |
| 174 | 572 | */ |
| 440 | 573 | cx_attr_nonnull |
| 574 | cx_attr_cstr_arg(2) | |
| 174 | 575 | static inline int cx_map_put_str( |
| 576 | CxMap *map, | |
| 324 | 577 | const char *key, |
| 174 | 578 | void *value |
| 579 | ) { | |
| 580 | return map->cl->put(map, cx_hash_key_str(key), value); | |
| 581 | } | |
| 582 | ||
| 583 | /** | |
| 584 | * Puts a key/value-pair into the map. | |
| 585 | * | |
| 440 | 586 | * 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
|
587 | * 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
|
588 | * the overwritten element. |
| 440 | 589 | * |
| 590 | * If this map is storing pointers, the @p value pointer is written | |
| 591 | * to the map. Otherwise, the memory is copied from @p value with | |
| 592 | * memcpy(). | |
| 593 | * | |
| 594 | * The @p key is always copied. | |
| 595 | * | |
| 596 | * @param map (@c CxMap*) the map | |
| 597 | * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key | |
| 598 | * @param value (@c void*) the value | |
| 599 | * @retval zero success | |
| 600 | * @retval non-zero value on memory allocation failure | |
| 174 | 601 | */ |
| 602 | #define cxMapPut(map, key, value) _Generic((key), \ | |
| 603 | CxHashKey: cx_map_put, \ | |
| 604 | cxstring: cx_map_put_cxstr, \ | |
| 605 | cxmutstr: cx_map_put_mustr, \ | |
| 606 | char*: cx_map_put_str, \ | |
| 324 | 607 | const char*: cx_map_put_str) \ |
| 174 | 608 | (map, key, value) |
| 609 | ||
| 610 | /** | |
| 440 | 611 | * @copydoc cxMapGet() |
| 174 | 612 | */ |
| 440 | 613 | cx_attr_nonnull |
| 614 | cx_attr_nodiscard | |
| 174 | 615 | static inline void *cx_map_get( |
| 324 | 616 | const CxMap *map, |
| 174 | 617 | CxHashKey key |
| 618 | ) { | |
| 619 | return map->cl->get(map, key); | |
| 620 | } | |
| 621 | ||
| 622 | /** | |
| 440 | 623 | * @copydoc cxMapGet() |
| 174 | 624 | */ |
| 440 | 625 | cx_attr_nonnull |
| 626 | cx_attr_nodiscard | |
| 174 | 627 | static inline void *cx_map_get_cxstr( |
| 324 | 628 | const CxMap *map, |
| 174 | 629 | cxstring key |
| 630 | ) { | |
| 631 | return map->cl->get(map, cx_hash_key_cxstr(key)); | |
| 632 | } | |
| 633 | ||
| 634 | /** | |
| 440 | 635 | * @copydoc cxMapGet() |
| 174 | 636 | */ |
| 440 | 637 | cx_attr_nonnull |
| 638 | cx_attr_nodiscard | |
| 174 | 639 | static inline void *cx_map_get_mustr( |
| 324 | 640 | const CxMap *map, |
| 174 | 641 | cxmutstr key |
| 642 | ) { | |
| 643 | return map->cl->get(map, cx_hash_key_cxstr(key)); | |
| 644 | } | |
| 645 | ||
| 646 | /** | |
| 440 | 647 | * @copydoc cxMapGet() |
| 174 | 648 | */ |
| 440 | 649 | cx_attr_nonnull |
| 650 | cx_attr_nodiscard | |
| 651 | cx_attr_cstr_arg(2) | |
| 174 | 652 | static inline void *cx_map_get_str( |
| 324 | 653 | const CxMap *map, |
| 654 | const char *key | |
| 174 | 655 | ) { |
| 656 | return map->cl->get(map, cx_hash_key_str(key)); | |
| 657 | } | |
| 658 | ||
| 659 | /** | |
| 660 | * Retrieves a value by using a key. | |
| 661 | * | |
| 440 | 662 | * If this map is storing pointers, the stored pointer is returned. |
| 663 | * Otherwise, a pointer to the element within the map's memory | |
| 664 | * is returned (which is valid as long as the element stays in the map). | |
| 665 | * | |
| 666 | * @param map (@c CxMap*) the map | |
| 667 | * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key | |
| 668 | * @return (@c void*) the value | |
| 174 | 669 | */ |
| 670 | #define cxMapGet(map, key) _Generic((key), \ | |
| 671 | CxHashKey: cx_map_get, \ | |
| 672 | cxstring: cx_map_get_cxstr, \ | |
| 673 | cxmutstr: cx_map_get_mustr, \ | |
| 674 | char*: cx_map_get_str, \ | |
| 324 | 675 | const char*: cx_map_get_str) \ |
| 174 | 676 | (map, key) |
| 677 | ||
| 678 | /** | |
| 440 | 679 | * @copydoc cxMapRemove() |
| 174 | 680 | */ |
| 440 | 681 | cx_attr_nonnull |
| 682 | static inline int cx_map_remove( | |
| 174 | 683 | CxMap *map, |
| 684 | CxHashKey key | |
| 685 | ) { | |
| 440 | 686 | return map->cl->remove(map, key, NULL); |
| 687 | } | |
| 688 | ||
| 689 | /** | |
| 690 | * @copydoc cxMapRemove() | |
| 691 | */ | |
| 692 | cx_attr_nonnull | |
| 693 | static inline int cx_map_remove_cxstr( | |
| 694 | CxMap *map, | |
| 695 | cxstring key | |
| 696 | ) { | |
| 697 | return map->cl->remove(map, cx_hash_key_cxstr(key), NULL); | |
| 174 | 698 | } |
| 699 | ||
| 700 | /** | |
| 440 | 701 | * @copydoc cxMapRemove() |
| 174 | 702 | */ |
| 440 | 703 | cx_attr_nonnull |
| 704 | static inline int cx_map_remove_mustr( | |
| 174 | 705 | CxMap *map, |
| 440 | 706 | cxmutstr key |
| 174 | 707 | ) { |
| 440 | 708 | return map->cl->remove(map, cx_hash_key_cxstr(key), NULL); |
| 709 | } | |
| 710 | ||
| 711 | /** | |
| 712 | * @copydoc cxMapRemove() | |
| 713 | */ | |
| 714 | cx_attr_nonnull | |
| 715 | cx_attr_cstr_arg(2) | |
| 716 | static inline int cx_map_remove_str( | |
| 717 | CxMap *map, | |
| 718 | const char *key | |
| 719 | ) { | |
| 720 | return map->cl->remove(map, cx_hash_key_str(key), NULL); | |
| 174 | 721 | } |
| 722 | ||
| 723 | /** | |
| 724 | * Removes a key/value-pair from the map by using the key. | |
| 725 | * | |
| 440 | 726 | * Always invokes the destructors functions, if any, on the removed element. |
| 174 | 727 | * |
| 440 | 728 | * @param map (@c CxMap*) the map |
| 729 | * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key | |
| 730 | * @retval zero success | |
| 731 | * @retval non-zero the key was not found | |
| 732 | * | |
| 174 | 733 | * @see cxMapRemoveAndGet() |
| 734 | */ | |
| 735 | #define cxMapRemove(map, key) _Generic((key), \ | |
| 736 | CxHashKey: cx_map_remove, \ | |
| 737 | cxstring: cx_map_remove_cxstr, \ | |
| 738 | cxmutstr: cx_map_remove_mustr, \ | |
| 739 | char*: cx_map_remove_str, \ | |
| 324 | 740 | const char*: cx_map_remove_str) \ |
| 174 | 741 | (map, key) |
| 742 | ||
| 743 | /** | |
| 440 | 744 | * @copydoc cxMapRemoveAndGet() |
| 174 | 745 | */ |
| 440 | 746 | cx_attr_nonnull |
| 747 | cx_attr_access_w(3) | |
| 748 | static inline int cx_map_remove_and_get( | |
| 174 | 749 | CxMap *map, |
| 440 | 750 | CxHashKey key, |
| 751 | void *targetbuf | |
| 174 | 752 | ) { |
| 440 | 753 | return map->cl->remove(map, key, targetbuf); |
| 174 | 754 | } |
| 755 | ||
| 756 | /** | |
| 440 | 757 | * @copydoc cxMapRemoveAndGet() |
| 174 | 758 | */ |
| 440 | 759 | cx_attr_nonnull |
| 760 | cx_attr_access_w(3) | |
| 761 | static inline int cx_map_remove_and_get_cxstr( | |
| 174 | 762 | CxMap *map, |
| 440 | 763 | cxstring key, |
| 764 | void *targetbuf | |
| 174 | 765 | ) { |
| 440 | 766 | return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf); |
| 174 | 767 | } |
| 768 | ||
| 769 | /** | |
| 440 | 770 | * @copydoc cxMapRemoveAndGet() |
| 174 | 771 | */ |
| 440 | 772 | cx_attr_nonnull |
| 773 | cx_attr_access_w(3) | |
| 774 | static inline int cx_map_remove_and_get_mustr( | |
| 775 | CxMap *map, | |
| 776 | cxmutstr key, | |
| 777 | void *targetbuf | |
| 778 | ) { | |
| 779 | return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf); | |
| 780 | } | |
| 174 | 781 | |
| 782 | /** | |
| 440 | 783 | * @copydoc cxMapRemoveAndGet() |
| 174 | 784 | */ |
| 440 | 785 | cx_attr_nonnull |
| 786 | cx_attr_access_w(3) | |
| 787 | cx_attr_cstr_arg(2) | |
| 788 | static inline int cx_map_remove_and_get_str( | |
| 174 | 789 | CxMap *map, |
| 440 | 790 | const char *key, |
| 791 | void *targetbuf | |
| 174 | 792 | ) { |
| 440 | 793 | return map->cl->remove(map, cx_hash_key_str(key), targetbuf); |
| 174 | 794 | } |
| 795 | ||
| 796 | /** | |
| 797 | * Removes a key/value-pair from the map by using the key. | |
| 798 | * | |
| 440 | 799 | * 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
|
800 | * to the target buffer, which must be guaranteed to be large enough |
| 440 | 801 | * to hold the element (the map's element size). |
| 802 | * The destructor functions, if any, will @em not be called. | |
| 174 | 803 | * |
| 440 | 804 | * If this map is storing pointers, the element is the pointer itself |
| 805 | * and not the object it points to. | |
| 174 | 806 | * |
| 440 | 807 | * @param map (@c CxMap*) the map |
| 808 | * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key | |
| 809 | * @param targetbuf (@c void*) the buffer where the element shall be copied to | |
| 810 | * @retval zero success | |
| 811 | * @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
|
812 | * |
| 440 | 813 | * @see cxMapRemove() |
| 174 | 814 | */ |
| 440 | 815 | #define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \ |
| 174 | 816 | CxHashKey: cx_map_remove_and_get, \ |
| 817 | cxstring: cx_map_remove_and_get_cxstr, \ | |
| 818 | cxmutstr: cx_map_remove_and_get_mustr, \ | |
| 819 | char*: cx_map_remove_and_get_str, \ | |
| 324 | 820 | const char*: cx_map_remove_and_get_str) \ |
| 440 | 821 | (map, key, targetbuf) |
| 174 | 822 | |
| 823 | #endif // __cplusplus | |
| 824 | ||
| 825 | #endif // UCX_MAP_H |