src/cx/iterator.h

changeset 1429
6e0c3a8a914a
parent 1426
3a89b31f0724
equal deleted inserted replaced
1428:0ac4aa1737fd 1429:6e0c3a8a914a
60 60
61 /** 61 /**
62 * Original implementation in case the function needs to be wrapped. 62 * Original implementation in case the function needs to be wrapped.
63 */ 63 */
64 void *(*current_impl)(const void *); 64 void *(*current_impl)(const void *);
65
66 /** 65 /**
67 * Advances the iterator. 66 * Advances the iterator.
68 * 67 *
69 * When valid returns false, the behavior of this function is undefined. 68 * When valid returns false, the behavior of this function is undefined.
70 */ 69 */
74 */ 73 */
75 void (*next_impl)(void *); 74 void (*next_impl)(void *);
76 /** 75 /**
77 * Indicates whether this iterator may remove elements. 76 * Indicates whether this iterator may remove elements.
78 */ 77 */
79 bool mutating; 78 bool allow_remove;
80 /** 79 /**
81 * Internal flag for removing the current element when advancing. 80 * Internal flag for removing the current element when advancing.
82 */ 81 */
83 bool remove; 82 bool remove;
84 }; 83 };
110 void *elem_handle; 109 void *elem_handle;
111 110
112 /** 111 /**
113 * Handle for the source collection, if any. 112 * Handle for the source collection, if any.
114 */ 113 */
115 union { 114 void *src_handle;
116 /**
117 * Access for mutating iterators.
118 */
119 void *m;
120 /**
121 * Access for normal iterators.
122 */
123 const void *c;
124 } src_handle;
125 115
126 /** 116 /**
127 * If the iterator is position-aware, contains the index of the element in the underlying collection. 117 * If the iterator is position-aware, contains the index of the element in the underlying collection.
128 * Otherwise, this field is usually uninitialized. 118 * Otherwise, this field is usually uninitialized.
129 */ 119 */
147 * An iterator points to a certain element in a (possibly unbounded) chain of elements. 137 * An iterator points to a certain element in a (possibly unbounded) chain of elements.
148 * Iterators that are based on collections (which have a defined "first" element) are supposed 138 * Iterators that are based on collections (which have a defined "first" element) are supposed
149 * to be "position-aware", which means that they keep track of the current index within the collection. 139 * to be "position-aware", which means that they keep track of the current index within the collection.
150 * 140 *
151 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, 141 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
152 * any concurrent mutation of the collection other than by this iterator makes this iterator invalid, 142 * any concurrent mutation of the collection other than by this iterator makes this iterator obsolete,
153 * and it must not be used anymore. 143 * and it must not be used anymore.
154 */ 144 */
155 typedef struct cx_iterator_s CxIterator; 145 typedef struct cx_iterator_s CxIterator;
156 146
157 /** 147 /**
180 * @param iter the iterator 170 * @param iter the iterator
181 */ 171 */
182 #define cxIteratorNext(iter) (iter).base.next(&iter) 172 #define cxIteratorNext(iter) (iter).base.next(&iter)
183 173
184 /** 174 /**
185 * Flags the current element for removal if this iterator is mutating. 175 * Flags the current element for removal if the iterator allows it.
186 * 176 *
187 * Does nothing for non-mutating iterators. 177 * @param iter the iterator
188 * 178 * @return @c true if removal is allowed, @c false otherwise
189 * @param iter the iterator 179 */
190 */ 180 #define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove)
191 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
192 181
193 /** 182 /**
194 * Obtains a reference to an arbitrary iterator. 183 * Obtains a reference to an arbitrary iterator.
195 * 184 *
196 * This is useful for APIs that expect some iterator as an argument. 185 * This is useful for APIs that expect some iterator as an argument.
220 * This iterator yields the addresses of the array elements. 209 * This iterator yields the addresses of the array elements.
221 * If you want to iterator over an array of pointers, you can 210 * If you want to iterator over an array of pointers, you can
222 * use cxIteratorPtr() to create an iterator which directly 211 * use cxIteratorPtr() to create an iterator which directly
223 * yields the stored pointers. 212 * yields the stored pointers.
224 * 213 *
225 * @param array a pointer to the array (can be @c NULL)
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
229 * @see cxIteratorPtr()
230 */
231 cx_attr_nodiscard
232 CX_EXPORT CxIterator cxIterator(const void *array,
233 size_t elem_size, size_t elem_count);
234
235 /**
236 * Creates a mutating iterator for the specified plain array.
237 *
238 * While the iterator is in use, the array may only be altered by removing 214 * While the iterator is in use, the array may only be altered by removing
239 * elements through #cxIteratorFlagRemoval(). Every other change to the array 215 * elements through #cxIteratorFlagRemoval(). Every other change to the array
240 * will bring this iterator to an undefined state. 216 * will bring this iterator to an undefined state.
241 * 217 *
242 * When @p remove_keeps_order is set to @c false, removing an element will only 218 * When @p remove_keeps_order is set to @c false, removing an element will only
243 * move the last element to the position of the removed element, instead of 219 * move the last element to the position of the removed element, instead of
244 * moving all subsequent elements by one. Usually, when the order of elements is 220 * moving all subsequent elements by one. Usually, when the order of elements is
245 * not important, this parameter should be set to @c false. 221 * not important, this parameter should be set to @c false.
246 *
247 * The @p array can be @c NULL, in which case the iterator will be immediately
248 * initialized such that #cxIteratorValid() returns @c false.
249 *
250 * 222 *
251 * @param array a pointer to the array (can be @c NULL) 223 * @param array a pointer to the array (can be @c NULL)
252 * @param elem_size the size of one array element 224 * @param elem_size the size of one array element
253 * @param elem_count the number of elements in the array 225 * @param elem_count the number of elements in the array
254 * @param remove_keeps_order @c true if the order of elements must be preserved 226 * @param remove_keeps_order @c true if the order of elements must be preserved
255 * when removing an element 227 * when removing an element
256 * @return an iterator for the specified array 228 * @return an iterator for the specified array
229 * @see cxIteratorPtr()
257 */ 230 */
258 cx_attr_nodiscard 231 cx_attr_nodiscard
259 CX_EXPORT CxIterator cxMutIterator(void *array, 232 CX_EXPORT CxIterator cxIterator(const void *array,
260 size_t elem_size, size_t elem_count, bool remove_keeps_order); 233 size_t elem_size, size_t elem_count, bool remove_keeps_order);
261 234
262 /** 235 /**
263 * Creates an iterator for the specified plain pointer array. 236 * Creates an iterator for the specified plain pointer array.
264 * 237 *
265 * This iterator assumes that every element in the array is a pointer 238 * This iterator assumes that every element in the array is a pointer
266 * and yields exactly those pointers during iteration (on the other 239 * and yields exactly those pointers during iteration (on the other
267 * hand, an iterator created with cxIterator() would return the 240 * hand, an iterator created with cxIterator() would return the
268 * addresses of those pointers within the array). 241 * addresses of those pointers within the array).
269 * 242 *
270 * @param array a pointer to the array (can be @c NULL) 243 * While the iterator is in use, the array may only be altered by removing
271 * @param elem_count the number of elements in the array 244 * elements through #cxIteratorFlagRemoval(). Every other change to the array
272 * @return an iterator for the specified array 245 * will bring this iterator to an undefined state.
273 * @see cxIterator() 246 *
274 */ 247 * When @p remove_keeps_order is set to @c false, removing an element will only
275 cx_attr_nodiscard 248 * move the last element to the position of the removed element, instead of
276 CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count); 249 * moving all subsequent elements by one. Usually, when the order of elements is
277 250 * not important, this parameter should be set to @c false.
278 /**
279 * Creates a mutating iterator for the specified plain pointer array.
280 *
281 * This is the mutating variant of cxIteratorPtr(). See also
282 * cxMutIterator().
283 * 251 *
284 * @param array a pointer to the array (can be @c NULL) 252 * @param array a pointer to the array (can be @c NULL)
285 * @param elem_count the number of elements in the array 253 * @param elem_count the number of elements in the array
286 * @param remove_keeps_order @c true if the order of elements must be preserved 254 * @param remove_keeps_order @c true if the order of elements must be preserved
287 * when removing an element 255 * when removing an element
288 * @return an iterator for the specified array 256 * @return an iterator for the specified array
289 * @see cxMutIterator() 257 * @see cxIterator()
290 * @see cxIteratorPtr()
291 */ 258 */
292 cx_attr_nodiscard 259 cx_attr_nodiscard
293 CX_EXPORT CxIterator cxMutIteratorPtr(void *array, 260 CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count,
294 size_t elem_count, bool remove_keeps_order); 261 bool remove_keeps_order);
295 262
296 #ifdef __cplusplus 263 #ifdef __cplusplus
297 } // extern "C" 264 } // extern "C"
298 #endif 265 #endif
299 266

mercurial