|
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 */ |
|
28 /** |
|
29 * \file iterator.h |
|
30 * \brief Interface for iterator implementations. |
|
31 * \author Mike Becker |
|
32 * \author Olaf Wintermann |
|
33 * \version 3.0 |
|
34 * \copyright 2-Clause BSD License |
|
35 */ |
|
36 |
|
37 #ifndef UCX_ITERATOR_H |
|
38 #define UCX_ITERATOR_H |
|
39 |
|
40 #include "common.h" |
|
41 |
|
42 /** |
|
43 * Internal iterator struct - use CxIterator. |
|
44 */ |
|
45 struct cx_iterator_s { |
|
46 /** |
|
47 * True iff the iterator points to valid data. |
|
48 */ |
|
49 __attribute__ ((__nonnull__)) |
|
50 bool (*valid)(struct cx_iterator_s const *); |
|
51 |
|
52 /** |
|
53 * Returns a pointer to the current element. |
|
54 */ |
|
55 __attribute__ ((__nonnull__)) |
|
56 void *(*current)(struct cx_iterator_s const *); |
|
57 |
|
58 /** |
|
59 * Advances the iterator. |
|
60 */ |
|
61 __attribute__ ((__nonnull__)) |
|
62 void (*next)(struct cx_iterator_s *); |
|
63 |
|
64 /** |
|
65 * Handle for the current element, if required. |
|
66 */ |
|
67 void *elem_handle; |
|
68 |
|
69 /** |
|
70 * Handle for the source collection, if any. |
|
71 */ |
|
72 void *src_handle; |
|
73 |
|
74 /** |
|
75 * Field for storing a key-value pair. |
|
76 * May be used by iterators that iterate over k/v-collections. |
|
77 */ |
|
78 struct { |
|
79 /** |
|
80 * A pointer to the key. |
|
81 */ |
|
82 void *key; |
|
83 /** |
|
84 * A pointer to the value. |
|
85 */ |
|
86 void *value; |
|
87 } kv_data; |
|
88 |
|
89 /** |
|
90 * Field for storing a slot number. |
|
91 * May be used by iterators that iterate over multi-bucket collections. |
|
92 */ |
|
93 size_t slot; |
|
94 |
|
95 /** |
|
96 * If the iterator is position-aware, contains the index of the element in the underlying collection. |
|
97 * Otherwise, this field is usually uninitialized. |
|
98 */ |
|
99 size_t index; |
|
100 |
|
101 /** |
|
102 * Users may set this to true, if the current element shall be removed from the underlying collection |
|
103 * when the iterator advances. |
|
104 * Has no effect on iterators that are not based on a collection. |
|
105 */ |
|
106 bool remove; |
|
107 }; |
|
108 |
|
109 /** |
|
110 * Iterator value type. |
|
111 * An iterator points to a certain element in an (possibly unbounded) chain of elements. |
|
112 * Iterators that are based on collections (which have a defined "first" element), are supposed |
|
113 * to be "position-aware", which means that they keep track of the current index within the collection. |
|
114 * |
|
115 * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the |
|
116 * iterator is based on a collection and the underlying collection is mutated (elements added or removed), |
|
117 * the iterator becomes invalid (regardless of what cxIteratorValid() returns) and MUST be re-obtained |
|
118 * from the collection. |
|
119 */ |
|
120 typedef struct cx_iterator_s CxIterator; |
|
121 |
|
122 /** |
|
123 * Checks if the iterator points to valid data. |
|
124 * |
|
125 * This is especially false for past-the-end iterators. |
|
126 * |
|
127 * @param iter a pointer to the iterator |
|
128 * @return true iff the iterator points to valid data |
|
129 */ |
|
130 __attribute__ ((__nonnull__)) |
|
131 static inline bool cxIteratorValid(CxIterator const *iter) { |
|
132 return iter->valid(iter); |
|
133 } |
|
134 |
|
135 /** |
|
136 * Returns a pointer to the current element. |
|
137 * |
|
138 * The behavior is undefined if this iterator is invalid. |
|
139 * |
|
140 * @param iter a pointer to the iterator |
|
141 * @return a pointer to the current element |
|
142 */ |
|
143 __attribute__ ((__nonnull__)) |
|
144 static inline void *cxIteratorCurrent(CxIterator const *iter) { |
|
145 return iter->current(iter); |
|
146 } |
|
147 |
|
148 /** |
|
149 * Advances the iterator to the next element. |
|
150 * |
|
151 * @param iter a pointer to the iterator |
|
152 */ |
|
153 __attribute__ ((__nonnull__)) |
|
154 static inline void cxIteratorNext(CxIterator *iter) { |
|
155 iter->next(iter); |
|
156 } |
|
157 |
|
158 /** |
|
159 * Loops over an iterator. |
|
160 * @param type the type of the elements |
|
161 * @param elem the name of the iteration variable |
|
162 * @param iter the iterator |
|
163 */ |
|
164 #define cx_foreach(type, elem, iter) \ |
|
165 for (type elem; cxIteratorValid(&iter) && (elem = (type)cxIteratorCurrent(&iter)) != NULL ; cxIteratorNext(&iter)) // NOLINT(bugprone-macro-parentheses) |
|
166 |
|
167 #endif /* UCX_ITERATOR_H */ |