docs/Writerside/topics/iterator.h.md

changeset 1424
563033aa998c
parent 1245
721e2032fa25
equal deleted inserted replaced
1423:9a72258446cd 1424:563033aa998c
4 This allows iteration over arrays, lists, maps, trees, etc. in a unified way. 4 This allows iteration over arrays, lists, maps, trees, etc. in a unified way.
5 5
6 Creating an iterator is as simple as creating a `CxIterator` struct and setting the fields in a meaningful way. 6 Creating an iterator is as simple as creating a `CxIterator` struct and setting the fields in a meaningful way.
7 The UCX collections provide various functions to create such iterators. 7 The UCX collections provide various functions to create such iterators.
8 8
9 If the predefined fields are insufficient (or introduce too much bloat) for your use case, 9 If the predefined fields are not enough (or introduce too much bloat) for your use case,
10 you can alternatively create your own iterator structure 10 you can alternatively create your own iterator structure
11 and place the `CX_ITERATOR_BASE` macro as first member of that structure. 11 and place the `CX_ITERATOR_BASE` macro as first member of that structure.
12 12
13 ```C 13 ```C
14 #include <cx/iterator.h> 14 #include <cx/iterator.h>
77 The macro takes three arguments: 77 The macro takes three arguments:
78 1. the pointer-type of a pointer to an element, 78 1. the pointer-type of a pointer to an element,
79 2. the name of the variable you want to use for accessing the element, 79 2. the name of the variable you want to use for accessing the element,
80 3. and the iterator. 80 3. and the iterator.
81 81
82 > An iterator does not necessarily need to iterate over the elements of a collections. 82 > An iterator does not necessarily need to iterate over the concrete elements of a collection.
83 > Map iterators, for example, can iterator over the key/value pairs, 83 > Map iterators, for example, can iterator over the key/value pairs,
84 > but they can also iterate over just the values or just the keys. 84 > but they can also iterate over just the values or just the keys.
85 > 85 >
86 > You should read the documentation of the function creating the iterator to learn 86 > You should read the documentation of the function creating the iterator to learn
87 > what exactly the iterator is iterating over. 87 > what exactly the iterator is iterating over.
98 If you are implementing your own iterator, it is up to you to implement this behavior. 98 If you are implementing your own iterator, it is up to you to implement this behavior.
99 99
100 ## Passing Iterators to Functions 100 ## Passing Iterators to Functions
101 101
102 To eliminate the need of memory management for iterators, the structures are usually used by value. 102 To eliminate the need of memory management for iterators, the structures are usually used by value.
103 This does not come with additional costs, because iteration is implemented entirely by macros. 103 This does not come with additional costs because iteration is implemented entirely by using macros.
104 104
105 However, sometimes it is necessary to pass an iterator to another function. 105 However, sometimes it is necessary to pass an iterator to another function.
106 To make that possible in a generalized way, such functions should accept a `CxIteratorBase*` pointer 106 To make that possible in a generalized way, such functions should accept a `CxIteratorBase*` pointer
107 which can be obtained with the `cxIteratorRef()` macro on the calling site. 107 which can be obtained with the `cxIteratorRef()` macro on the calling site.
108 108
143 143
144 Iterators may be wrapped in which case the original implementation can be stored in `current_impl` and 144 Iterators may be wrapped in which case the original implementation can be stored in `current_impl` and
145 called by a wrapper implementation pointed to by `current`. 145 called by a wrapper implementation pointed to by `current`.
146 This can be useful when you want to support the `store_pointer` field of the [](collection.h.md) API. 146 This can be useful when you want to support the `store_pointer` field of the [](collection.h.md) API.
147 147
148 A specialized, simple, and fast iterator over an array of a certain type, 148 A specialized, simple, and fast iterator over an array of a certain type
149 that does not support mutation, can be implemented as follows: 149 that does not support mutation can be implemented as follows:
150 ```C 150 ```C
151 #include <cx/iterator.h> 151 #include <cx/iterator.h>
152 152
153 typedef struct my_foo_s { 153 typedef struct my_foo_s {
154 // ... your data ... 154 // ... your data ...
194 } 194 }
195 ``` 195 ```
196 196
197 > Note, that the behavior of `current` is undefined when `valid` returns `false`. 197 > Note, that the behavior of `current` is undefined when `valid` returns `false`.
198 > That means, on the one hand, `current` does not need to check for validity of the iterator, 198 > That means, on the one hand, `current` does not need to check for validity of the iterator,
199 > but on the other hand it is forbidden to invoke `current` when `valid` would return `false`. 199 > but on the other hand, it is forbidden to invoke `current` when `valid` would return `false`.
200 {style="note"} 200 {style="note"}
201 201
202 > If performance matters in your application, it is recommended that you indeed create specialized iterators 202 > If performance matters in your application, it is recommended that you indeed create specialized iterators
203 > for your collections. The default UCX implementations trade some of the performance for generality. 203 > for your collections. The default UCX implementations trade some performance for generality.
204 204
205 <seealso> 205 <seealso>
206 <category ref="apidoc"> 206 <category ref="apidoc">
207 <a href="https://ucx.sourceforge.io/api/iterator_8h.html">iterator.h</a> 207 <a href="https://ucx.sourceforge.io/api/iterator_8h.html">iterator.h</a>
208 </category> 208 </category>

mercurial