Wed, 31 Dec 2025 16:40:12 +0100
update ucx to version 4.0
| 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 | /** |
| 42 | * Common data for all iterators. | |
| 43 | */ | |
| 174 | 44 | struct cx_iterator_base_s { |
| 45 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
46 | * True if the iterator points to valid data. |
| 174 | 47 | */ |
| 324 | 48 | bool (*valid)(const void *); |
| 870 | 49 | /** |
| 50 | * Original implementation in case the function needs to be wrapped. | |
| 51 | */ | |
| 52 | bool (*valid_impl)(const void *); | |
| 174 | 53 | /** |
| 54 | * Returns a pointer to the current element. | |
| 55 | * | |
| 56 | * When valid returns false, the behavior of this function is undefined. | |
| 57 | */ | |
| 324 | 58 | void *(*current)(const void *); |
| 174 | 59 | |
| 60 | /** | |
| 61 | * Original implementation in case the function needs to be wrapped. | |
| 62 | */ | |
| 324 | 63 | void *(*current_impl)(const void *); |
| 174 | 64 | /** |
| 65 | * Advances the iterator. | |
| 66 | * | |
| 67 | * When valid returns false, the behavior of this function is undefined. | |
| 68 | */ | |
| 69 | void (*next)(void *); | |
| 70 | /** | |
| 845 | 71 | * Original implementation in case the function needs to be wrapped. |
| 72 | */ | |
| 73 | void (*next_impl)(void *); | |
| 74 | /** | |
| 174 | 75 | * Indicates whether this iterator may remove elements. |
| 76 | */ | |
| 870 | 77 | bool allow_remove; |
| 174 | 78 | /** |
| 79 | * Internal flag for removing the current element when advancing. | |
| 80 | */ | |
| 81 | bool remove; | |
| 82 | }; | |
| 83 | ||
| 84 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
85 | * 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
|
86 | * @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
|
87 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
88 | 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
|
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 | /** |
| 324 | 91 | * Declares base attributes for an iterator. |
| 92 | * Must be the first member of an iterator structure. | |
| 174 | 93 | */ |
| 324 | 94 | #define CX_ITERATOR_BASE struct cx_iterator_base_s base |
| 95 | ||
| 96 | /** | |
| 97 | * Internal iterator struct - use CxIterator. | |
| 98 | */ | |
| 99 | struct cx_iterator_s { | |
| 440 | 100 | /** |
| 101 | * Inherited common data for all iterators. | |
| 102 | */ | |
| 324 | 103 | CX_ITERATOR_BASE; |
| 174 | 104 | |
| 105 | /** | |
| 324 | 106 | * Handle for the current element. |
| 174 | 107 | */ |
| 108 | void *elem_handle; | |
| 109 | ||
| 110 | /** | |
| 111 | * Handle for the source collection, if any. | |
| 112 | */ | |
| 870 | 113 | void *src_handle; |
| 174 | 114 | |
| 115 | /** | |
| 116 | * If the iterator is position-aware, contains the index of the element in the underlying collection. | |
| 117 | * Otherwise, this field is usually uninitialized. | |
| 118 | */ | |
| 119 | size_t index; | |
| 324 | 120 | |
| 121 | /** | |
| 122 | * The size of an individual element. | |
| 123 | */ | |
| 124 | size_t elem_size; | |
| 125 | ||
| 126 | /** | |
| 127 | * May contain the total number of elements, if known. | |
| 440 | 128 | * Shall be set to @c SIZE_MAX when the total number is unknown during iteration. |
| 324 | 129 | */ |
| 130 | size_t elem_count; | |
| 174 | 131 | }; |
| 132 | ||
| 133 | /** | |
| 324 | 134 | * Iterator type. |
| 174 | 135 | * |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
136 | * An iterator points to a certain element in a (possibly unbounded) chain of elements. |
| 845 | 137 | * Iterators that are based on collections (which have a defined "first" element) are supposed |
| 174 | 138 | * to be "position-aware", which means that they keep track of the current index within the collection. |
| 139 | * | |
| 140 | * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, | |
| 870 | 141 | * any concurrent mutation of the collection other than by this iterator makes this iterator obsolete, |
| 324 | 142 | * and it must not be used anymore. |
| 174 | 143 | */ |
| 144 | typedef struct cx_iterator_s CxIterator; | |
| 145 | ||
| 146 | /** | |
| 147 | * Checks if the iterator points to valid data. | |
| 148 | * | |
| 149 | * @param iter the iterator | |
| 440 | 150 | * @retval true if the iterator points to valid data |
| 151 | * @retval false if the iterator already moved past the end | |
| 174 | 152 | */ |
| 153 | #define cxIteratorValid(iter) (iter).base.valid(&(iter)) | |
| 154 | ||
| 155 | /** | |
| 156 | * Returns a pointer to the current element. | |
| 157 | * | |
| 158 | * The behavior is undefined if this iterator is invalid. | |
| 159 | * | |
| 160 | * @param iter the iterator | |
| 161 | * @return a pointer to the current element | |
| 440 | 162 | * @see cxIteratorValid() |
| 174 | 163 | */ |
| 164 | #define cxIteratorCurrent(iter) (iter).base.current(&iter) | |
| 165 | ||
| 166 | /** | |
| 167 | * Advances the iterator to the next element. | |
| 168 | * | |
| 169 | * @param iter the iterator | |
| 170 | */ | |
| 171 | #define cxIteratorNext(iter) (iter).base.next(&iter) | |
| 172 | ||
| 173 | /** | |
| 870 | 174 | * Flags the current element for removal if the iterator allows it. |
| 440 | 175 | * |
| 174 | 176 | * @param iter the iterator |
| 870 | 177 | * @return @c true if removal is allowed, @c false otherwise |
| 174 | 178 | */ |
| 870 | 179 | #define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove) |
| 324 | 180 | |
| 181 | /** | |
| 174 | 182 | * Loops over an iterator. |
| 440 | 183 | * |
| 174 | 184 | * @param type the type of the elements |
| 185 | * @param elem the name of the iteration variable | |
| 186 | * @param iter the iterator | |
| 187 | */ | |
| 188 | #define cx_foreach(type, elem, iter) \ | |
| 189 | for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter)) | |
| 190 | ||
| 324 | 191 | |
| 192 | /** | |
| 193 | * Creates an iterator for the specified plain array. | |
| 194 | * | |
| 845 | 195 | * The @p array can be @c NULL, in which case the iterator will be immediately |
| 440 | 196 | * initialized such that #cxIteratorValid() returns @c false. |
| 324 | 197 | * |
| 440 | 198 | * This iterator yields the addresses of the array elements. |
| 199 | * If you want to iterator over an array of pointers, you can | |
| 200 | * use cxIteratorPtr() to create an iterator which directly | |
| 201 | * yields the stored pointers. | |
| 324 | 202 | * |
| 440 | 203 | * @param array a pointer to the array (can be @c NULL) |
| 324 | 204 | * @param elem_size the size of one array element |
| 205 | * @param elem_count the number of elements in the array | |
| 206 | * @return an iterator for the specified array | |
| 440 | 207 | * @see cxIteratorPtr() |
| 324 | 208 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
209 | CX_EXTERN CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
210 | CxIterator cxIterator(const void *array, |
| 1016 | 211 | size_t elem_size, size_t elem_count); |
| 324 | 212 | |
| 213 | /** | |
| 870 | 214 | * Creates an iterator for the specified plain pointer array. |
| 215 | * | |
| 216 | * This iterator assumes that every element in the array is a pointer | |
| 217 | * and yields exactly those pointers during iteration (on the other | |
| 218 | * hand, an iterator created with cxIterator() would return the | |
| 219 | * addresses of those pointers within the array). | |
| 324 | 220 | * |
| 440 | 221 | * @param array a pointer to the array (can be @c NULL) |
| 222 | * @param elem_count the number of elements in the array | |
| 223 | * @return an iterator for the specified array | |
| 870 | 224 | * @see cxIterator() |
| 440 | 225 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
226 | CX_EXTERN CX_NODISCARD |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
227 | CxIterator cxIteratorPtr(const void *array, size_t elem_count); |
| 440 | 228 | |
| 174 | 229 | #endif // UCX_ITERATOR_H |