ucx/cx/iterator.h

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

mercurial