ucx/cx/iterator.h

changeset 852
83fdf679df99
parent 816
839fefbdedc7
equal deleted inserted replaced
850:bbe2925eb590 852:83fdf679df99
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)(void const *); 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
54 void *(*current)(void const *); 61 cx_attr_nodiscard
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
60 void *(*current_impl)(void const *); 68 cx_attr_nodiscard
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;
76 bool remove; 85 bool remove;
77 }; 86 };
78 87
79 /** 88 /**
80 * Declares base attributes for an iterator. 89 * Declares base attributes for an iterator.
90 * Must be the first member of an iterator structure.
81 */ 91 */
82 #define CX_ITERATOR_BASE struct cx_iterator_base_s base 92 #define CX_ITERATOR_BASE struct cx_iterator_base_s base
83 93
84 /** 94 /**
85 * Internal iterator struct - use CxIterator. 95 * Internal iterator struct - use CxIterator.
86 */ 96 */
87 struct cx_iterator_s { 97 struct cx_iterator_s {
98 /**
99 * Inherited common data for all iterators.
100 */
88 CX_ITERATOR_BASE; 101 CX_ITERATOR_BASE;
89 102
90 /** 103 /**
91 * Handle for the current element. 104 * Handle for the current element.
92 */ 105 */
101 */ 114 */
102 void *m; 115 void *m;
103 /** 116 /**
104 * Access for normal iterators. 117 * Access for normal iterators.
105 */ 118 */
106 void const *c; 119 const void *c;
107 } src_handle; 120 } src_handle;
108 121
109 /** 122 /**
110 * Field for storing a key-value pair. 123 * Field for storing a key-value pair.
111 * May be used by iterators that iterate over k/v-collections. 124 * May be used by iterators that iterate over k/v-collections.
112 */ 125 */
113 struct { 126 struct {
114 /** 127 /**
115 * A pointer to the key. 128 * A pointer to the key.
116 */ 129 */
117 void const *key; 130 const void *key;
118 /** 131 /**
119 * A pointer to the value. 132 * A pointer to the value.
120 */ 133 */
121 void *value; 134 void *value;
122 } kv_data; 135 } kv_data;
138 */ 151 */
139 size_t elem_size; 152 size_t elem_size;
140 153
141 /** 154 /**
142 * May contain the total number of elements, if known. 155 * May contain the total number of elements, if known.
143 * 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.
144 */ 157 */
145 size_t elem_count; 158 size_t elem_count;
146 }; 159 };
147 160
148 /** 161 /**
151 * 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.
152 * 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
153 * 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.
154 * 167 *
155 * @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,
156 * 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,
157 * and it must not be used anymore. 170 * and it must not be used anymore.
158 */ 171 */
159 typedef struct cx_iterator_s CxIterator; 172 typedef struct cx_iterator_s CxIterator;
160 173
161 /** 174 /**
162 * Checks if the iterator points to valid data. 175 * Checks if the iterator points to valid data.
163 * 176 *
164 * This is especially false for past-the-end iterators. 177 * This is especially false for past-the-end iterators.
165 * 178 *
166 * @param iter the iterator 179 * @param iter the iterator
167 * @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
168 */ 182 */
169 #define cxIteratorValid(iter) (iter).base.valid(&(iter)) 183 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
170 184
171 /** 185 /**
172 * Returns a pointer to the current element. 186 * Returns a pointer to the current element.
173 * 187 *
174 * The behavior is undefined if this iterator is invalid. 188 * The behavior is undefined if this iterator is invalid.
175 * 189 *
176 * @param iter the iterator 190 * @param iter the iterator
177 * @return a pointer to the current element 191 * @return a pointer to the current element
192 * @see cxIteratorValid()
178 */ 193 */
179 #define cxIteratorCurrent(iter) (iter).base.current(&iter) 194 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
180 195
181 /** 196 /**
182 * Advances the iterator to the next element. 197 * Advances the iterator to the next element.
186 #define cxIteratorNext(iter) (iter).base.next(&iter) 201 #define cxIteratorNext(iter) (iter).base.next(&iter)
187 202
188 /** 203 /**
189 * Flags the current element for removal, if this iterator is mutating. 204 * Flags the current element for removal, if this iterator is mutating.
190 * 205 *
206 * Does nothing for non-mutating iterators.
207 *
191 * @param iter the iterator 208 * @param iter the iterator
192 */ 209 */
193 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating 210 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
194 211
195 /** 212 /**
213 * Obtains a reference to an arbitrary iterator.
214 *
215 * This is useful for APIs that expect some iterator as an argument.
216 *
217 * @param iter the iterator
218 * @return (@c CxIterator*) a pointer to the iterator
219 */
220 #define cxIteratorRef(iter) &((iter).base)
221
222 /**
196 * Loops over an iterator. 223 * Loops over an iterator.
224 *
197 * @param type the type of the elements 225 * @param type the type of the elements
198 * @param elem the name of the iteration variable 226 * @param elem the name of the iteration variable
199 * @param iter the iterator 227 * @param iter the iterator
200 */ 228 */
201 #define cx_foreach(type, elem, iter) \ 229 #define cx_foreach(type, elem, iter) \
203 231
204 232
205 /** 233 /**
206 * Creates an iterator for the specified plain array. 234 * Creates an iterator for the specified plain array.
207 * 235 *
208 * 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
209 * initialized such that #cxIteratorValid() returns \c false. 237 * initialized such that #cxIteratorValid() returns @c false.
210 * 238 *
211 * 239 * This iterator yields the addresses of the array elements.
212 * @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)
213 * @param elem_size the size of one array element 245 * @param elem_size the size of one array element
214 * @param elem_count the number of elements in the array 246 * @param elem_count the number of elements in the array
215 * @return an iterator for the specified array 247 * @return an iterator for the specified array
216 */ 248 * @see cxIteratorPtr()
217 __attribute__((__warn_unused_result__)) 249 */
250 cx_attr_nodiscard
218 CxIterator cxIterator( 251 CxIterator cxIterator(
219 void const *array, 252 const void *array,
220 size_t elem_size, 253 size_t elem_size,
221 size_t elem_count 254 size_t elem_count
222 ); 255 );
223 256
224 /** 257 /**
226 * 259 *
227 * 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
228 * elements through #cxIteratorFlagRemoval(). Every other change to the array 261 * elements through #cxIteratorFlagRemoval(). Every other change to the array
229 * will bring this iterator to an undefined state. 262 * will bring this iterator to an undefined state.
230 * 263 *
231 * 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
232 * 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
233 * 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
234 * not important, this parameter should be set to \c false. 267 * not important, this parameter should be set to @c false.
235 * 268 *
236 * 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
237 * initialized such that #cxIteratorValid() returns \c false. 270 * initialized such that #cxIteratorValid() returns @c false.
238 * 271 *
239 * 272 *
240 * @param array a pointer to the array (can be \c NULL) 273 * @param array a pointer to the array (can be @c NULL)
241 * @param elem_size the size of one array element 274 * @param elem_size the size of one array element
242 * @param elem_count the number of elements in the array 275 * @param elem_count the number of elements in the array
243 * @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
244 * when removing an element 277 * when removing an element
245 * @return an iterator for the specified array 278 * @return an iterator for the specified array
246 */ 279 */
247 __attribute__((__warn_unused_result__)) 280 cx_attr_nodiscard
248 CxIterator cxMutIterator( 281 CxIterator cxMutIterator(
249 void *array, 282 void *array,
250 size_t elem_size, 283 size_t elem_size,
251 size_t elem_count, 284 size_t elem_count,
252 bool remove_keeps_order 285 bool remove_keeps_order
253 ); 286 );
254 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
255 #endif // UCX_ITERATOR_H 332 #endif // UCX_ITERATOR_H

mercurial