1 | #include <fstream.hfa> |
---|
2 | #include <stdlib.hfa> // new, delete |
---|
3 | #include <bits/sequence.hfa> |
---|
4 | |
---|
5 | int main() { |
---|
6 | // Fred test |
---|
7 | |
---|
8 | struct Fred { |
---|
9 | inline Seqable; // Plan 9 inheritance |
---|
10 | int i; |
---|
11 | }; |
---|
12 | void ?{}( Fred & fred ) { abort(); } |
---|
13 | void ?{}( Fred & fred, int p ) with( fred ) { |
---|
14 | i = p; |
---|
15 | } |
---|
16 | Fred *& Back( Fred * n ) { |
---|
17 | return (Fred *)Back( (Seqable *)n ); |
---|
18 | } |
---|
19 | Fred *& Next( Fred * n ) { |
---|
20 | return (Fred *)Next( (Colable *)n ); |
---|
21 | } |
---|
22 | |
---|
23 | Sequence(Fred) fred; |
---|
24 | SeqIter(Fred) iter = { fred }; |
---|
25 | Fred & f; |
---|
26 | |
---|
27 | sout | nlOff; // turn off auto newline |
---|
28 | |
---|
29 | for ( ; iter | f; ) { // empty list |
---|
30 | sout | f.i | ' '; |
---|
31 | } |
---|
32 | sout | "empty" | nl; |
---|
33 | |
---|
34 | for ( i; 10 ) { |
---|
35 | add( fred, *new( 2 * i ) ); |
---|
36 | } |
---|
37 | |
---|
38 | sout | head( fred ).i | tail( fred ).i | nl; |
---|
39 | |
---|
40 | for ( SeqIter(Fred) iter = { fred }; iter | f; ) { |
---|
41 | sout | f.i | ' '; |
---|
42 | } |
---|
43 | sout | nl; |
---|
44 | |
---|
45 | for ( i; 9 ) { |
---|
46 | delete( &dropHead( fred ) ); |
---|
47 | } |
---|
48 | |
---|
49 | for ( over( iter, fred ); iter | f; ) { |
---|
50 | sout | f.i | ' '; |
---|
51 | } |
---|
52 | sout | nl; |
---|
53 | |
---|
54 | for ( i; 10 ) { |
---|
55 | addTail( fred, *new( 2 * i + 1 ) ); |
---|
56 | } |
---|
57 | |
---|
58 | Fred * head = new( -1 ), tail = { -2 }; |
---|
59 | addHead( fred, *head ); |
---|
60 | addTail( fred, tail ); |
---|
61 | |
---|
62 | sout | head( fred ).i | succ( fred, head )->i | tail( fred ).i | nl; |
---|
63 | |
---|
64 | for ( over( iter, fred ); iter | f; ) { |
---|
65 | sout | f.i | ' '; |
---|
66 | } |
---|
67 | sout | nl; |
---|
68 | |
---|
69 | remove( fred, *head ); |
---|
70 | remove( fred, tail ); |
---|
71 | delete( head ); |
---|
72 | delete( &dropTail( fred ) ); |
---|
73 | |
---|
74 | for ( over( iter, fred ); iter | f; ) { |
---|
75 | sout | f.i | ' '; |
---|
76 | } |
---|
77 | sout | nl; |
---|
78 | |
---|
79 | for ( i; 5 ) { |
---|
80 | delete( &dropTail( fred ) ); |
---|
81 | } |
---|
82 | for ( over( iter, fred ); iter | f; ) { |
---|
83 | sout | f.i | ' '; |
---|
84 | } |
---|
85 | sout | nl; |
---|
86 | |
---|
87 | for ( over( iter, fred ); iter | f; ) { |
---|
88 | delete( &remove( fred, f ) ); |
---|
89 | } |
---|
90 | for ( over( iter, fred ); iter | f; ) { |
---|
91 | sout | f.i | ' '; |
---|
92 | } |
---|
93 | sout | "empty" | nl; |
---|
94 | |
---|
95 | Fred & middle; |
---|
96 | for ( i; 10 ) { |
---|
97 | addHead( fred, *new( i ) ); // reverse order |
---|
98 | if ( i == 5 ) { |
---|
99 | &middle = &head( fred ); |
---|
100 | } |
---|
101 | } |
---|
102 | for ( SeqIterRev(Fred) riter = { fred }; riter | f; ) { |
---|
103 | sout | f.i | ' '; |
---|
104 | } |
---|
105 | sout | nl; |
---|
106 | |
---|
107 | head = new( -1 ); |
---|
108 | insertBef( fred, *head, head( fred ) ); |
---|
109 | insertAft( fred, *head, tail ); |
---|
110 | |
---|
111 | sout | head( fred ).i | succ( fred, head )->i | tail( fred ).i | nl; |
---|
112 | for ( over( iter, fred ); iter | f; ) { |
---|
113 | sout | f.i | ' '; |
---|
114 | } |
---|
115 | sout | nl; |
---|
116 | |
---|
117 | remove( fred, *head ); |
---|
118 | remove( fred, tail ); |
---|
119 | delete( head ); |
---|
120 | delete( &dropTail( fred ) ); |
---|
121 | |
---|
122 | for ( over( iter, fred ); iter | f; ) { |
---|
123 | sout | f.i | ' '; |
---|
124 | } |
---|
125 | sout | nl; |
---|
126 | |
---|
127 | Sequence(Fred) fred2; |
---|
128 | |
---|
129 | split( fred2, fred, middle ); |
---|
130 | |
---|
131 | for ( over( iter, fred ); iter | f; ) { |
---|
132 | sout | f.i | ' '; |
---|
133 | } |
---|
134 | sout | nl; |
---|
135 | |
---|
136 | for ( over( iter, fred2 ); iter | f; ) { |
---|
137 | sout | f.i | ' '; |
---|
138 | } |
---|
139 | sout | nl; |
---|
140 | |
---|
141 | transfer( fred, fred2 ); |
---|
142 | |
---|
143 | for ( over( iter, fred ); iter | f; ) { |
---|
144 | sout | f.i | ' '; |
---|
145 | } |
---|
146 | sout | nl; |
---|
147 | |
---|
148 | for ( over( iter, fred ); iter | f; ) { |
---|
149 | delete( &f ); |
---|
150 | } |
---|
151 | |
---|
152 | // Mary test |
---|
153 | |
---|
154 | struct Mary { |
---|
155 | inline Fred; // Plan 9 inheritance |
---|
156 | int j; |
---|
157 | }; |
---|
158 | void ?{}( Mary & mary ) { abort(); } |
---|
159 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
160 | ((Fred &)mary){ p }; |
---|
161 | j = p; |
---|
162 | } |
---|
163 | Mary *& Back( Mary * n ) { |
---|
164 | return (Mary *)Back( (Fred *)n ); |
---|
165 | } |
---|
166 | Mary *& Next( Mary * n ) { |
---|
167 | return (Mary *)Next( (Fred *)n ); |
---|
168 | } |
---|
169 | |
---|
170 | Sequence(Mary) mary; |
---|
171 | SeqIter(Mary) iter = { mary }; |
---|
172 | Mary & m; |
---|
173 | |
---|
174 | for ( ; iter | m; ) { // empty list |
---|
175 | sout | m.i | m.j | ' '; |
---|
176 | } |
---|
177 | sout | "empty" | nl; |
---|
178 | |
---|
179 | for ( i; 10 ) { |
---|
180 | add( mary, *new( 2 * i ) ); |
---|
181 | } |
---|
182 | |
---|
183 | sout | head( mary ).i | tail( mary ).i | nl; |
---|
184 | |
---|
185 | for ( SeqIter(Mary) iter = { mary }; iter | m; ) { |
---|
186 | sout | m.i | m.j | ' '; |
---|
187 | } |
---|
188 | sout | nl; |
---|
189 | |
---|
190 | for ( i; 9 ) { |
---|
191 | delete( &dropHead( mary ) ); |
---|
192 | } |
---|
193 | |
---|
194 | for ( over( iter, mary ); iter | m; ) { |
---|
195 | sout | m.i | m.j | ' '; |
---|
196 | } |
---|
197 | sout | nl; |
---|
198 | |
---|
199 | for ( i; 10 ) { |
---|
200 | addTail( mary, *new( 2 * i + 1 ) ); |
---|
201 | } |
---|
202 | |
---|
203 | Mary * head = new( -1 ), tail = { -2 }; |
---|
204 | addHead( mary, *head ); |
---|
205 | addTail( mary, tail ); |
---|
206 | |
---|
207 | sout | head( mary ).i | succ( mary, head )->i | tail( mary ).i | nl; |
---|
208 | |
---|
209 | for ( over( iter, mary ); iter | m; ) { |
---|
210 | sout | m.i | m.j | ' '; |
---|
211 | } |
---|
212 | sout | nl; |
---|
213 | |
---|
214 | remove( mary, *head ); |
---|
215 | remove( mary, tail ); |
---|
216 | delete( (Mary *)head ); |
---|
217 | delete( &dropTail( mary ) ); |
---|
218 | |
---|
219 | for ( over( iter, mary ); iter | m; ) { |
---|
220 | sout | m.i | m.j | ' '; |
---|
221 | } |
---|
222 | sout | nl; |
---|
223 | |
---|
224 | for ( i; 5 ) { |
---|
225 | delete( &dropTail( mary ) ); |
---|
226 | } |
---|
227 | for ( over( iter, mary ); iter | m; ) { |
---|
228 | sout | m.i | m.j | ' '; |
---|
229 | } |
---|
230 | sout | nl; |
---|
231 | |
---|
232 | for ( over( iter, mary ); iter | m; ) { |
---|
233 | delete( &remove( mary, m ) ); |
---|
234 | } |
---|
235 | for ( over( iter, mary ); iter | m; ) { |
---|
236 | sout | m.i | m.j | ' '; |
---|
237 | } |
---|
238 | sout | "empty" | nl; |
---|
239 | |
---|
240 | Mary & middle; |
---|
241 | for ( i; 10 ) { |
---|
242 | add( mary, *new( i ) ); |
---|
243 | if ( i == 4 ) { |
---|
244 | &middle = &tail( mary ); |
---|
245 | } |
---|
246 | } |
---|
247 | for ( SeqIter(Mary) iter = { mary }; iter | m; ) { |
---|
248 | sout | m.i | m.j | ' '; |
---|
249 | } |
---|
250 | sout | nl; |
---|
251 | |
---|
252 | Sequence(Mary) mary2; |
---|
253 | |
---|
254 | split( mary2, mary, middle ); |
---|
255 | |
---|
256 | for ( over( iter, mary ); iter | m; ) { |
---|
257 | sout | m.i | m.j | ' '; |
---|
258 | } |
---|
259 | sout | nl; |
---|
260 | for ( over( iter, mary2 ); iter | m; ) { |
---|
261 | sout | m.i | m.j | ' '; |
---|
262 | } |
---|
263 | sout | nl; |
---|
264 | |
---|
265 | transfer( mary, mary2 ); |
---|
266 | |
---|
267 | for ( over( iter, mary ); iter | m; ) { |
---|
268 | sout | m.i | m.j | ' '; |
---|
269 | } |
---|
270 | sout | nl; |
---|
271 | |
---|
272 | for ( over( iter, mary ); iter | m; ) { |
---|
273 | delete( &m ); |
---|
274 | } |
---|
275 | } |
---|