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) fredIter = { fred }; |
---|
22 | Fred & f; |
---|
23 | |
---|
24 | sout | nlOff; // turn off auto newline |
---|
25 | |
---|
26 | for ( ; fredIter >> 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 | nl; |
---|
36 | sout | tail(fred).i | nl; |
---|
37 | |
---|
38 | for ( QueueIter(Fred) iter = { fred }; iter >> f; ) { |
---|
39 | sout | f.i | ' '; |
---|
40 | } |
---|
41 | sout | nl; |
---|
42 | |
---|
43 | for ( i; 9 ) { |
---|
44 | delete( &drop( fred ) ); |
---|
45 | } |
---|
46 | |
---|
47 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
48 | sout | f.i | ' '; |
---|
49 | } |
---|
50 | sout | nl; |
---|
51 | |
---|
52 | for ( i; 10 ) { |
---|
53 | add( fred, *new( 2 * i + 1 ) ); |
---|
54 | } |
---|
55 | |
---|
56 | Fred * front = new( -1 ); |
---|
57 | addHead( fred, *front ); |
---|
58 | sout | succ( fred, front )->i | nl; |
---|
59 | remove( fred, *front ); |
---|
60 | delete( front ); |
---|
61 | |
---|
62 | Fred & end = dropTail( fred ); |
---|
63 | delete( &end ); |
---|
64 | |
---|
65 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
66 | sout | f.i | ' '; |
---|
67 | } |
---|
68 | sout | nl; |
---|
69 | |
---|
70 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
71 | delete( &f ); |
---|
72 | } |
---|
73 | |
---|
74 | Queue(Fred) fred0; |
---|
75 | Fred * middle; |
---|
76 | for ( i; 10 ) { |
---|
77 | if( i == 5) { |
---|
78 | middle = new( i ); |
---|
79 | add( fred0, *middle ); |
---|
80 | continue; |
---|
81 | } |
---|
82 | add( fred0, *new( i ) ); |
---|
83 | } |
---|
84 | |
---|
85 | for ( QueueIter(Fred) iter = { fred0 }; iter >> f; ) { |
---|
86 | sout | f.i | ' '; |
---|
87 | } |
---|
88 | sout | nl; |
---|
89 | |
---|
90 | Queue(Fred) fred2; |
---|
91 | |
---|
92 | split( fred2, fred0, *middle); |
---|
93 | |
---|
94 | for ( over( fredIter, fred0 ); fredIter >> f; ) { |
---|
95 | sout | f.i | ' '; |
---|
96 | } |
---|
97 | sout | nl; |
---|
98 | |
---|
99 | for ( over( fredIter, fred2 ); fredIter >> f; ) { |
---|
100 | sout | f.i | ' '; |
---|
101 | } |
---|
102 | sout | nl; |
---|
103 | |
---|
104 | transfer( fred0, fred2); |
---|
105 | |
---|
106 | for ( over( fredIter, fred0 ); fredIter >> f; ) { |
---|
107 | sout | f.i | ' '; |
---|
108 | } |
---|
109 | sout | nl; |
---|
110 | |
---|
111 | for ( over( fredIter, fred0 ); fredIter >> f; ) { |
---|
112 | delete( &f ); |
---|
113 | } |
---|
114 | |
---|
115 | // Mary test |
---|
116 | |
---|
117 | struct Mary { |
---|
118 | inline Fred; // Plan 9 inheritance |
---|
119 | int j; |
---|
120 | }; |
---|
121 | void ?{}( Mary & mary ) { abort(); } |
---|
122 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
123 | ((Fred &)mary){ p }; |
---|
124 | j = i = p; |
---|
125 | } |
---|
126 | |
---|
127 | Mary *& Next( Mary * n ) { |
---|
128 | return (Mary *)Next( (Fred *)n ); |
---|
129 | } |
---|
130 | |
---|
131 | Queue(Mary) mary; |
---|
132 | QueueIter(Mary) maryIter = { mary }; |
---|
133 | Mary & m; |
---|
134 | |
---|
135 | for ( ; maryIter >> m; ) { // empty list |
---|
136 | sout | m.i | m.j | ' '; |
---|
137 | } |
---|
138 | sout | "empty" | nl; |
---|
139 | |
---|
140 | for ( i; 10 ) { |
---|
141 | add( mary, *new( 2 * i ) ); |
---|
142 | } |
---|
143 | |
---|
144 | for ( QueueIter(Mary) iter = { mary }; iter >> m; ) { |
---|
145 | sout | m.i | m.j | ' '; |
---|
146 | } |
---|
147 | sout | nl; |
---|
148 | |
---|
149 | for ( i; 9 ) { |
---|
150 | delete( &drop( mary ) ); |
---|
151 | } |
---|
152 | |
---|
153 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
154 | sout | m.i | m.j | ' '; |
---|
155 | } |
---|
156 | sout | nl; |
---|
157 | |
---|
158 | for ( i; 10 ) { |
---|
159 | add( mary, *new( 2 * i + 1 ) ); |
---|
160 | } |
---|
161 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
162 | sout | m.i | m.j | ' '; |
---|
163 | } |
---|
164 | sout | nl; |
---|
165 | |
---|
166 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
167 | delete( &m ); |
---|
168 | } |
---|
169 | } |
---|