[de2f969] | 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 | Queue(Fred) fred; |
---|
| 21 | QueueIter(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 | add( fred, *new( 2 * i ) ); |
---|
| 33 | } |
---|
| 34 | |
---|
[62a7cc0] | 35 | sout | head( fred ).i | tail( fred ).i | nl; |
---|
[de2f969] | 36 | |
---|
| 37 | for ( QueueIter(Fred) iter = { fred }; iter >> f; ) { |
---|
| 38 | sout | f.i | ' '; |
---|
| 39 | } |
---|
| 40 | sout | nl; |
---|
| 41 | |
---|
| 42 | for ( i; 9 ) { |
---|
| 43 | delete( &drop( fred ) ); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
| 47 | sout | f.i | ' '; |
---|
| 48 | } |
---|
| 49 | sout | nl; |
---|
| 50 | |
---|
| 51 | for ( i; 10 ) { |
---|
| 52 | add( fred, *new( 2 * i + 1 ) ); |
---|
| 53 | } |
---|
| 54 | |
---|
[62a7cc0] | 55 | Fred * head = new( -1 ), tail = { -2 }; |
---|
| 56 | addHead( fred, *head ); |
---|
| 57 | addTail( fred, tail ); |
---|
[de2f969] | 58 | |
---|
[62a7cc0] | 59 | sout | head( fred ).i | succ( fred, head )->i | tail( fred ).i | nl; |
---|
| 60 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
| 61 | sout | f.i | ' '; |
---|
| 62 | } |
---|
| 63 | sout | nl; |
---|
| 64 | |
---|
| 65 | remove( fred, *head ); |
---|
| 66 | remove( fred, tail ); |
---|
| 67 | delete( head ); |
---|
| 68 | delete( &dropTail( fred ) ); |
---|
[de2f969] | 69 | |
---|
| 70 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
| 71 | sout | f.i | ' '; |
---|
| 72 | } |
---|
| 73 | sout | nl; |
---|
| 74 | |
---|
[62a7cc0] | 75 | for ( i; 5 ) { |
---|
| 76 | delete( &dropTail( fred ) ); |
---|
| 77 | } |
---|
[de2f969] | 78 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[62a7cc0] | 79 | sout | f.i | ' '; |
---|
| 80 | } |
---|
| 81 | sout | nl; |
---|
| 82 | |
---|
| 83 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
| 84 | delete( &remove( fred, f ) ); |
---|
| 85 | } |
---|
| 86 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
| 87 | sout | f.i | ' '; |
---|
[de2f969] | 88 | } |
---|
[62a7cc0] | 89 | sout | "empty" | nl; |
---|
[de2f969] | 90 | |
---|
[62a7cc0] | 91 | Fred & middle; |
---|
[de2f969] | 92 | for ( i; 10 ) { |
---|
[62a7cc0] | 93 | add( fred, *new( i ) ); |
---|
| 94 | if ( i == 4 ) { |
---|
| 95 | &middle = &tail( fred ); |
---|
[de2f969] | 96 | } |
---|
| 97 | } |
---|
[62a7cc0] | 98 | for ( QueueIter(Fred) iter = { fred }; iter >> f; ) { |
---|
[de2f969] | 99 | sout | f.i | ' '; |
---|
| 100 | } |
---|
| 101 | sout | nl; |
---|
| 102 | |
---|
| 103 | Queue(Fred) fred2; |
---|
| 104 | |
---|
[62a7cc0] | 105 | split( fred2, fred, middle ); |
---|
[de2f969] | 106 | |
---|
[62a7cc0] | 107 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[de2f969] | 108 | sout | f.i | ' '; |
---|
| 109 | } |
---|
| 110 | sout | nl; |
---|
| 111 | |
---|
| 112 | for ( over( fredIter, fred2 ); fredIter >> f; ) { |
---|
| 113 | sout | f.i | ' '; |
---|
| 114 | } |
---|
| 115 | sout | nl; |
---|
| 116 | |
---|
[62a7cc0] | 117 | transfer( fred, fred2 ); |
---|
[de2f969] | 118 | |
---|
[62a7cc0] | 119 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[de2f969] | 120 | sout | f.i | ' '; |
---|
| 121 | } |
---|
| 122 | sout | nl; |
---|
| 123 | |
---|
[62a7cc0] | 124 | for ( over( fredIter, fred ); fredIter >> f; ) { |
---|
[de2f969] | 125 | delete( &f ); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | // Mary test |
---|
| 129 | |
---|
| 130 | struct Mary { |
---|
| 131 | inline Fred; // Plan 9 inheritance |
---|
| 132 | int j; |
---|
| 133 | }; |
---|
| 134 | void ?{}( Mary & mary ) { abort(); } |
---|
| 135 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
| 136 | ((Fred &)mary){ p }; |
---|
[62a7cc0] | 137 | j = p; |
---|
[de2f969] | 138 | } |
---|
| 139 | Mary *& Next( Mary * n ) { |
---|
| 140 | return (Mary *)Next( (Fred *)n ); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | Queue(Mary) mary; |
---|
| 144 | QueueIter(Mary) maryIter = { mary }; |
---|
| 145 | Mary & m; |
---|
| 146 | |
---|
| 147 | for ( ; maryIter >> m; ) { // empty list |
---|
| 148 | sout | m.i | m.j | ' '; |
---|
| 149 | } |
---|
| 150 | sout | "empty" | nl; |
---|
| 151 | |
---|
| 152 | for ( i; 10 ) { |
---|
| 153 | add( mary, *new( 2 * i ) ); |
---|
| 154 | } |
---|
| 155 | |
---|
[62a7cc0] | 156 | sout | head( mary ).i | tail( mary ).i | nl; |
---|
| 157 | |
---|
[de2f969] | 158 | for ( QueueIter(Mary) iter = { mary }; iter >> m; ) { |
---|
| 159 | sout | m.i | m.j | ' '; |
---|
| 160 | } |
---|
| 161 | sout | nl; |
---|
| 162 | |
---|
| 163 | for ( i; 9 ) { |
---|
| 164 | delete( &drop( mary ) ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
| 168 | sout | m.i | m.j | ' '; |
---|
| 169 | } |
---|
| 170 | sout | nl; |
---|
| 171 | |
---|
| 172 | for ( i; 10 ) { |
---|
| 173 | add( mary, *new( 2 * i + 1 ) ); |
---|
| 174 | } |
---|
[62a7cc0] | 175 | |
---|
| 176 | Mary * head = new( -1 ), tail = { -1 }; |
---|
| 177 | addHead( mary, *head ); |
---|
| 178 | addTail( mary, tail ); |
---|
| 179 | sout | head( mary ).i | succ( mary, head )->i | tail( mary ).i | nl; |
---|
| 180 | remove( mary, *head ); |
---|
| 181 | remove( mary, tail ); |
---|
| 182 | delete( (Mary *)head ); |
---|
| 183 | delete( &dropTail( mary ) ); |
---|
| 184 | |
---|
[de2f969] | 185 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
| 186 | sout | m.i | m.j | ' '; |
---|
| 187 | } |
---|
| 188 | sout | nl; |
---|
| 189 | |
---|
[62a7cc0] | 190 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
| 191 | delete( &remove( mary, m ) ); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | Mary & middle; |
---|
| 195 | for ( i; 10 ) { |
---|
| 196 | add( mary, *new( i ) ); |
---|
| 197 | if ( i == 4 ) { |
---|
| 198 | &middle = &tail( mary ); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | for ( QueueIter(Mary) iter = { mary }; iter >> m; ) { |
---|
| 202 | sout | m.i | m.j | ' '; |
---|
| 203 | } |
---|
| 204 | sout | nl; |
---|
| 205 | |
---|
| 206 | Queue(Mary) mary2; |
---|
| 207 | |
---|
| 208 | split( mary2, mary, middle ); |
---|
| 209 | |
---|
| 210 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
| 211 | sout | m.i | m.j | ' '; |
---|
| 212 | } |
---|
| 213 | sout | nl; |
---|
| 214 | for ( over( maryIter, mary2 ); maryIter >> m; ) { |
---|
| 215 | sout | m.i | m.j | ' '; |
---|
| 216 | } |
---|
| 217 | sout | nl; |
---|
| 218 | |
---|
| 219 | transfer( mary, mary2 ); |
---|
| 220 | |
---|
| 221 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
| 222 | sout | m.i | m.j | ' '; |
---|
| 223 | } |
---|
| 224 | sout | nl; |
---|
[de2f969] | 225 | for ( over( maryIter, mary ); maryIter >> m; ) { |
---|
| 226 | delete( &m ); |
---|
| 227 | } |
---|
| 228 | } |
---|