180 |
180 |
181 ## Iterator |
181 ## Iterator |
182 |
182 |
183 *Header file:* [iterator.h](api/iterator_8h.html) |
183 *Header file:* [iterator.h](api/iterator_8h.html) |
184 |
184 |
|
185 In UCX 3 a new feature has been introduced to write own iterators, that work with the `cx_foreach` macro. |
|
186 In previous UCX releases there were different hard-coded foreach macros for lists and maps that were not customizable. |
|
187 Now, creating an iterator is as simple as creating a `CxIterator` struct and setting the fields in a meaningful way. |
|
188 |
|
189 You do not always need all fields in the iterator structure, depending on your use case. |
|
190 Sometimes you only need the `index` (for example when iterating over simple lists), and other times you will need the |
|
191 `slot` and `kv_data` fields (for example when iterating over maps). |
|
192 |
|
193 Usually an iterator is not mutating the collection it is iterating over. |
|
194 In some programming languages it is even disallowed to change the collection while iterating with foreach. |
|
195 But sometimes it is desirable to remove an element from the collection while iterating over it. |
|
196 This is, what the `CxMutIterator` is for. |
|
197 The only differences are, that the `mutating` flag is `true` and the `src_handle` is not const. |
|
198 On mutating iterators it is allowed to call the `cxFlagForRemoval()` function, which instructs the iterator to remove |
|
199 the current element from the collection on the next call to `cxIteratorNext()` and clear the flag afterward. |
|
200 If you are implementing your own iterator, it is up to you to implement this behavior in your `next` method, or |
|
201 make the implementation of the `flag_removal` method always return `false`. |
|
202 |
185 ## Collection |
203 ## Collection |
186 |
204 |
187 *Header file:* [collection.h](api/collection_8h.html) |
205 *Header file:* [collection.h](api/collection_8h.html) |
188 |
206 |
189 Collections in UCX 3 have several common features. |
207 Collections in UCX 3 have several common features. |