docs/Writerside/topics/iterator.h.md

Sat, 25 Jan 2025 13:44:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jan 2025 13:44:24 +0100
branch
docs/3.1
changeset 1146
151c057faf7c
parent 1143
0559812df10c
permissions
-rw-r--r--

add marker to every incomplete page

relates to #451

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Iterators
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
3 <warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
4 Outdated - Rewrite!
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
5 </warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
6
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 In UCX 3 a new feature has been introduced to write own iterators, that work with the `cx_foreach` macro.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 In previous UCX releases there were different hard-coded foreach macros for lists and maps that were not customizable.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 Now, creating an iterator is as simple as creating a `CxIterator` struct and setting the fields in a meaningful way.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 You do not always need all fields in the iterator structure, depending on your use case.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 Sometimes you only need the `index` (for example when iterating over simple lists), and other times you will need the
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 `slot` and `kv_data` fields (for example when iterating over maps).
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 If the predefined fields are insufficient for your use case, you can alternatively create your own iterator structure
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 and place the `CX_ITERATOR_BASE` macro as first member of that structure.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 Usually an iterator is not mutating the collection it is iterating over.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 In some programming languages it is even disallowed to change the collection while iterating with foreach.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 But sometimes it is desirable to remove an element from the collection while iterating over it.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 For this purpose, most collections allow the creation of a _mutating_ iterator.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 The only differences are, that the `mutating` flag is `true` and the `src_handle` is not const.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 On mutating iterators it is allowed to call the `cxFlagForRemoval()` function, which instructs the iterator to remove
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 the current element from the collection on the next call to `cxIteratorNext()` and clear the flag afterward.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 If you are implementing your own iterator, it is up to you to implement this behavior.
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
26
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
27 ## Undocumented Symbols (TODO)
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
28 ### cxIterator
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
29 ### cxIteratorPtr
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
30 ### cxMutIterator
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
31 ### cxMutIteratorPtr

mercurial