ucx/cx/iterator.h

changeset 888
af685cc9d623
parent 854
1c8401ece69e
equal deleted inserted replaced
877:b60487c3ec36 888:af685cc9d623
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 */
71 void (*next)(void *); 73 void (*next)(void *);
72 /** 74 /**
75 * Original implementation in case the function needs to be wrapped.
76 */
77 void (*next_impl)(void *);
78 /**
73 * Indicates whether this iterator may remove elements. 79 * Indicates whether this iterator may remove elements.
74 */ 80 */
75 bool mutating; 81 bool allow_remove;
76 /** 82 /**
77 * Internal flag for removing the current element when advancing. 83 * Internal flag for removing the current element when advancing.
78 */ 84 */
79 bool remove; 85 bool remove;
80 }; 86 };
106 void *elem_handle; 112 void *elem_handle;
107 113
108 /** 114 /**
109 * Handle for the source collection, if any. 115 * Handle for the source collection, if any.
110 */ 116 */
111 union { 117 void *src_handle;
112 /**
113 * Access for mutating iterators.
114 */
115 void *m;
116 /**
117 * Access for normal iterators.
118 */
119 const void *c;
120 } src_handle;
121 118
122 /** 119 /**
123 * 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.
124 * Otherwise, this field is usually uninitialized. 121 * Otherwise, this field is usually uninitialized.
125 */ 122 */
139 136
140 /** 137 /**
141 * Iterator type. 138 * Iterator type.
142 * 139 *
143 * 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.
144 * 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
145 * 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.
146 * 143 *
147 * @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,
148 * 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,
149 * and it must not be used anymore. 146 * and it must not be used anymore.
150 */ 147 */
151 typedef struct cx_iterator_s CxIterator; 148 typedef struct cx_iterator_s CxIterator;
152 149
153 /** 150 /**
176 * @param iter the iterator 173 * @param iter the iterator
177 */ 174 */
178 #define cxIteratorNext(iter) (iter).base.next(&iter) 175 #define cxIteratorNext(iter) (iter).base.next(&iter)
179 176
180 /** 177 /**
181 * Flags the current element for removal, if this iterator is mutating. 178 * Flags the current element for removal if the iterator allows it.
182 * 179 *
183 * Does nothing for non-mutating iterators. 180 * @param iter the iterator
184 * 181 * @return @c true if removal is allowed, @c false otherwise
185 * @param iter the iterator 182 */
186 */ 183 #define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove)
187 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
188 184
189 /** 185 /**
190 * Obtains a reference to an arbitrary iterator. 186 * Obtains a reference to an arbitrary iterator.
191 * 187 *
192 * 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.
208 204
209 205
210 /** 206 /**
211 * Creates an iterator for the specified plain array. 207 * Creates an iterator for the specified plain array.
212 * 208 *
213 * The @p array can be @c NULL in which case the iterator will be immediately 209 * The @p array can be @c NULL, in which case the iterator will be immediately
214 * initialized such that #cxIteratorValid() returns @c false. 210 * initialized such that #cxIteratorValid() returns @c false.
215 * 211 *
216 * This iterator yields the addresses of the array elements. 212 * This iterator yields the addresses of the array elements.
217 * 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
218 * use cxIteratorPtr() to create an iterator which directly 214 * use cxIteratorPtr() to create an iterator which directly
219 * yields the stored pointers. 215 * yields the stored pointers.
220 * 216 *
221 * @param array a pointer to the array (can be @c NULL)
222 * @param elem_size the size of one array element
223 * @param elem_count the number of elements in the array
224 * @return an iterator for the specified array
225 * @see cxIteratorPtr()
226 */
227 cx_attr_nodiscard
228 cx_attr_export
229 CxIterator cxIterator(
230 const void *array,
231 size_t elem_size,
232 size_t elem_count
233 );
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 217 * While the iterator is in use, the array may only be altered by removing
239 * elements through #cxIteratorFlagRemoval(). Every other change to the array 218 * elements through #cxIteratorFlagRemoval(). Every other change to the array
240 * will bring this iterator to an undefined state. 219 * will bring this iterator to an undefined state.
241 * 220 *
242 * 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
243 * 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
244 * 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
245 * not important, this parameter should be set to @c false. 224 * 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 * 225 *
251 * @param array a pointer to the array (can be @c NULL) 226 * @param array a pointer to the array (can be @c NULL)
252 * @param elem_size the size of one array element 227 * @param elem_size the size of one array element
253 * @param elem_count the number of elements in the array 228 * @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 229 * @param remove_keeps_order @c true if the order of elements must be preserved
255 * when removing an element 230 * when removing an element
256 * @return an iterator for the specified array 231 * @return an iterator for the specified array
232 * @see cxIteratorPtr()
257 */ 233 */
258 cx_attr_nodiscard 234 cx_attr_nodiscard
259 cx_attr_export 235 CX_EXPORT CxIterator cxIterator(const void *array,
260 CxIterator cxMutIterator( 236 size_t elem_size, size_t elem_count, bool remove_keeps_order);
261 void *array,
262 size_t elem_size,
263 size_t elem_count,
264 bool remove_keeps_order
265 );
266 237
267 /** 238 /**
268 * Creates an iterator for the specified plain pointer array. 239 * Creates an iterator for the specified plain pointer array.
269 * 240 *
270 * 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
271 * and yields exactly those pointers during iteration (while in contrast 242 * and yields exactly those pointers during iteration (on the other
272 * an iterator created with cxIterator() would return the addresses 243 * hand, an iterator created with cxIterator() would return the
273 * of those pointers within the array). 244 * addresses of those pointers within the array).
274 * 245 *
275 * @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
276 * @param elem_count the number of elements in the array 247 * elements through #cxIteratorFlagRemoval(). Every other change to the array
277 * @return an iterator for the specified array 248 * will bring this iterator to an undefined state.
278 * @see cxIterator() 249 *
279 */ 250 * When @p remove_keeps_order is set to @c false, removing an element will only
280 cx_attr_nodiscard 251 * move the last element to the position of the removed element, instead of
281 cx_attr_export 252 * moving all subsequent elements by one. Usually, when the order of elements is
282 CxIterator cxIteratorPtr( 253 * not important, this parameter should be set to @c false.
283 const void *array,
284 size_t elem_count
285 );
286
287 /**
288 * Creates a mutating iterator for the specified plain pointer array.
289 *
290 * This is the mutating variant of cxIteratorPtr(). See also
291 * cxMutIterator().
292 * 254 *
293 * @param array a pointer to the array (can be @c NULL) 255 * @param array a pointer to the array (can be @c NULL)
294 * @param elem_count the number of elements in the array 256 * @param elem_count the number of elements in the array
295 * @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
296 * when removing an element 258 * when removing an element
297 * @return an iterator for the specified array 259 * @return an iterator for the specified array
298 * @see cxMutIterator() 260 * @see cxIterator()
299 * @see cxIteratorPtr()
300 */ 261 */
301 cx_attr_nodiscard 262 cx_attr_nodiscard
302 cx_attr_export 263 CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count,
303 CxIterator cxMutIteratorPtr( 264 bool remove_keeps_order);
304 void *array,
305 size_t elem_count,
306 bool remove_keeps_order
307 );
308 265
309 #ifdef __cplusplus 266 #ifdef __cplusplus
310 } // extern "C" 267 } // extern "C"
311 #endif 268 #endif
312 269

mercurial