UNIXworkcode

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