1 | #include <fstream.hfa> |
---|
2 | #include <stdlib.hfa> // new, delete |
---|
3 | #include "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 | |
---|
17 | Sequence(Fred) fred; |
---|
18 | SeqIter(Fred) fredIter = { fred }; |
---|
19 | Fred & f; |
---|
20 | |
---|
21 | sout | nlOff; // turn off auto newline |
---|
22 | |
---|
23 | for ( ; fredIter >> f; ) { // empty list |
---|
24 | sout | f.i | ' '; |
---|
25 | } |
---|
26 | sout | "empty" | nl; |
---|
27 | |
---|
28 | for ( i; 10 ) { |
---|
29 | add( fred, *new( 2 * i ) ); |
---|
30 | } |
---|
31 | |
---|
32 | for ( SeqIter(Fred) iter = { fred }; iter >> f; ) { |
---|
33 | sout | f.i | ' '; |
---|
34 | } |
---|
35 | sout | nl; |
---|
36 | |
---|
37 | for ( i; 9 ) { |
---|
38 | delete( &dropHead( fred ) ); |
---|
39 | } |
---|
40 | |
---|
41 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
42 | sout | f.i | ' '; |
---|
43 | } |
---|
44 | sout | nl; |
---|
45 | |
---|
46 | for ( i; 10 ) { |
---|
47 | addTail( fred, *new( 2 * i + 1 ) ); |
---|
48 | } |
---|
49 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
50 | sout | f.i | ' '; |
---|
51 | } |
---|
52 | sout | nl; |
---|
53 | |
---|
54 | for ( i; 9 ) { |
---|
55 | delete( &dropTail( fred ) ); |
---|
56 | } |
---|
57 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
58 | sout | f.i | ' '; |
---|
59 | } |
---|
60 | sout | nl; |
---|
61 | |
---|
62 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
63 | delete( &f ); |
---|
64 | } |
---|
65 | |
---|
66 | // Mary test |
---|
67 | |
---|
68 | struct Mary { |
---|
69 | inline Fred; // Plan 9 inheritance |
---|
70 | int j; |
---|
71 | }; |
---|
72 | void ?{}( Mary & mary ) { abort(); } |
---|
73 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
74 | ((Fred &)mary){ p }; |
---|
75 | j = p; |
---|
76 | } |
---|
77 | |
---|
78 | Sequence(Mary) mary; |
---|
79 | Sequence(Mary) baz; |
---|
80 | SeqIter(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 | add( mary, *new( 2 * i ) ); |
---|
90 | add( baz, *new( 2 * i ) ); |
---|
91 | } |
---|
92 | |
---|
93 | for ( SeqIter(Mary) iter = { mary }; iter >> m; ) { |
---|
94 | sout | m.i | m.j | ' '; |
---|
95 | } |
---|
96 | sout | nl; |
---|
97 | |
---|
98 | for ( i; 9 ) { |
---|
99 | delete( &dropHead( mary ) ); |
---|
100 | } |
---|
101 | |
---|
102 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
103 | sout | m.i | m.j | ' '; |
---|
104 | } |
---|
105 | sout | nl; |
---|
106 | |
---|
107 | for ( i; 10 ) { |
---|
108 | addTail( mary, *new( 2 * i + 1 ) ); |
---|
109 | } |
---|
110 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
111 | sout | m.i | m.j | ' '; |
---|
112 | } |
---|
113 | sout | nl; |
---|
114 | |
---|
115 | for ( i; 9 ) { |
---|
116 | delete( &dropTail( mary ) ); |
---|
117 | } |
---|
118 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
119 | sout | m.i | m.j | ' '; |
---|
120 | } |
---|
121 | sout | nl; |
---|
122 | |
---|
123 | transfer( mary, baz ); |
---|
124 | |
---|
125 | for ( over( maryIter, baz ); maryIter >> m; ) { |
---|
126 | sout | m.i | m.j | ' '; |
---|
127 | } |
---|
128 | sout | "empty" | nl; |
---|
129 | |
---|
130 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
131 | sout | m.i | m.j | ' '; |
---|
132 | } |
---|
133 | sout | nl; |
---|
134 | |
---|
135 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
136 | delete( &m ); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | // Local Variables: // |
---|
141 | // compile-command: "cfa sequence_example.cfa" // |
---|
142 | // End: // |
---|