src/cx/iterator.h

changeset 1096
2cb1ed4da55d
parent 1070
0a5a356a4486
equal deleted inserted replaced
1095:a5b0e47fc7de 1096:2cb1ed4da55d
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
151 */ 151 */
152 size_t elem_size; 152 size_t elem_size;
153 153
154 /** 154 /**
155 * May contain the total number of elements, if known. 155 * May contain the total number of elements, if known.
156 * 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.
157 */ 157 */
158 size_t elem_count; 158 size_t elem_count;
159 }; 159 };
160 160
161 /** 161 /**
164 * 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.
165 * 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
166 * 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.
167 * 167 *
168 * @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,
169 * 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,
170 * and it must not be used anymore. 170 * and it must not be used anymore.
171 */ 171 */
172 typedef struct cx_iterator_s CxIterator; 172 typedef struct cx_iterator_s CxIterator;
173 173
174 /** 174 /**
175 * Checks if the iterator points to valid data. 175 * Checks if the iterator points to valid data.
176 * 176 *
177 * This is especially false for past-the-end iterators. 177 * This is especially false for past-the-end iterators.
178 * 178 *
179 * @param iter the iterator 179 * @param iter the iterator
180 * @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
181 */ 182 */
182 #define cxIteratorValid(iter) (iter).base.valid(&(iter)) 183 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
183 184
184 /** 185 /**
185 * Returns a pointer to the current element. 186 * Returns a pointer to the current element.
186 * 187 *
187 * The behavior is undefined if this iterator is invalid. 188 * The behavior is undefined if this iterator is invalid.
188 * 189 *
189 * @param iter the iterator 190 * @param iter the iterator
190 * @return a pointer to the current element 191 * @return a pointer to the current element
192 * @see cxIteratorValid()
191 */ 193 */
192 #define cxIteratorCurrent(iter) (iter).base.current(&iter) 194 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
193 195
194 /** 196 /**
195 * Advances the iterator to the next element. 197 * Advances the iterator to the next element.
199 #define cxIteratorNext(iter) (iter).base.next(&iter) 201 #define cxIteratorNext(iter) (iter).base.next(&iter)
200 202
201 /** 203 /**
202 * Flags the current element for removal, if this iterator is mutating. 204 * Flags the current element for removal, if this iterator is mutating.
203 * 205 *
206 * Does nothing for non-mutating iterators.
207 *
204 * @param iter the iterator 208 * @param iter the iterator
205 */ 209 */
206 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating 210 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
207 211
208 /** 212 /**
209 * Obtains a reference to an arbitrary iterator. 213 * Obtains a reference to an arbitrary iterator.
210 * 214 *
211 * 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.
212 * 216 *
213 * @param iter the iterator 217 * @param iter the iterator
218 * @return (@c CxIterator*) a pointer to the iterator
214 */ 219 */
215 #define cxIteratorRef(iter) &((iter).base) 220 #define cxIteratorRef(iter) &((iter).base)
216 221
217 /** 222 /**
218 * Loops over an iterator. 223 * Loops over an iterator.
224 *
219 * @param type the type of the elements 225 * @param type the type of the elements
220 * @param elem the name of the iteration variable 226 * @param elem the name of the iteration variable
221 * @param iter the iterator 227 * @param iter the iterator
222 */ 228 */
223 #define cx_foreach(type, elem, iter) \ 229 #define cx_foreach(type, elem, iter) \
225 231
226 232
227 /** 233 /**
228 * Creates an iterator for the specified plain array. 234 * Creates an iterator for the specified plain array.
229 * 235 *
230 * 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
231 * initialized such that #cxIteratorValid() returns \c false. 237 * initialized such that #cxIteratorValid() returns @c false.
232 * 238 *
233 * This iterator yields the addresses of the array elements. 239 * This iterator yields the addresses of the array elements.
234 * If you want to iterator over an array of pointers, you can 240 * If you want to iterator over an array of pointers, you can
235 * use cxIteratorPtr() to create an iterator which directly 241 * use cxIteratorPtr() to create an iterator which directly
236 * yields the stored pointers. 242 * yields the stored pointers.
237 * 243 *
238 * @param array a pointer to the array (can be \c NULL) 244 * @param array a pointer to the array (can be @c NULL)
239 * @param elem_size the size of one array element 245 * @param elem_size the size of one array element
240 * @param elem_count the number of elements in the array 246 * @param elem_count the number of elements in the array
241 * @return an iterator for the specified array 247 * @return an iterator for the specified array
242 * @see cxIteratorPtr() 248 * @see cxIteratorPtr()
243 */ 249 */
253 * 259 *
254 * 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
255 * elements through #cxIteratorFlagRemoval(). Every other change to the array 261 * elements through #cxIteratorFlagRemoval(). Every other change to the array
256 * will bring this iterator to an undefined state. 262 * will bring this iterator to an undefined state.
257 * 263 *
258 * 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
259 * 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
260 * 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
261 * not important, this parameter should be set to \c false. 267 * not important, this parameter should be set to @c false.
262 * 268 *
263 * 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
264 * initialized such that #cxIteratorValid() returns \c false. 270 * initialized such that #cxIteratorValid() returns @c false.
265 * 271 *
266 * 272 *
267 * @param array a pointer to the array (can be \c NULL) 273 * @param array a pointer to the array (can be @c NULL)
268 * @param elem_size the size of one array element 274 * @param elem_size the size of one array element
269 * @param elem_count the number of elements in the array 275 * @param elem_count the number of elements in the array
270 * @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
271 * when removing an element 277 * when removing an element
272 * @return an iterator for the specified array 278 * @return an iterator for the specified array
273 */ 279 */
274 cx_attr_nodiscard 280 cx_attr_nodiscard
275 CxIterator cxMutIterator( 281 CxIterator cxMutIterator(
285 * This iterator assumes that every element in the array is a pointer 291 * This iterator assumes that every element in the array is a pointer
286 * and yields exactly those pointers during iteration (while in contrast 292 * and yields exactly those pointers during iteration (while in contrast
287 * an iterator created with cxIterator() would return the addresses 293 * an iterator created with cxIterator() would return the addresses
288 * of those pointers within the array). 294 * of those pointers within the array).
289 * 295 *
290 * @param array a pointer to the array (can be \c NULL) 296 * @param array a pointer to the array (can be @c NULL)
291 * @param elem_count the number of elements in the array 297 * @param elem_count the number of elements in the array
292 * @return an iterator for the specified array 298 * @return an iterator for the specified array
293 * @see cxIterator() 299 * @see cxIterator()
294 */ 300 */
295 cx_attr_nodiscard 301 cx_attr_nodiscard
302 * Creates a mutating iterator for the specified plain pointer array. 308 * Creates a mutating iterator for the specified plain pointer array.
303 * 309 *
304 * This is the mutating variant of cxIteratorPtr(). See also 310 * This is the mutating variant of cxIteratorPtr(). See also
305 * cxMutIterator(). 311 * cxMutIterator().
306 * 312 *
307 * @param array a pointer to the array (can be \c NULL) 313 * @param array a pointer to the array (can be @c NULL)
308 * @param elem_count the number of elements in the array 314 * @param elem_count the number of elements in the array
309 * @param remove_keeps_order \c true if the order of elements must be preserved 315 * @param remove_keeps_order @c true if the order of elements must be preserved
310 * when removing an element 316 * when removing an element
311 * @return an iterator for the specified array 317 * @return an iterator for the specified array
312 * @see cxMutIterator() 318 * @see cxMutIterator()
313 * @see cxIteratorPtr() 319 * @see cxIteratorPtr()
314 */ 320 */

mercurial