ucx/cx/iterator.h

changeset 440
7c4b9cba09ca
parent 324
ce13a778654a
equal deleted inserted replaced
439:bf7084544cb1 440:7c4b9cba09ca
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file iterator.h 29 * @file iterator.h
30 * \brief Interface for iterator implementations. 30 * @brief Interface for iterator implementations.
31 * \author Mike Becker 31 * @author Mike Becker
32 * \author Olaf Wintermann 32 * @author Olaf Wintermann
33 * \copyright 2-Clause BSD License 33 * @copyright 2-Clause BSD License
34 */ 34 */
35 35
36 #ifndef UCX_ITERATOR_H 36 #ifndef UCX_ITERATOR_H
37 #define UCX_ITERATOR_H 37 #define UCX_ITERATOR_H
38 38
39 #include "common.h" 39 #include "common.h"
40 40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /**
46 * Common data for all iterators.
47 */
41 struct cx_iterator_base_s { 48 struct cx_iterator_base_s {
42 /** 49 /**
43 * True iff the iterator points to valid data. 50 * True iff the iterator points to valid data.
44 */ 51 */
45 __attribute__ ((__nonnull__)) 52 cx_attr_nonnull
46 bool (*valid)(const void *); 53 bool (*valid)(const void *);
47 54
48 /** 55 /**
49 * Returns a pointer to the current element. 56 * Returns a pointer to the current element.
50 * 57 *
51 * When valid returns false, the behavior of this function is undefined. 58 * When valid returns false, the behavior of this function is undefined.
52 */ 59 */
53 __attribute__ ((__nonnull__)) 60 cx_attr_nonnull
61 cx_attr_nodiscard
54 void *(*current)(const void *); 62 void *(*current)(const void *);
55 63
56 /** 64 /**
57 * Original implementation in case the function needs to be wrapped. 65 * Original implementation in case the function needs to be wrapped.
58 */ 66 */
59 __attribute__ ((__nonnull__)) 67 cx_attr_nonnull
68 cx_attr_nodiscard
60 void *(*current_impl)(const void *); 69 void *(*current_impl)(const void *);
61 70
62 /** 71 /**
63 * Advances the iterator. 72 * Advances the iterator.
64 * 73 *
65 * When valid returns false, the behavior of this function is undefined. 74 * When valid returns false, the behavior of this function is undefined.
66 */ 75 */
67 __attribute__ ((__nonnull__)) 76 cx_attr_nonnull
68 void (*next)(void *); 77 void (*next)(void *);
69 /** 78 /**
70 * Indicates whether this iterator may remove elements. 79 * Indicates whether this iterator may remove elements.
71 */ 80 */
72 bool mutating; 81 bool mutating;
84 93
85 /** 94 /**
86 * Internal iterator struct - use CxIterator. 95 * Internal iterator struct - use CxIterator.
87 */ 96 */
88 struct cx_iterator_s { 97 struct cx_iterator_s {
98 /**
99 * Inherited common data for all iterators.
100 */
89 CX_ITERATOR_BASE; 101 CX_ITERATOR_BASE;
90 102
91 /** 103 /**
92 * Handle for the current element. 104 * Handle for the current element.
93 */ 105 */
139 */ 151 */
140 size_t elem_size; 152 size_t elem_size;
141 153
142 /** 154 /**
143 * May contain the total number of elements, if known. 155 * May contain the total number of elements, if known.
144 * Shall be set to \c SIZE_MAX when the total number is unknown during iteration. 156 * Shall be set to @c SIZE_MAX when the total number is unknown during iteration.
145 */ 157 */
146 size_t elem_count; 158 size_t elem_count;
147 }; 159 };
148 160
149 /** 161 /**
152 * An iterator points to a certain element in a (possibly unbounded) chain of elements. 164 * An iterator points to a certain element in a (possibly unbounded) chain of elements.
153 * Iterators that are based on collections (which have a defined "first" element), are supposed 165 * Iterators that are based on collections (which have a defined "first" element), are supposed
154 * to be "position-aware", which means that they keep track of the current index within the collection. 166 * to be "position-aware", which means that they keep track of the current index within the collection.
155 * 167 *
156 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, 168 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
157 * any concurrent mutation of the collection other than by this iterator makes this iterator invalid 169 * any concurrent mutation of the collection other than by this iterator makes this iterator invalid,
158 * and it must not be used anymore. 170 * and it must not be used anymore.
159 */ 171 */
160 typedef struct cx_iterator_s CxIterator; 172 typedef struct cx_iterator_s CxIterator;
161 173
162 /** 174 /**
163 * Checks if the iterator points to valid data. 175 * Checks if the iterator points to valid data.
164 * 176 *
165 * This is especially false for past-the-end iterators. 177 * This is especially false for past-the-end iterators.
166 * 178 *
167 * @param iter the iterator 179 * @param iter the iterator
168 * @return true iff the iterator points to valid data 180 * @retval true if the iterator points to valid data
181 * @retval false if the iterator already moved past the end
169 */ 182 */
170 #define cxIteratorValid(iter) (iter).base.valid(&(iter)) 183 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
171 184
172 /** 185 /**
173 * Returns a pointer to the current element. 186 * Returns a pointer to the current element.
174 * 187 *
175 * The behavior is undefined if this iterator is invalid. 188 * The behavior is undefined if this iterator is invalid.
176 * 189 *
177 * @param iter the iterator 190 * @param iter the iterator
178 * @return a pointer to the current element 191 * @return a pointer to the current element
192 * @see cxIteratorValid()
179 */ 193 */
180 #define cxIteratorCurrent(iter) (iter).base.current(&iter) 194 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
181 195
182 /** 196 /**
183 * Advances the iterator to the next element. 197 * Advances the iterator to the next element.
187 #define cxIteratorNext(iter) (iter).base.next(&iter) 201 #define cxIteratorNext(iter) (iter).base.next(&iter)
188 202
189 /** 203 /**
190 * Flags the current element for removal, if this iterator is mutating. 204 * Flags the current element for removal, if this iterator is mutating.
191 * 205 *
206 * Does nothing for non-mutating iterators.
207 *
192 * @param iter the iterator 208 * @param iter the iterator
193 */ 209 */
194 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating 210 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
195 211
196 /** 212 /**
197 * Obtains a reference to an arbitrary iterator. 213 * Obtains a reference to an arbitrary iterator.
198 * 214 *
199 * This is useful for APIs that expect some iterator as an argument. 215 * This is useful for APIs that expect some iterator as an argument.
200 * 216 *
201 * @param iter the iterator 217 * @param iter the iterator
218 * @return (@c CxIterator*) a pointer to the iterator
202 */ 219 */
203 #define cxIteratorRef(iter) &((iter).base) 220 #define cxIteratorRef(iter) &((iter).base)
204 221
205 /** 222 /**
206 * Loops over an iterator. 223 * Loops over an iterator.
224 *
207 * @param type the type of the elements 225 * @param type the type of the elements
208 * @param elem the name of the iteration variable 226 * @param elem the name of the iteration variable
209 * @param iter the iterator 227 * @param iter the iterator
210 */ 228 */
211 #define cx_foreach(type, elem, iter) \ 229 #define cx_foreach(type, elem, iter) \
213 231
214 232
215 /** 233 /**
216 * Creates an iterator for the specified plain array. 234 * Creates an iterator for the specified plain array.
217 * 235 *
218 * The \p array can be \c NULL in which case the iterator will be immediately 236 * The @p array can be @c NULL in which case the iterator will be immediately
219 * initialized such that #cxIteratorValid() returns \c false. 237 * initialized such that #cxIteratorValid() returns @c false.
220 * 238 *
221 * 239 * This iterator yields the addresses of the array elements.
222 * @param array a pointer to the array (can be \c NULL) 240 * If you want to iterator over an array of pointers, you can
241 * use cxIteratorPtr() to create an iterator which directly
242 * yields the stored pointers.
243 *
244 * @param array a pointer to the array (can be @c NULL)
223 * @param elem_size the size of one array element 245 * @param elem_size the size of one array element
224 * @param elem_count the number of elements in the array 246 * @param elem_count the number of elements in the array
225 * @return an iterator for the specified array 247 * @return an iterator for the specified array
226 */ 248 * @see cxIteratorPtr()
227 __attribute__((__warn_unused_result__)) 249 */
250 cx_attr_nodiscard
228 CxIterator cxIterator( 251 CxIterator cxIterator(
229 const void *array, 252 const void *array,
230 size_t elem_size, 253 size_t elem_size,
231 size_t elem_count 254 size_t elem_count
232 ); 255 );
236 * 259 *
237 * While the iterator is in use, the array may only be altered by removing 260 * While the iterator is in use, the array may only be altered by removing
238 * elements through #cxIteratorFlagRemoval(). Every other change to the array 261 * elements through #cxIteratorFlagRemoval(). Every other change to the array
239 * will bring this iterator to an undefined state. 262 * will bring this iterator to an undefined state.
240 * 263 *
241 * When \p remove_keeps_order is set to \c false, removing an element will only 264 * When @p remove_keeps_order is set to @c false, removing an element will only
242 * move the last element to the position of the removed element, instead of 265 * move the last element to the position of the removed element, instead of
243 * moving all subsequent elements by one. Usually, when the order of elements is 266 * moving all subsequent elements by one. Usually, when the order of elements is
244 * not important, this parameter should be set to \c false. 267 * not important, this parameter should be set to @c false.
245 * 268 *
246 * The \p array can be \c NULL in which case the iterator will be immediately 269 * The @p array can be @c NULL in which case the iterator will be immediately
247 * initialized such that #cxIteratorValid() returns \c false. 270 * initialized such that #cxIteratorValid() returns @c false.
248 * 271 *
249 * 272 *
250 * @param array a pointer to the array (can be \c NULL) 273 * @param array a pointer to the array (can be @c NULL)
251 * @param elem_size the size of one array element 274 * @param elem_size the size of one array element
252 * @param elem_count the number of elements in the array 275 * @param elem_count the number of elements in the array
253 * @param remove_keeps_order \c true if the order of elements must be preserved 276 * @param remove_keeps_order @c true if the order of elements must be preserved
254 * when removing an element 277 * when removing an element
255 * @return an iterator for the specified array 278 * @return an iterator for the specified array
256 */ 279 */
257 __attribute__((__warn_unused_result__)) 280 cx_attr_nodiscard
258 CxIterator cxMutIterator( 281 CxIterator cxMutIterator(
259 void *array, 282 void *array,
260 size_t elem_size, 283 size_t elem_size,
261 size_t elem_count, 284 size_t elem_count,
262 bool remove_keeps_order 285 bool remove_keeps_order
263 ); 286 );
264 287
288 /**
289 * Creates an iterator for the specified plain pointer array.
290 *
291 * This iterator assumes that every element in the array is a pointer
292 * and yields exactly those pointers during iteration (while in contrast
293 * an iterator created with cxIterator() would return the addresses
294 * of those pointers within the array).
295 *
296 * @param array a pointer to the array (can be @c NULL)
297 * @param elem_count the number of elements in the array
298 * @return an iterator for the specified array
299 * @see cxIterator()
300 */
301 cx_attr_nodiscard
302 CxIterator cxIteratorPtr(
303 const void *array,
304 size_t elem_count
305 );
306
307 /**
308 * Creates a mutating iterator for the specified plain pointer array.
309 *
310 * This is the mutating variant of cxIteratorPtr(). See also
311 * cxMutIterator().
312 *
313 * @param array a pointer to the array (can be @c NULL)
314 * @param elem_count the number of elements in the array
315 * @param remove_keeps_order @c true if the order of elements must be preserved
316 * when removing an element
317 * @return an iterator for the specified array
318 * @see cxMutIterator()
319 * @see cxIteratorPtr()
320 */
321 cx_attr_nodiscard
322 CxIterator cxMutIteratorPtr(
323 void *array,
324 size_t elem_count,
325 bool remove_keeps_order
326 );
327
328 #ifdef __cplusplus
329 } // extern "C"
330 #endif
331
265 #endif // UCX_ITERATOR_H 332 #endif // UCX_ITERATOR_H

mercurial