|
| 1 | +## Program Design: iterators {#it} |
| 2 | + |
| 3 | +_Skeleton descriptions are typeset in italic text,_ |
| 4 | +_so please don't remove these descriptions when editing the topic._ |
| 5 | + |
| 6 | +### Overview |
| 7 | + |
| 8 | +_Provides a short natural language abstract of the module’s contents._ |
| 9 | +_Specifies the different levels of teaching._ |
| 10 | + |
| 11 | +------------------------------------------------------------------------ |
| 12 | +Level Objective |
| 13 | +----------------- ------------------------------------------------------ |
| 14 | +Foundational Using iterators |
| 15 | + |
| 16 | +Main Writing iterators |
| 17 | + |
| 18 | +Advanced --- |
| 19 | + |
| 20 | +------------------------------------------------------------------------ |
| 21 | + |
| 22 | +### Motivation |
| 23 | + |
| 24 | +_Why is this important?_ |
| 25 | +_Why do we want to learn/teach this topic?_ |
| 26 | + |
| 27 | +Iterators provide a generic interface between algorithms and containers. |
| 28 | +The container defines the iteration over its data. |
| 29 | +Therefore, the algorithm does neither need to know about the iteration nor the concrete data types that are stored in the container. |
| 30 | +This way, algorithms can be implemented in a generic way without detailed knowledge of the containers they operate on, and the containers can be written without predetermining what algorithms should be available for it. |
| 31 | + |
| 32 | +### Topic introduction |
| 33 | + |
| 34 | +_Very brief introduction to the topic._ |
| 35 | + |
| 36 | +### Foundational: Using iterators {#it-found} |
| 37 | + |
| 38 | +#### Background/Required Knowledge |
| 39 | + |
| 40 | +A student: |
| 41 | +* containers [[Program Design: Containers - Foundational]][1] |
| 42 | +* for loops |
| 43 | +* [Maybe] [[Object Model: Value Semantics - Foundational]][2][[Object Model: Reference Semantics - Foundational]][3] TODO: revisited after ref/value semantics |
| 44 | + |
| 45 | +#### Student outcomes |
| 46 | + |
| 47 | +_A list of things "a student should be able to" after the curriculum._ |
| 48 | +_The next word should be an action word and testable in an exam._ |
| 49 | +_Max 5 items._ |
| 50 | + |
| 51 | +A student should be able to: |
| 52 | + |
| 53 | +1. write a for loop using iterators (not just range-based for). |
| 54 | +2. use the canonical iterator operations (begin/end, dereference, etc.). |
| 55 | +3. use the appropriate operations with regard to the iterators category. |
| 56 | +4. show examples where iterators are invalidated by operations on a container. |
| 57 | +5. demonstrate how iterators provide an abstraction between the operation and the underlying data structure. |
| 58 | + |
| 59 | +#### Caveats |
| 60 | + |
| 61 | +_This section mentions subtle points to understand, like anything resulting in |
| 62 | +implementation-defined, unspecified, or undefined behavior._ |
| 63 | + |
| 64 | +* Dangling iterators (i.e., having an iterator to a no longer valid container) |
| 65 | +* `end()` marks the position after the last element and, by that, is not dereferencable |
| 66 | +* When comparing iterators, both iterators need to belong to the same underlying data structure |
| 67 | +* Keep in mind that iterators do not guarantee that the underlying memory is contiguous. |
| 68 | + |
| 69 | +#### Points to cover |
| 70 | + |
| 71 | +_This section lists important details for each point._ |
| 72 | + |
| 73 | +* Using standard iterators with iterator-based for loops |
| 74 | + * Difference between value/reference semantics based for loops |
| 75 | + * Explain connection to for-each |
| 76 | +* Basic iterator nomenclature and functionality |
| 77 | + * begin/end |
| 78 | + * Give an example of the typical for loop pattern `iter != end()` |
| 79 | + * Dereferencing iterators |
| 80 | + * Advancing iterators |
| 81 | + * Iterator adapters for containers (e.g., `back_inserter`) |
| 82 | +* Explain in a simple way the differences between different iterator notions |
| 83 | + * For example, forward/backward/random access |
| 84 | + * Defer a more detailed explanation of iterator categories to main |
| 85 | +* Give a brief introduction to iterator invalidation |
| 86 | + * Defer a more detailed explanation to main |
| 87 | +* Explain the design idea behind iterators |
| 88 | + * Iterators provide abstraction |
| 89 | + * Iterators decouple accessing elements and iterating over a container from the actual container type |
| 90 | + * Iterators store the iteration state (i.e., where you are in the container) |
| 91 | + |
| 92 | + |
| 93 | +### Main: Writing iterators {#it-main} |
| 94 | + |
| 95 | +#### Background/Required Knowledge |
| 96 | + |
| 97 | +#### Student outcomes |
| 98 | + |
| 99 | +A student should be able to: |
| 100 | + |
| 101 | +1. create an iterator based on an existing iterator and change its behavior. |
| 102 | +2. create an standard compliant iterator for a self written container. |
| 103 | +3. determine when an iterator is invalidated and write code accordingly. |
| 104 | + |
| 105 | +#### Caveats |
| 106 | + |
| 107 | +* Beware when using iterators in a multi-threaded environment as both the iterator and the data structure need to be correctly synchronized (see XYZ). TODO: find reference |
| 108 | +* Make sure that the implementation of the iterator offers at least the interface and guarantees that its category offers. |
| 109 | + |
| 110 | + |
| 111 | +#### Points to cover |
| 112 | + |
| 113 | +* When and how to defined internal/external iterators |
| 114 | + * Iterator adapters (external) |
| 115 | + * adds additional logic (e.g., reverse iterators) |
| 116 | + * non-linear iterators (e.g., skip some number elements) |
| 117 | + * API-extending iterators (external/internal) |
| 118 | + * e.g., adding a new pre-order traversal iterator to a tree |
| 119 | +* Iterator invalidation |
| 120 | + * Modifying the underlying data structure can invalidate an iterator |
| 121 | + * Different types of containers have different iterator invalidation characteristics, even on similar methods (e.g., `vector.push_back` vs `list.insert`). |
| 122 | + * Invalidation can be unpredictable (e.g., a `push_back` to a vector may but does not have to invalidate an iterator) |
| 123 | + * Containers that don’t have stable addressing have the problem of iterator invalidation. |
| 124 | + * The iterator should not outlive the container (lifetime) |
| 125 | +* Templates and iterators |
| 126 | + * Iterator type interface (`value_type`, `pointer`, `reference`, `iterator_category`) |
| 127 | + * To support different types of iterators algorithms often use template parameters for the concrete iterator type and only rely on the interface of an iterator (see topic [[Program Design: Algorithms - Main]][4]) |
| 128 | + |
| 129 | + |
| 130 | +### Advanced |
| 131 | + |
| 132 | +_These are important topics that are not expected to be covered but provide |
| 133 | +guidance where one can continue to investigate this topic in more depth._ |
| 134 | + |
| 135 | +[1]: ../program-design/containers.md |
| 136 | +[2]: ../object-model/value-semantics.md |
| 137 | +[3]: ../object-model/reference-semantics.md |
| 138 | +[4]: ../program-design/algorithms.md |
0 commit comments