132
|
1 module example.reference.chapter11;
|
|
2
|
|
3 import tango.util.collection.HashMap;
|
|
4 import tango.util.collection.ArrayBag;
|
|
5 import tango.util.collection.LinkSeq;
|
|
6 import tango.util.collection.CircularSeq;
|
|
7 import tango.util.collection.ArraySeq;
|
|
8 import tango.util.collection.TreeBag;
|
|
9 import tango.util.collection.iterator.FilteringIterator;
|
|
10 import tango.util.collection.iterator.InterleavingIterator;
|
|
11
|
|
12 import tango.io.Stdout;
|
|
13 import tango.core.Exception;
|
|
14
|
|
15 import tango.util.collection.model.Comparator;
|
|
16 import tango.util.collection.impl.BagCollection;
|
|
17 import tango.util.collection.impl.SeqCollection;
|
|
18 import Ascii = tango.text.Ascii;
|
|
19
|
|
20 void linkedListExample(){
|
|
21 Stdout.format( "linkedListExample" ).newline;
|
|
22 alias LinkSeq!(char[]) StrList;
|
|
23 StrList lst = new StrList();
|
|
24
|
|
25 lst.append( "value1" );
|
|
26 lst.append( "value2" );
|
|
27 lst.append( "value3" );
|
|
28
|
|
29 auto it = lst.elements();
|
|
30 // The call to .more gives true, if there are more elements
|
|
31 // and switches the iterator to the next one if available.
|
|
32 while( it.more ){
|
|
33 char[] item_value = it.get;
|
|
34 Stdout.format( "Value:{0}", item_value ).newline;
|
|
35 }
|
|
36 }
|
|
37
|
|
38 void hashMapExample(){
|
|
39 Stdout.format( "hashMapExample" ).newline;
|
|
40 alias HashMap!(char[], char[]) StrStrMap;
|
|
41 StrStrMap map = new StrStrMap();
|
|
42 map.add( "key1", "value1" );
|
|
43 char[] key = "key1";
|
|
44 Stdout.format( "Key: {0}, Value:{1}", key, map.get( key )).newline;
|
|
45
|
|
46
|
|
47 auto it = map.keys();
|
|
48 // The call to .more gives true, if there are more elements
|
|
49 // and switches the iterator to the next one if available.
|
|
50 while( it.more ){
|
|
51 char[] item_key = void;
|
|
52 char[] item_value = it.get( item_key ); // only for maps, the key is returns via inout
|
|
53 Stdout.format( "Key: {0}, Value:{1}", item_key, item_value ).newline;
|
|
54 }
|
|
55 }
|
|
56
|
|
57 void testComparator(){
|
|
58 char[][] result;
|
|
59
|
|
60 // Create and fill the containers
|
|
61 auto nameSet = new TreeBag!(char[])( null, new class() Comparator!(char[]){
|
|
62 int compare( char[] first, char[] second ){
|
|
63 return Ascii.icompare( first, second );
|
|
64 }
|
|
65 });
|
|
66
|
|
67 nameSet.addIf( "Alice" );
|
|
68 nameSet.addIf( "Bob" );
|
|
69 nameSet.addIf( "aliCe" );
|
|
70
|
|
71 // use foreach to iterate over the container
|
|
72 foreach ( char[] i; nameSet )
|
|
73 result ~= i;
|
|
74 foreach( char[] i; result ) Stdout.format( "{0} ", i );
|
|
75 Stdout.newline;
|
|
76
|
|
77 // test the result
|
|
78 assert( result == [ "Alice", "Bob" ], "testIterator" );
|
|
79 }
|
|
80
|
|
81 void testSreener(){
|
|
82 int[] result;
|
|
83
|
|
84 // Create and fill the containers
|
|
85 auto ratioSamples = new ArraySeq!(float)( (float v){
|
|
86 return v >= 0.0f && v < 1.0f;
|
|
87 });
|
|
88
|
|
89 ratioSamples.append( 0.0f );
|
|
90 ratioSamples.append( 0.5f );
|
|
91 ratioSamples.append( 0.99999f );
|
|
92 // try to insert a value that is not allowed
|
|
93 try{
|
|
94 ratioSamples.append( 1.0f );
|
|
95 // will never get here
|
|
96 assert( false );
|
|
97 } catch( IllegalElementException e ){
|
|
98 }
|
|
99 }
|
|
100
|
|
101
|
|
102 void testForeach(){
|
|
103 int[] result;
|
|
104
|
|
105 // Create and fill the containers
|
|
106 auto l1 = new CircularSeq!(int);
|
|
107 for( int i = 0; i < 6; i+=2 ){
|
|
108 l1.append( i );
|
|
109 }
|
|
110
|
|
111 // use foreach to iterate over the container
|
|
112 foreach ( int i; l1 )
|
|
113 result ~= i;
|
|
114 // foreach_reverse ( int i; l1 )
|
|
115 // result ~= i;
|
|
116
|
|
117 // test the result
|
|
118 assert( result == [ 0, 2, 4 ], "testIterator" );
|
|
119 }
|
|
120
|
|
121 void testIterator(){
|
|
122 int[] result;
|
|
123
|
|
124 // Create and fill the containers
|
|
125 auto l1 = new LinkSeq!(int);
|
|
126 for( int i = 0; i < 6; i+=2 ){
|
|
127 l1.append( i );
|
|
128 }
|
|
129
|
|
130 // define the Iterator
|
|
131 auto it = l1.elements();
|
|
132
|
|
133 // use the Iterator to iterate over the container
|
|
134 while (it.more())
|
|
135 result ~= it.get();
|
|
136
|
|
137 // test the result
|
|
138 assert( result == [ 0, 2, 4 ], "testIterator" );
|
|
139 }
|
|
140
|
|
141 void testFilteringIterator(){
|
|
142 int[] result;
|
|
143 alias ArrayBag!(int) IntBag;
|
|
144
|
|
145 // Create and fill the container
|
|
146 auto ib = new IntBag;
|
|
147 for( int i = 0; i < 20; i++ ){
|
|
148 ib.add( i );
|
|
149 }
|
|
150
|
|
151 // define the FilteringIterator with a function literal
|
|
152 auto it = new FilteringIterator!(int)( ib.elements(), (int i){
|
|
153 return i >= 3 && i < 7;
|
|
154 });
|
|
155
|
|
156 // use the Iterator with the more()/get() pattern
|
|
157 while (it.more())
|
|
158 result ~= it.get();
|
|
159
|
|
160 // test the result
|
|
161 assert( result == [ 3, 4, 5, 6 ], "testFilteringIterator" );
|
|
162
|
|
163 }
|
|
164
|
|
165 void testFilteringIteratorRemove(){
|
|
166 int[] result;
|
|
167
|
|
168 // Create and fill the container
|
|
169 auto container = new LinkSeq!(int);
|
|
170 for( int i = 0; i < 10; i++ ){
|
|
171 container.append( i );
|
|
172 }
|
|
173
|
|
174 // 1. Build a list of elements to delete
|
|
175 auto dellist = new LinkSeq!(int);
|
|
176 foreach( int i; container ){
|
|
177 if( i < 3 || i >= 7 ){
|
|
178 // container.remove( i ); /* NOT POSSIBLE */
|
|
179 dellist.append( i );
|
|
180 }
|
|
181 }
|
|
182 // 2. Iterate over this deletion list and
|
|
183 // delete the items in the original container.
|
|
184 foreach( int i; dellist ){
|
|
185 container.remove( i );
|
|
186 }
|
|
187
|
|
188 foreach ( int i; container )
|
|
189 result ~= i;
|
|
190
|
|
191 // test the result
|
|
192 assert( result == [ 3, 4, 5, 6 ], "testFilteringIterator" );
|
|
193
|
|
194 }
|
|
195
|
|
196 void testInterleavingIterator(){
|
|
197 int[] result;
|
|
198
|
|
199 // Create and fill the containers
|
|
200 auto l1 = new LinkSeq!(int);
|
|
201 auto l2 = new ArraySeq!(int);
|
|
202 for( int i = 0; i < 6; i+=2 ){
|
|
203 l1.append( i );
|
|
204 l2.append( i+1 );
|
|
205 }
|
|
206
|
|
207 // define the InterleavingIterator
|
|
208 auto it = new InterleavingIterator!(int)( l1.elements(), l2.elements() );
|
|
209
|
|
210 // use the InterleavingIterator to iterate over the container
|
|
211 while (it.more())
|
|
212 result ~= it.get();
|
|
213
|
|
214 // test the result
|
|
215 assert( result == [ 0, 1, 2, 3, 4, 5 ], "testInterleavingIterator" );
|
|
216 }
|
|
217
|
|
218 // foreach( int i; result ) Stdout.format( "{0} ", i );
|
|
219 // Stdout.newline;
|
|
220
|
|
221 void main(){
|
|
222 Stdout.format( "reference - Chapter 11 Example" ).newline;
|
|
223 hashMapExample();
|
|
224 linkedListExample();
|
|
225
|
|
226 testSreener();
|
|
227 testComparator();
|
|
228 testForeach();
|
|
229 testIterator();
|
|
230 testFilteringIterator();
|
|
231 testInterleavingIterator();
|
|
232 testFilteringIteratorRemove();
|
|
233
|
|
234 Stdout.format( "=== End ===" ).newline;
|
|
235 }
|
|
236
|
|
237
|