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