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 iterator.h |
| 30 | * @brief Interface for iterator implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 34 | */ |
| 35 | ||
| 36 | #ifndef UCX_ITERATOR_H | |
| 37 | #define UCX_ITERATOR_H | |
| 38 | ||
| 39 | #include "common.h" | |
| 40 | ||
| 440 | 41 | #ifdef __cplusplus |
| 42 | extern "C" { | |
| 43 | #endif | |
| 44 | ||
| 45 | /** | |
| 46 | * Common data for all iterators. | |
| 47 | */ | |
| 174 | 48 | struct cx_iterator_base_s { |
| 49 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
50 | * True if the iterator points to valid data. |
| 174 | 51 | */ |
| 324 | 52 | bool (*valid)(const void *); |
| 174 | 53 | |
| 54 | /** | |
| 55 | * Returns a pointer to the current element. | |
| 56 | * | |
| 57 | * When valid returns false, the behavior of this function is undefined. | |
| 58 | */ | |
| 324 | 59 | void *(*current)(const void *); |
| 174 | 60 | |
| 61 | /** | |
| 62 | * Original implementation in case the function needs to be wrapped. | |
| 63 | */ | |
| 324 | 64 | void *(*current_impl)(const void *); |
| 174 | 65 | |
| 66 | /** | |
| 67 | * Advances the iterator. | |
| 68 | * | |
| 69 | * When valid returns false, the behavior of this function is undefined. | |
| 70 | */ | |
| 71 | void (*next)(void *); | |
| 72 | /** | |
| 845 | 73 | * Original implementation in case the function needs to be wrapped. |
| 74 | */ | |
| 75 | void (*next_impl)(void *); | |
| 76 | /** | |
| 174 | 77 | * Indicates whether this iterator may remove elements. |
| 78 | */ | |
| 79 | bool mutating; | |
| 80 | /** | |
| 81 | * Internal flag for removing the current element when advancing. | |
| 82 | */ | |
| 83 | bool remove; | |
| 84 | }; | |
| 85 | ||
| 86 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
87 | * Convenience type definition for the base structure of an iterator. |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
88 | * @see #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
|
89 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
90 | typedef struct cx_iterator_base_s CxIteratorBase; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
91 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
92 | /** |
| 324 | 93 | * Declares base attributes for an iterator. |
| 94 | * Must be the first member of an iterator structure. | |
| 174 | 95 | */ |
| 324 | 96 | #define CX_ITERATOR_BASE struct cx_iterator_base_s base |
| 97 | ||
| 98 | /** | |
| 99 | * Internal iterator struct - use CxIterator. | |
| 100 | */ | |
| 101 | struct cx_iterator_s { | |
| 440 | 102 | /** |
| 103 | * Inherited common data for all iterators. | |
| 104 | */ | |
| 324 | 105 | CX_ITERATOR_BASE; |
| 174 | 106 | |
| 107 | /** | |
| 324 | 108 | * Handle for the current element. |
| 174 | 109 | */ |
| 110 | void *elem_handle; | |
| 111 | ||
| 112 | /** | |
| 113 | * Handle for the source collection, if any. | |
| 114 | */ | |
| 324 | 115 | union { |
| 116 | /** | |
| 117 | * Access for mutating iterators. | |
| 118 | */ | |
| 119 | void *m; | |
| 120 | /** | |
| 121 | * Access for normal iterators. | |
| 122 | */ | |
| 123 | const void *c; | |
| 124 | } src_handle; | |
| 174 | 125 | |
| 126 | /** | |
| 127 | * If the iterator is position-aware, contains the index of the element in the underlying collection. | |
| 128 | * Otherwise, this field is usually uninitialized. | |
| 129 | */ | |
| 130 | size_t index; | |
| 324 | 131 | |
| 132 | /** | |
| 133 | * The size of an individual element. | |
| 134 | */ | |
| 135 | size_t elem_size; | |
| 136 | ||
| 137 | /** | |
| 138 | * May contain the total number of elements, if known. | |
| 440 | 139 | * Shall be set to @c SIZE_MAX when the total number is unknown during iteration. |
| 324 | 140 | */ |
| 141 | size_t elem_count; | |
| 174 | 142 | }; |
| 143 | ||
| 144 | /** | |
| 324 | 145 | * Iterator type. |
| 174 | 146 | * |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
147 | * An iterator points to a certain element in a (possibly unbounded) chain of elements. |
| 845 | 148 | * Iterators that are based on collections (which have a defined "first" element) are supposed |
| 174 | 149 | * to be "position-aware", which means that they keep track of the current index within the collection. |
| 150 | * | |
| 151 | * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, | |
| 440 | 152 | * any concurrent mutation of the collection other than by this iterator makes this iterator invalid, |
| 324 | 153 | * and it must not be used anymore. |
| 174 | 154 | */ |
| 155 | typedef struct cx_iterator_s CxIterator; | |
| 156 | ||
| 157 | /** | |
| 158 | * Checks if the iterator points to valid data. | |
| 159 | * | |
| 160 | * @param iter the iterator | |
| 440 | 161 | * @retval true if the iterator points to valid data |
| 162 | * @retval false if the iterator already moved past the end | |
| 174 | 163 | */ |
| 164 | #define cxIteratorValid(iter) (iter).base.valid(&(iter)) | |
| 165 | ||
| 166 | /** | |
| 167 | * Returns a pointer to the current element. | |
| 168 | * | |
| 169 | * The behavior is undefined if this iterator is invalid. | |
| 170 | * | |
| 171 | * @param iter the iterator | |
| 172 | * @return a pointer to the current element | |
| 440 | 173 | * @see cxIteratorValid() |
| 174 | 174 | */ |
| 175 | #define cxIteratorCurrent(iter) (iter).base.current(&iter) | |
| 176 | ||
| 177 | /** | |
| 178 | * Advances the iterator to the next element. | |
| 179 | * | |
| 180 | * @param iter the iterator | |
| 181 | */ | |
| 182 | #define cxIteratorNext(iter) (iter).base.next(&iter) | |
| 183 | ||
| 184 | /** | |
| 845 | 185 | * Flags the current element for removal if this iterator is mutating. |
| 174 | 186 | * |
| 440 | 187 | * Does nothing for non-mutating iterators. |
| 188 | * | |
| 174 | 189 | * @param iter the iterator |
| 190 | */ | |
| 324 | 191 | #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating |
| 192 | ||
| 193 | /** | |
| 194 | * Obtains a reference to an arbitrary iterator. | |
| 195 | * | |
| 196 | * This is useful for APIs that expect some iterator as an argument. | |
| 197 | * | |
| 198 | * @param iter the iterator | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
199 | * @return (@c struct @c cx_iterator_base_s*) a pointer to the iterator |
| 324 | 200 | */ |
| 201 | #define cxIteratorRef(iter) &((iter).base) | |
| 174 | 202 | |
| 203 | /** | |
| 204 | * Loops over an iterator. | |
| 440 | 205 | * |
| 174 | 206 | * @param type the type of the elements |
| 207 | * @param elem the name of the iteration variable | |
| 208 | * @param iter the iterator | |
| 209 | */ | |
| 210 | #define cx_foreach(type, elem, iter) \ | |
| 211 | for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter)) | |
| 212 | ||
| 324 | 213 | |
| 214 | /** | |
| 215 | * Creates an iterator for the specified plain array. | |
| 216 | * | |
| 845 | 217 | * The @p array can be @c NULL, in which case the iterator will be immediately |
| 440 | 218 | * initialized such that #cxIteratorValid() returns @c false. |
| 324 | 219 | * |
| 440 | 220 | * This iterator yields the addresses of the array elements. |
| 221 | * If you want to iterator over an array of pointers, you can | |
| 222 | * use cxIteratorPtr() to create an iterator which directly | |
| 223 | * yields the stored pointers. | |
| 324 | 224 | * |
| 440 | 225 | * @param array a pointer to the array (can be @c NULL) |
| 324 | 226 | * @param elem_size the size of one array element |
| 227 | * @param elem_count the number of elements in the array | |
| 228 | * @return an iterator for the specified array | |
| 440 | 229 | * @see cxIteratorPtr() |
| 324 | 230 | */ |
| 440 | 231 | 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
|
232 | cx_attr_export |
| 324 | 233 | CxIterator cxIterator( |
| 234 | const void *array, | |
| 235 | size_t elem_size, | |
| 236 | size_t elem_count | |
| 237 | ); | |
| 238 | ||
| 239 | /** | |
| 240 | * Creates a mutating iterator for the specified plain array. | |
| 241 | * | |
| 242 | * While the iterator is in use, the array may only be altered by removing | |
| 243 | * elements through #cxIteratorFlagRemoval(). Every other change to the array | |
| 244 | * will bring this iterator to an undefined state. | |
| 245 | * | |
| 440 | 246 | * When @p remove_keeps_order is set to @c false, removing an element will only |
| 324 | 247 | * move the last element to the position of the removed element, instead of |
| 248 | * moving all subsequent elements by one. Usually, when the order of elements is | |
| 440 | 249 | * not important, this parameter should be set to @c false. |
| 324 | 250 | * |
| 845 | 251 | * The @p array can be @c NULL, in which case the iterator will be immediately |
| 440 | 252 | * initialized such that #cxIteratorValid() returns @c false. |
| 324 | 253 | * |
| 254 | * | |
| 440 | 255 | * @param array a pointer to the array (can be @c NULL) |
| 324 | 256 | * @param elem_size the size of one array element |
| 257 | * @param elem_count the number of elements in the array | |
| 440 | 258 | * @param remove_keeps_order @c true if the order of elements must be preserved |
| 324 | 259 | * when removing an element |
| 260 | * @return an iterator for the specified array | |
| 261 | */ | |
| 440 | 262 | 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
|
263 | cx_attr_export |
| 324 | 264 | CxIterator cxMutIterator( |
| 265 | void *array, | |
| 266 | size_t elem_size, | |
| 267 | size_t elem_count, | |
| 268 | bool remove_keeps_order | |
| 269 | ); | |
| 270 | ||
| 440 | 271 | /** |
| 272 | * Creates an iterator for the specified plain pointer array. | |
| 273 | * | |
| 274 | * This iterator assumes that every element in the array is a pointer | |
| 845 | 275 | * and yields exactly those pointers during iteration (on the other |
| 276 | * hand, an iterator created with cxIterator() would return the | |
| 277 | * addresses of those pointers within the array). | |
| 440 | 278 | * |
| 279 | * @param array a pointer to the array (can be @c NULL) | |
| 280 | * @param elem_count the number of elements in the array | |
| 281 | * @return an iterator for the specified array | |
| 282 | * @see cxIterator() | |
| 283 | */ | |
| 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 | cx_attr_export |
| 440 | 286 | CxIterator cxIteratorPtr( |
| 287 | const void *array, | |
| 288 | size_t elem_count | |
| 289 | ); | |
| 290 | ||
| 291 | /** | |
| 292 | * Creates a mutating iterator for the specified plain pointer array. | |
| 293 | * | |
| 294 | * This is the mutating variant of cxIteratorPtr(). See also | |
| 295 | * cxMutIterator(). | |
| 296 | * | |
| 297 | * @param array a pointer to the array (can be @c NULL) | |
| 298 | * @param elem_count the number of elements in the array | |
| 299 | * @param remove_keeps_order @c true if the order of elements must be preserved | |
| 300 | * when removing an element | |
| 301 | * @return an iterator for the specified array | |
| 302 | * @see cxMutIterator() | |
| 303 | * @see cxIteratorPtr() | |
| 304 | */ | |
| 305 | 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
|
306 | cx_attr_export |
| 440 | 307 | CxIterator cxMutIteratorPtr( |
| 308 | void *array, | |
| 309 | size_t elem_count, | |
| 310 | bool remove_keeps_order | |
| 311 | ); | |
| 312 | ||
| 313 | #ifdef __cplusplus | |
| 314 | } // extern "C" | |
| 315 | #endif | |
| 316 | ||
| 174 | 317 | #endif // UCX_ITERATOR_H |