[de2f969] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <stdlib.hfa> // new, delete |
---|
| 3 | #include <bits/stack.hfa> |
---|
| 4 | |
---|
| 5 | int main() { |
---|
| 6 | // Fred test |
---|
| 7 | |
---|
[20be782] | 8 | struct Fred { |
---|
| 9 | inline Colable; // Plan 9 inheritance |
---|
| 10 | int i; |
---|
| 11 | }; |
---|
[de2f969] | 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 | |
---|
[20be782] | 20 | Stack(Fred) fred; |
---|
[657c36f] | 21 | StackIter(Fred) inter = { fred }; |
---|
[de2f969] | 22 | Fred & f; |
---|
| 23 | |
---|
| 24 | sout | nlOff; // turn off auto newline |
---|
| 25 | |
---|
[657c36f] | 26 | for ( ; inter | f; ) { // empty list |
---|
[de2f969] | 27 | sout | f.i | ' '; |
---|
| 28 | } |
---|
| 29 | sout | "empty" | nl; |
---|
| 30 | |
---|
| 31 | for ( i; 10 ) { |
---|
| 32 | push( fred, *new( 2 * i ) ); |
---|
| 33 | } |
---|
| 34 | |
---|
[657c36f] | 35 | for ( StackIter(Fred) iter = { fred }; iter | f; ) { |
---|
[de2f969] | 36 | sout | f.i | ' '; |
---|
| 37 | } |
---|
| 38 | sout | nl; |
---|
| 39 | |
---|
[62a7cc0] | 40 | sout | head( fred ).i | nl; |
---|
[de2f969] | 41 | |
---|
| 42 | for ( i; 9 ) { |
---|
| 43 | delete( &pop( fred ) ); |
---|
| 44 | } |
---|
| 45 | |
---|
[657c36f] | 46 | for ( over( inter, fred ); inter | f; ) { |
---|
[de2f969] | 47 | sout | f.i | ' '; |
---|
| 48 | } |
---|
| 49 | sout | nl; |
---|
| 50 | |
---|
| 51 | for ( i; 10 ) { |
---|
| 52 | push( fred, *new( 2 * i + 1 ) ); |
---|
| 53 | } |
---|
[657c36f] | 54 | for ( over( inter, fred ); inter | f; ) { |
---|
[de2f969] | 55 | sout | f.i | ' '; |
---|
| 56 | } |
---|
| 57 | sout | nl; |
---|
| 58 | |
---|
[657c36f] | 59 | for ( over( inter, fred ); inter | f; ) { |
---|
[de2f969] | 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 }; |
---|
[62a7cc0] | 72 | j = p; |
---|
[de2f969] | 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 | |
---|
[657c36f] | 83 | for ( ; maryIter | m; ) { // empty list |
---|
[de2f969] | 84 | sout | m.i | m.j | ' '; |
---|
| 85 | } |
---|
| 86 | sout | "empty" | nl; |
---|
| 87 | |
---|
| 88 | for ( i; 10 ) { |
---|
| 89 | push( mary, *new( 2 * i ) ); |
---|
| 90 | } |
---|
| 91 | |
---|
[657c36f] | 92 | for ( StackIter(Mary) iter = { mary }; iter | m; ) { |
---|
[de2f969] | 93 | sout | m.i | m.j | ' '; |
---|
| 94 | } |
---|
| 95 | sout | nl; |
---|
| 96 | |
---|
| 97 | for ( i; 9 ) { |
---|
| 98 | delete( &pop( mary ) ); |
---|
| 99 | } |
---|
| 100 | |
---|
[657c36f] | 101 | for ( over( maryIter, mary ); maryIter | m; ) { |
---|
[de2f969] | 102 | sout | m.i | m.j | ' '; |
---|
| 103 | } |
---|
| 104 | sout | nl; |
---|
| 105 | |
---|
| 106 | for ( i; 10 ) { |
---|
| 107 | push( mary, *new( 2 * i + 1 ) ); |
---|
| 108 | } |
---|
[657c36f] | 109 | for ( over( maryIter, mary ); maryIter | m; ) { |
---|
[de2f969] | 110 | sout | m.i | m.j | ' '; |
---|
| 111 | } |
---|
| 112 | sout | nl; |
---|
| 113 | |
---|
[657c36f] | 114 | for ( over( maryIter, mary ); maryIter | m; ) { |
---|
[de2f969] | 115 | delete( &m ); |
---|
| 116 | } |
---|
| 117 | } |
---|