Represents an iterator for a container by implementing the IIterator interface. More...
Public Member Functions | |
Iterator (TItemType[] items) | |
Constructor. | |
TItemType? | Next () |
Returns the next item in the iteration or null, if there are no more items. | |
void | Reset () |
Reset the iterator to the beginning. | |
void | Reset () |
Start iteration from beginning of container. | |
TItemType? | Next () |
Retrieve the next item from the container. | |
Private Attributes | |
TItemType[] | _items |
The array of items to iterate over. | |
int | _index |
The index into the _items array to the next item. | |
Represents an iterator for a container by implementing the IIterator interface.
This class retains a copy of the elements to be iterated over so it is immune to changes in the container from which this iterator was generated.
TItemType | The type of each item provided by the iterator |
This iterator is immune to changes in the source container. However, that makes this iterator expensive because the items being iterated over have to be duplicated in this class.
One alternative is for there to be only a single iterator and that iterator is implemented on the container class itself. If there are more than one type of iterator (as in this example) then the source container would have to implement multiple iterators, one for each type of item returned from the iterator.
Another alternative is for this iterator class to have a way to access the source container's data so the data doesn't have to be copied.
Definition at line 90 of file IteratorContainer_Class.cs.
|
inline |
Constructor.
items | The items to iterate over. |
Definition at line 106 of file IteratorContainer_Class.cs.
References Iterator< TItemType >._items.
|
inline |
Returns the next item in the iteration or null, if there are no more items.
Implements IIterator< TItemType >.
Definition at line 116 of file IteratorContainer_Class.cs.
References Iterator< TItemType >._index, and Iterator< TItemType >._items.
|
inline |
Reset the iterator to the beginning.
Implements IIterator< TItemType >.
Definition at line 131 of file IteratorContainer_Class.cs.
References Iterator< TItemType >._index.
|
private |
The index into the _items array to the next item.
Definition at line 100 of file IteratorContainer_Class.cs.
Referenced by Iterator< TItemType >.Next(), Iterator.Next(), Iterator< TItemType >.Reset(), and Iterator.Reset().
|
private |
The array of items to iterate over.
Definition at line 95 of file IteratorContainer_Class.cs.
Referenced by Iterator< TItemType >.Iterator(), Iterator< TItemType >.Next(), and Iterator.Next().