1 | #include <fstream.hfa> |
---|
2 | #include <stdlib.hfa> // new, delete |
---|
3 | #include <bits/stack.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 | Stack(Fred) fred; |
---|
21 | StackIter(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 | push( fred, *new( 2 * i ) ); |
---|
33 | } |
---|
34 | |
---|
35 | for ( StackIter(Fred) iter = { fred }; iter >> f; ) { |
---|
36 | sout | f.i | ' '; |
---|
37 | } |
---|
38 | sout | nl; |
---|
39 | |
---|
40 | sout | head(fred).i | nl; |
---|
41 | |
---|
42 | for ( i; 9 ) { |
---|
43 | delete( &pop( fred ) ); |
---|
44 | } |
---|
45 | |
---|
46 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
47 | sout | f.i | ' '; |
---|
48 | } |
---|
49 | sout | nl; |
---|
50 | |
---|
51 | for ( i; 10 ) { |
---|
52 | push( fred, *new( 2 * i + 1 ) ); |
---|
53 | } |
---|
54 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
55 | sout | f.i | ' '; |
---|
56 | } |
---|
57 | sout | nl; |
---|
58 | |
---|
59 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
60 | delete( &f ); |
---|
61 | } |
---|
62 | |
---|
63 | // Mary test |
---|
64 | |
---|
65 | struct Mary { |
---|
66 | inline Fred; // Plan 9 inheritance |
---|
67 | int j; |
---|
68 | }; |
---|
69 | void ?{}( Mary & mary ) { abort(); } |
---|
70 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
71 | ((Fred &)mary){ p }; |
---|
72 | j = i = p; |
---|
73 | } |
---|
74 | |
---|
75 | Mary *& Next( Mary * n ) { |
---|
76 | return (Mary *)Next( (Fred *)n ); |
---|
77 | } |
---|
78 | |
---|
79 | Stack(Mary) mary; |
---|
80 | StackIter(Mary) maryIter = { mary }; |
---|
81 | Mary & m; |
---|
82 | |
---|
83 | for ( ; maryIter >> m; ) { // empty list |
---|
84 | sout | m.i | m.j | ' '; |
---|
85 | } |
---|
86 | sout | "empty" | nl; |
---|
87 | |
---|
88 | for ( i; 10 ) { |
---|
89 | push( mary, *new( 2 * i ) ); |
---|
90 | } |
---|
91 | |
---|
92 | for ( StackIter(Mary) iter = { mary }; iter >> m; ) { |
---|
93 | sout | m.i | m.j | ' '; |
---|
94 | } |
---|
95 | sout | nl; |
---|
96 | |
---|
97 | for ( i; 9 ) { |
---|
98 | delete( &pop( mary ) ); |
---|
99 | } |
---|
100 | |
---|
101 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
102 | sout | m.i | m.j | ' '; |
---|
103 | } |
---|
104 | sout | nl; |
---|
105 | |
---|
106 | for ( i; 10 ) { |
---|
107 | push( mary, *new( 2 * i + 1 ) ); |
---|
108 | } |
---|
109 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
110 | sout | m.i | m.j | ' '; |
---|
111 | } |
---|
112 | sout | nl; |
---|
113 | |
---|
114 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
115 | delete( &m ); |
---|
116 | } |
---|
117 | } |
---|