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 | bool listed( Fred * n ) { |
---|
21 | return Next( (Colable *)n ) != 0p; |
---|
22 | } |
---|
23 | |
---|
24 | Queue(Fred) fred; |
---|
25 | QueueIter(Fred) fredIter = { fred }; |
---|
26 | Fred & f; |
---|
27 | |
---|
28 | sout | nlOff; // turn off auto newline |
---|
29 | |
---|
30 | for ( ; fredIter >> f; ) { // empty list |
---|
31 | sout | f.i | ' '; |
---|
32 | } |
---|
33 | sout | "empty" | nl; |
---|
34 | |
---|
35 | for ( i; 10 ) { |
---|
36 | add( fred, *new( 2 * i ) ); |
---|
37 | } |
---|
38 | |
---|
39 | for ( QueueIter(Fred) iter = { fred }; iter >> f; ) { |
---|
40 | sout | f.i | ' '; |
---|
41 | } |
---|
42 | sout | nl; |
---|
43 | |
---|
44 | for ( i; 9 ) { |
---|
45 | delete( &drop( fred ) ); |
---|
46 | } |
---|
47 | |
---|
48 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
49 | sout | f.i | ' '; |
---|
50 | } |
---|
51 | sout | nl; |
---|
52 | |
---|
53 | for ( i; 10 ) { |
---|
54 | add( fred, *new( 2 * i + 1 ) ); |
---|
55 | } |
---|
56 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
57 | sout | f.i | ' '; |
---|
58 | } |
---|
59 | sout | nl; |
---|
60 | |
---|
61 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
62 | delete( &f ); |
---|
63 | } |
---|
64 | |
---|
65 | // Mary test |
---|
66 | |
---|
67 | struct Mary { |
---|
68 | inline Fred; // Plan 9 inheritance |
---|
69 | int j; |
---|
70 | }; |
---|
71 | void ?{}( Mary & mary ) { abort(); } |
---|
72 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
73 | ((Fred &)mary){ p }; |
---|
74 | j = i = p; |
---|
75 | } |
---|
76 | |
---|
77 | Mary *& Next( Mary * n ) { |
---|
78 | return (Mary *)Next( (Colable *)n ); |
---|
79 | } |
---|
80 | |
---|
81 | bool listed( Mary * n ) { |
---|
82 | return Next( (Colable *)n ) != 0p; |
---|
83 | } |
---|
84 | |
---|
85 | Queue(Mary) mary; |
---|
86 | QueueIter(Mary) maryIter = { mary }; |
---|
87 | Mary & m; |
---|
88 | |
---|
89 | for ( ; maryIter >> m; ) { // empty list |
---|
90 | sout | m.i | m.j | ' '; |
---|
91 | } |
---|
92 | sout | "empty" | nl; |
---|
93 | |
---|
94 | for ( i; 10 ) { |
---|
95 | add( mary, *new( 2 * i ) ); |
---|
96 | } |
---|
97 | |
---|
98 | for ( QueueIter(Mary) iter = { mary }; iter >> m; ) { |
---|
99 | sout | m.i | m.j | ' '; |
---|
100 | } |
---|
101 | sout | nl; |
---|
102 | |
---|
103 | for ( i; 9 ) { |
---|
104 | delete( &drop( mary ) ); |
---|
105 | } |
---|
106 | |
---|
107 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
108 | sout | m.i | m.j | ' '; |
---|
109 | } |
---|
110 | sout | nl; |
---|
111 | |
---|
112 | for ( i; 10 ) { |
---|
113 | add( mary, *new( 2 * i + 1 ) ); |
---|
114 | } |
---|
115 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
116 | sout | m.i | m.j | ' '; |
---|
117 | } |
---|
118 | sout | nl; |
---|
119 | |
---|
120 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
121 | delete( &m ); |
---|
122 | } |
---|
123 | } |
---|