ucx
UAP Common Extensions
Loading...
Searching...
No Matches
iterator.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
36#ifndef UCX_ITERATOR_H
37#define UCX_ITERATOR_H
38
39#include "common.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
52 bool (*valid)(const void *);
53
59 void *(*current)(const void *);
60
64 void *(*current_impl)(const void *);
65
71 void (*next)(void *);
79 bool remove;
80};
81
87
92#define CX_ITERATOR_BASE struct cx_iterator_base_s base
93
102
107
111 union {
115 void *m;
119 const void *c;
121
126 size_t index;
127
131 size_t elem_size;
132
138};
139
152
160#define cxIteratorValid(iter) (iter).base.valid(&(iter))
161
171#define cxIteratorCurrent(iter) (iter).base.current(&iter)
172
178#define cxIteratorNext(iter) (iter).base.next(&iter)
179
187#define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
188
197#define cxIteratorRef(iter) &((iter).base)
198
206#define cx_foreach(type, elem, iter) \
207for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
208
209
230 const void *array,
231 size_t elem_size,
232 size_t elem_count
233);
234
261 void *array,
262 size_t elem_size,
263 size_t elem_count,
264 bool remove_keeps_order
265);
266
283 const void *array,
284 size_t elem_count
285);
286
304 void *array,
305 size_t elem_count,
306 bool remove_keeps_order
307);
308
309#ifdef __cplusplus
310} // extern "C"
311#endif
312
313#endif // UCX_ITERATOR_H
Common definitions and feature checks.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define CX_ITERATOR_BASE
Declares base attributes for an iterator.
Definition iterator.h:92
CxIterator cxIteratorPtr(const void *array, size_t elem_count)
Creates an iterator for the specified plain pointer array.
CxIterator cxMutIteratorPtr(void *array, size_t elem_count, bool remove_keeps_order)
Creates a mutating iterator for the specified plain pointer array.
CxIterator cxIterator(const void *array, size_t elem_size, size_t elem_count)
Creates an iterator for the specified plain array.
CxIterator cxMutIterator(void *array, size_t elem_size, size_t elem_count, bool remove_keeps_order)
Creates a mutating iterator for the specified plain array.
Common data for all iterators.
Definition iterator.h:48
bool remove
Internal flag for removing the current element when advancing.
Definition iterator.h:79
bool(* valid)(const void *)
True if the iterator points to valid data.
Definition iterator.h:52
bool mutating
Indicates whether this iterator may remove elements.
Definition iterator.h:75
void(* next)(void *)
Advances the iterator.
Definition iterator.h:71
Internal iterator struct - use CxIterator.
Definition iterator.h:97
void * m
Access for mutating iterators.
Definition iterator.h:115
size_t elem_count
May contain the total number of elements, if known.
Definition iterator.h:137
void * elem_handle
Handle for the current element.
Definition iterator.h:106
const void * c
Access for normal iterators.
Definition iterator.h:119
size_t index
If the iterator is position-aware, contains the index of the element in the underlying collection.
Definition iterator.h:126
union cx_iterator_s::@2 src_handle
Handle for the source collection, if any.
size_t elem_size
The size of an individual element.
Definition iterator.h:131