 |
- Author:
- keinfarbton (IP: 85.180.21.52)
- Timestamp:
- 10/06/08 17:27:55 (5 years ago)
- Comment:
more about using, iterator
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ChapterStorage
| v35 |
v36 |
|
| 68 | 68 | {{{ |
|---|
| 69 | 69 | #!d |
|---|
| 70 | | auto list1 = new LinkedList!( int ); |
|---|
| | 70 | auto list = new LinkedList!( int ); |
|---|
| | 71 | auto map = new HashMap!( char[], char[] ); |
|---|
| 71 | 72 | }}} |
|---|
| 72 | 73 | |
|---|
| 81 | 82 | == Add and remove elements == |
|---|
| 82 | 83 | |
|---|
| 83 | | Each container type has it own set of methods. |
|---|
| 84 | | {{{ |
|---|
| 85 | | #!d |
|---|
| 86 | | list1.prepend( 2 ); |
|---|
| 87 | | list1.append( 3 ); |
|---|
| 88 | | list1.append( 4 ); |
|---|
| 89 | | list2.append( "The first item in list1" ); |
|---|
| 90 | | }}} |
|---|
| 91 | | |
|---|
| 92 | | {{{ |
|---|
| 93 | | #!d |
|---|
| 94 | | int oldValue = list1.removeHead(); // removes the '2' |
|---|
| 95 | | }}} |
|---|
| | 84 | Each container type has its own set of methods. All containers support {{{add}}}. |
|---|
| | 85 | {{{ |
|---|
| | 86 | #!d |
|---|
| | 87 | list.add( 2 ); |
|---|
| | 88 | map.add( "first", "value one" ); |
|---|
| | 89 | }}} |
|---|
| | 90 | |
|---|
| | 91 | Some containers have specialized methods to add values at a certain index like {{{addAt}}}, {{{append}}} or {{{prepend}}} |
|---|
| | 92 | {{{ |
|---|
| | 93 | #!d |
|---|
| | 94 | list.prepend( 4 ); // now '4, 2' |
|---|
| | 95 | list.addAt( 1, 3 ); // now '4, 3, 2' |
|---|
| | 96 | list.append( 1 ); // now '4, 3, 2, 1' |
|---|
| | 97 | }}} |
|---|
| | 98 | |
|---|
| | 99 | {{{ |
|---|
| | 100 | #!d |
|---|
| | 101 | int oldValue = list.removeHead(); // now '3, 2, 1' and oldValue == 4 |
|---|
| | 102 | }}} |
|---|
| | 103 | |
|---|
| | 104 | There are more methods for adding, removing, searching, retrival, ... See the API doc of the container. |
|---|
| 96 | 105 | |
|---|
| 97 | 106 | == Visiting contained elements == |
|---|
| 98 | | ''This section is not completed yet and is a copy of the collection doc''[[br]] |
|---|
| | 107 | |
|---|
| 99 | 108 | The typical, and simplest pattern is |
|---|
| 100 | 109 | |
|---|
| 101 | 110 | {{{ |
|---|
| 102 | 111 | #!d |
|---|
| | 112 | foreach (value; list) |
|---|
| | 113 | Stdout.formatln ("Value: {}", value); |
|---|
| 103 | 114 | foreach (key, value; map) |
|---|
| 104 | 115 | Stdout.formatln ("Key: {}, Value: {}", key, value); |
|---|
| 105 | 116 | }}} |
|---|
| 106 | 117 | |
|---|
| 107 | | One variation is using only 'value' instead of both 'key' and 'value'. |
|---|
| | 118 | For the map, one variation is also using only 'value' instead of both 'key' and 'value'. |
|---|
| 108 | 119 | Both of these utilize a slightly more efficient mechanism to visit each contained element (or element pair), |
|---|
| 109 | 120 | as they can avoid heap activity when instantiating an '''Iterator'''. |
|---|
| 110 | 121 | |
|---|
| 111 | | Collections also expose iterators, supporting both ''foreach'' and a familiar '''more()'''/'''next()''' pattern. |
|---|
| | 122 | Containers also expose iterators, supporting both ''foreach'' and a familiar '''bool next(ref V v)''' pattern. |
|---|
| | 123 | {{{ |
|---|
| | 124 | #!d |
|---|
| | 125 | V value; |
|---|
| | 126 | auto it = list.iterator; |
|---|
| | 127 | while(it.next(value)) |
|---|
| | 128 | Stdout.formatln ("Value: {}", value); |
|---|
| | 129 | }}} |
|---|
| | 130 | '''Note:''' The iterators also support a {{{remove}}} method. |
|---|
| | 131 | |
|---|
| | 132 | |
|---|
| 112 | 133 | Each container exposes method(s) to instantiate an appropriate '''Iterator''' |
|---|
| 113 | 134 | |
|---|
| 114 | 135 | {{{ |
|---|
| 115 | 136 | #!d |
|---|
| 116 | | auto it = map.keys(); |
|---|
| 117 | | |
|---|
| 118 | | // The call to .more returns true if there are more elements, |
|---|
| 119 | | // and switches the iterator to the next one if available. |
|---|
| 120 | | while(it.more){ |
|---|
| 121 | | char[] key; |
|---|
| 122 | | auto value = it.get(key); |
|---|
| | 137 | auto it = map.iterator(); |
|---|
| | 138 | |
|---|
| | 139 | char[] key, value; |
|---|
| | 140 | while(it.next(key, value){ |
|---|
| 123 | 141 | Stdout.formatln ("Key: {}, Value:{}", key, value); |
|---|
| 124 | 142 | } |
|---|
| 125 | 143 | }}} |
|---|
| 126 | 144 | |
|---|
| 127 | | For associative containers, the iterator's '''get''' method has an out parameter for key retrival. |
|---|
| 128 | | |
|---|
| 129 | | The '''keys()''' method is used to also get a set of the key values. For the 'normal' containers, like '''!LinkSeq''' etc. the '''elements()''' method is used to get a conventional iterator. Each container also supports ''foreach()'' directly. |
|---|
| 130 | | |
|---|
| 131 | 145 | === Asynchronous mutation during iteration === |
|---|
| 132 | 146 | ''This section is not completed yet and is a copy of the collection doc''[[br]] |
|---|
| 133 | 147 | |
|---|
| 134 | 148 | Iterators are sensitive to container changes whilst they operate, and will throw an exception when this occurs. This it to ensure the client sees a consistent view of the container for the lifetime of the iterator. |
|---|
| 135 | | |
|---|
| 136 | | === Caution with iterators === |
|---|
| 137 | | ''This section is not completed yet and is a copy of the collection doc''[[br]] |
|---|
| 138 | | |
|---|
| 139 | | It is currently not possible to modify the container while iterating over it with ''foreach'' or an iterator. To delete certain elements from a container it is necessary to do it in two steps. First find the elements to delete, then do the deletion |
|---|
| 140 | | |
|---|
| 141 | | {{{ |
|---|
| 142 | | #!d |
|---|
| 143 | | // 1. Build a list of elements to delete |
|---|
| 144 | | auto dellist = new LinkSeq!(int); |
|---|
| 145 | | foreach( int i; container ){ |
|---|
| 146 | | if( i >= 3 && i < 7 ){ |
|---|
| 147 | | // container.remove( i ); /* NOT POSSIBLE */ |
|---|
| 148 | | dellist.append( i ); |
|---|
| 149 | | } |
|---|
| 150 | | } |
|---|
| 151 | | // 2. Iterate over this deletion list and |
|---|
| 152 | | // delete the items in the original container. |
|---|
| 153 | | foreach( int i; dellist ){ |
|---|
| 154 | | container.remove( i ); |
|---|
| 155 | | } |
|---|
| 156 | | }}} |
|---|
| 157 | | |
|---|
| 158 | | This may be rectified in the future. |
|---|
| 159 | 149 | |
|---|
| 160 | 150 | == !InterleavingIterator == |
|
 |
 |
|
 |
Copyright © 2006-2013 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic