[5e82d56] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <stdlib.hfa> // new, delete |
---|
[edf3ff1] | 3 | #include <bits/stack.hfa> |
---|
[5e82d56] | 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 | |
---|
| 17 | Stack(Fred) fred; |
---|
| 18 | StackIter(Fred) fredIter = { fred }; |
---|
[3d0560d] | 19 | Fred & f; |
---|
[5e82d56] | 20 | |
---|
| 21 | sout | nlOff; // turn off auto newline |
---|
| 22 | |
---|
| 23 | for ( ; fredIter >> f; ) { // empty list |
---|
[3d0560d] | 24 | sout | f.i | ' '; |
---|
[5e82d56] | 25 | } |
---|
| 26 | sout | "empty" | nl; |
---|
| 27 | |
---|
[3d0560d] | 28 | for ( i; 10 ) { |
---|
[636d3715] | 29 | push( fred, *new( 2 * i ) ); |
---|
[5e82d56] | 30 | } |
---|
| 31 | |
---|
[3d0560d] | 32 | for ( StackIter(Fred) iter = { fred }; iter >> f; ) { |
---|
| 33 | sout | f.i | ' '; |
---|
[5e82d56] | 34 | } |
---|
| 35 | sout | nl; |
---|
| 36 | |
---|
[3d0560d] | 37 | for ( i; 9 ) { |
---|
[636d3715] | 38 | delete( &pop( fred ) ); |
---|
[5e82d56] | 39 | } |
---|
| 40 | |
---|
| 41 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[3d0560d] | 42 | sout | f.i | ' '; |
---|
[5e82d56] | 43 | } |
---|
| 44 | sout | nl; |
---|
| 45 | |
---|
[3d0560d] | 46 | for ( i; 10 ) { |
---|
[636d3715] | 47 | push( fred, *new( 2 * i + 1 ) ); |
---|
[5e82d56] | 48 | } |
---|
| 49 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[3d0560d] | 50 | sout | f.i | ' '; |
---|
[5e82d56] | 51 | } |
---|
| 52 | sout | nl; |
---|
| 53 | |
---|
| 54 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[3d0560d] | 55 | delete( &f ); |
---|
[5e82d56] | 56 | } |
---|
| 57 | |
---|
| 58 | // Mary test |
---|
| 59 | |
---|
| 60 | struct Mary { |
---|
| 61 | inline Fred; // Plan 9 inheritance |
---|
| 62 | int j; |
---|
| 63 | }; |
---|
| 64 | void ?{}( Mary & mary ) { abort(); } |
---|
| 65 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
| 66 | ((Fred &)mary){ p }; |
---|
| 67 | j = i = p; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | Stack(Mary) mary; |
---|
| 71 | StackIter(Mary) maryIter = { mary }; |
---|
[3d0560d] | 72 | Mary & m; |
---|
[5e82d56] | 73 | |
---|
| 74 | for ( ; maryIter >> m; ) { // empty list |
---|
[3d0560d] | 75 | sout | m.i | m.j | ' '; |
---|
[5e82d56] | 76 | } |
---|
| 77 | sout | "empty" | nl; |
---|
| 78 | |
---|
[3d0560d] | 79 | for ( i; 10 ) { |
---|
[636d3715] | 80 | push( mary, *new( 2 * i ) ); |
---|
[5e82d56] | 81 | } |
---|
| 82 | |
---|
[3d0560d] | 83 | for ( StackIter(Mary) iter = { mary }; iter >> m; ) { |
---|
| 84 | sout | m.i | m.j | ' '; |
---|
[5e82d56] | 85 | } |
---|
| 86 | sout | nl; |
---|
| 87 | |
---|
[3d0560d] | 88 | for ( i; 9 ) { |
---|
[636d3715] | 89 | delete( &pop( mary ) ); |
---|
[5e82d56] | 90 | } |
---|
| 91 | |
---|
| 92 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
[3d0560d] | 93 | sout | m.i | m.j | ' '; |
---|
[5e82d56] | 94 | } |
---|
| 95 | sout | nl; |
---|
| 96 | |
---|
[3d0560d] | 97 | for ( i; 10 ) { |
---|
[636d3715] | 98 | push( mary, *new( 2 * i + 1 ) ); |
---|
[5e82d56] | 99 | } |
---|
| 100 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
[3d0560d] | 101 | sout | m.i | m.j | ' '; |
---|
[5e82d56] | 102 | } |
---|
| 103 | sout | nl; |
---|
| 104 | |
---|
| 105 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
[3d0560d] | 106 | delete( &m ); |
---|
[5e82d56] | 107 | } |
---|
| 108 | } |
---|