2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
30 * \brief Interface for iterator implementations.
32 * \author Olaf Wintermann
34 * \copyright 2-Clause BSD License
37 #ifndef UCX_ITERATOR_H
38 #define UCX_ITERATOR_H
43 * The base of mutating and non-mutating iterators.
45 struct cx_iterator_base_s {
47 * True iff the iterator points to valid data.
49 __attribute__ ((__nonnull__))
50 bool (*valid)(void const *);
53 * Returns a pointer to the current element.
55 __attribute__ ((__nonnull__))
56 void *(*current)(void const *);
59 * Original implementation in case the function needs to be wrapped.
61 __attribute__ ((__nonnull__))
62 void *(*current_impl)(void const *);
65 * Advances the iterator.
67 __attribute__ ((__nonnull__))
71 * Flag current element for removal, if possible.
73 __attribute__ ((__nonnull__))
74 bool (*flag_removal)(void *);
77 * Indicates whether this iterator is muting.
82 * Internal flag for removing the current element when advancing.
88 * Internal iterator struct - use CxMutIterator.
90 struct cx_mut_iterator_s {
93 * The base properties of this iterator.
95 struct cx_iterator_base_s base;
98 * Handle for the current element, if required.
103 * Handle for the source collection, if any.
108 * Field for storing a key-value pair.
109 * May be used by iterators that iterate over k/v-collections.
113 * A pointer to the key.
117 * A pointer to the value.
123 * Field for storing a slot number.
124 * May be used by iterators that iterate over multi-bucket collections.
129 * If the iterator is position-aware, contains the index of the element in the underlying collection.
130 * Otherwise, this field is usually uninitialized.
136 * Mutating iterator value type.
138 * An iterator points to a certain element in an (possibly unbounded) chain of elements.
139 * Iterators that are based on collections (which have a defined "first" element), are supposed
140 * to be "position-aware", which means that they keep track of the current index within the collection.
142 * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
143 * iterator is based on a collection and the underlying collection is mutated by other means than this iterator
144 * (e.g. elements added or removed), the iterator becomes invalid (regardless of what cxIteratorValid() returns)
145 * and MUST be re-obtained from the collection.
149 typedef struct cx_mut_iterator_s CxMutIterator;
152 * Internal iterator struct - use CxIterator.
154 struct cx_iterator_s {
157 * The base properties of this iterator.
159 struct cx_iterator_base_s base;
162 * Handle for the current element, if required.
167 * Handle for the source collection, if any.
169 void const *src_handle;
172 * Field for storing a key-value pair.
173 * May be used by iterators that iterate over k/v-collections.
177 * A pointer to the key.
181 * A pointer to the value.
187 * Field for storing a slot number.
188 * May be used by iterators that iterate over multi-bucket collections.
193 * If the iterator is position-aware, contains the index of the element in the underlying collection.
194 * Otherwise, this field is usually uninitialized.
200 * Iterator value type.
201 * An iterator points to a certain element in an (possibly unbounded) chain of elements.
202 * Iterators that are based on collections (which have a defined "first" element), are supposed
203 * to be "position-aware", which means that they keep track of the current index within the collection.
205 * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
206 * this iterator cannot mutate the collection itself (add or remove elements) and any mutation of the
207 * collection by other means make this iterator invalid (regardless of what cxIteratorValid() returns).
211 typedef struct cx_iterator_s CxIterator;
214 * Checks if the iterator points to valid data.
216 * This is especially false for past-the-end iterators.
218 * @param iter the iterator
219 * @return true iff the iterator points to valid data
221 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
224 * Returns a pointer to the current element.
226 * The behavior is undefined if this iterator is invalid.
228 * @param iter the iterator
229 * @return a pointer to the current element
231 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
234 * Advances the iterator to the next element.
236 * @param iter the iterator
238 #define cxIteratorNext(iter) (iter).base.next(&iter)
241 * Flags the current element for removal.
243 * @param iter the iterator
244 * @return false if this iterator cannot remove the element
246 #define cxIteratorFlagRemoval(iter) (iter).base.flag_removal(&iter)
249 * Loops over an iterator.
250 * @param type the type of the elements
251 * @param elem the name of the iteration variable
252 * @param iter the iterator
254 #define cx_foreach(type, elem, iter) \
255 for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
257 #endif // UCX_ITERATOR_H