[de2f969] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <stdlib.hfa> // new, delete |
---|
| 3 | #include <bits/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 | Fred *& Back( Fred * n ) { |
---|
| 17 | return (Fred *)Back( (Seqable *)n ); |
---|
| 18 | } |
---|
| 19 | Fred *& Next( Fred * n ) { |
---|
| 20 | return (Fred *)Next( (Colable *)n ); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | Sequence(Fred) fred; |
---|
[657c36f] | 24 | SeqIter(Fred) iter = { fred }; |
---|
[de2f969] | 25 | Fred & f; |
---|
| 26 | |
---|
| 27 | sout | nlOff; // turn off auto newline |
---|
| 28 | |
---|
[657c36f] | 29 | for ( ; iter | f; ) { // empty list |
---|
[de2f969] | 30 | sout | f.i | ' '; |
---|
| 31 | } |
---|
| 32 | sout | "empty" | nl; |
---|
| 33 | |
---|
| 34 | for ( i; 10 ) { |
---|
| 35 | add( fred, *new( 2 * i ) ); |
---|
| 36 | } |
---|
| 37 | |
---|
[62a7cc0] | 38 | sout | head( fred ).i | tail( fred ).i | nl; |
---|
| 39 | |
---|
[657c36f] | 40 | for ( SeqIter(Fred) iter = { fred }; iter | f; ) { |
---|
[de2f969] | 41 | sout | f.i | ' '; |
---|
| 42 | } |
---|
| 43 | sout | nl; |
---|
| 44 | |
---|
| 45 | for ( i; 9 ) { |
---|
| 46 | delete( &dropHead( fred ) ); |
---|
| 47 | } |
---|
| 48 | |
---|
[657c36f] | 49 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 50 | sout | f.i | ' '; |
---|
| 51 | } |
---|
| 52 | sout | nl; |
---|
| 53 | |
---|
| 54 | for ( i; 10 ) { |
---|
| 55 | addTail( fred, *new( 2 * i + 1 ) ); |
---|
| 56 | } |
---|
[62a7cc0] | 57 | |
---|
| 58 | Fred * head = new( -1 ), tail = { -2 }; |
---|
| 59 | addHead( fred, *head ); |
---|
| 60 | addTail( fred, tail ); |
---|
| 61 | |
---|
| 62 | sout | head( fred ).i | succ( fred, head )->i | tail( fred ).i | nl; |
---|
[657c36f] | 63 | |
---|
| 64 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 65 | sout | f.i | ' '; |
---|
| 66 | } |
---|
| 67 | sout | nl; |
---|
| 68 | |
---|
[62a7cc0] | 69 | remove( fred, *head ); |
---|
| 70 | remove( fred, tail ); |
---|
| 71 | delete( head ); |
---|
| 72 | delete( &dropTail( fred ) ); |
---|
| 73 | |
---|
[657c36f] | 74 | for ( over( iter, fred ); iter | f; ) { |
---|
[62a7cc0] | 75 | sout | f.i | ' '; |
---|
| 76 | } |
---|
| 77 | sout | nl; |
---|
| 78 | |
---|
| 79 | for ( i; 5 ) { |
---|
[de2f969] | 80 | delete( &dropTail( fred ) ); |
---|
| 81 | } |
---|
[657c36f] | 82 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 83 | sout | f.i | ' '; |
---|
| 84 | } |
---|
| 85 | sout | nl; |
---|
| 86 | |
---|
[657c36f] | 87 | for ( over( iter, fred ); iter | f; ) { |
---|
[62a7cc0] | 88 | delete( &remove( fred, f ) ); |
---|
[de2f969] | 89 | } |
---|
[657c36f] | 90 | for ( over( iter, fred ); iter | f; ) { |
---|
[62a7cc0] | 91 | sout | f.i | ' '; |
---|
| 92 | } |
---|
| 93 | sout | "empty" | nl; |
---|
[de2f969] | 94 | |
---|
[62a7cc0] | 95 | Fred & middle; |
---|
[de2f969] | 96 | for ( i; 10 ) { |
---|
[657c36f] | 97 | addHead( fred, *new( i ) ); // reverse order |
---|
[62a7cc0] | 98 | if ( i == 5 ) { |
---|
| 99 | &middle = &head( fred ); |
---|
[de2f969] | 100 | } |
---|
| 101 | } |
---|
[657c36f] | 102 | for ( SeqIterRev(Fred) riter = { fred }; riter | f; ) { |
---|
[de2f969] | 103 | sout | f.i | ' '; |
---|
| 104 | } |
---|
| 105 | sout | nl; |
---|
| 106 | |
---|
[62a7cc0] | 107 | head = new( -1 ); |
---|
| 108 | insertBef( fred, *head, head( fred ) ); |
---|
| 109 | insertAft( fred, *head, tail ); |
---|
| 110 | |
---|
| 111 | sout | head( fred ).i | succ( fred, head )->i | tail( fred ).i | nl; |
---|
[657c36f] | 112 | for ( over( iter, fred ); iter | f; ) { |
---|
[62a7cc0] | 113 | sout | f.i | ' '; |
---|
| 114 | } |
---|
| 115 | sout | nl; |
---|
[de2f969] | 116 | |
---|
[62a7cc0] | 117 | remove( fred, *head ); |
---|
| 118 | remove( fred, tail ); |
---|
| 119 | delete( head ); |
---|
| 120 | delete( &dropTail( fred ) ); |
---|
[de2f969] | 121 | |
---|
[657c36f] | 122 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 123 | sout | f.i | ' '; |
---|
| 124 | } |
---|
| 125 | sout | nl; |
---|
| 126 | |
---|
| 127 | Sequence(Fred) fred2; |
---|
| 128 | |
---|
[62a7cc0] | 129 | split( fred2, fred, middle ); |
---|
[de2f969] | 130 | |
---|
[657c36f] | 131 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 132 | sout | f.i | ' '; |
---|
| 133 | } |
---|
| 134 | sout | nl; |
---|
| 135 | |
---|
[657c36f] | 136 | for ( over( iter, fred2 ); iter | f; ) { |
---|
[de2f969] | 137 | sout | f.i | ' '; |
---|
| 138 | } |
---|
| 139 | sout | nl; |
---|
| 140 | |
---|
[62a7cc0] | 141 | transfer( fred, fred2 ); |
---|
[de2f969] | 142 | |
---|
[657c36f] | 143 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 144 | sout | f.i | ' '; |
---|
| 145 | } |
---|
| 146 | sout | nl; |
---|
| 147 | |
---|
[657c36f] | 148 | for ( over( iter, fred ); iter | f; ) { |
---|
[de2f969] | 149 | delete( &f ); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | // Mary test |
---|
| 153 | |
---|
| 154 | struct Mary { |
---|
| 155 | inline Fred; // Plan 9 inheritance |
---|
| 156 | int j; |
---|
| 157 | }; |
---|
| 158 | void ?{}( Mary & mary ) { abort(); } |
---|
| 159 | void ?{}( Mary & mary, int p ) with( mary ) { |
---|
| 160 | ((Fred &)mary){ p }; |
---|
| 161 | j = p; |
---|
| 162 | } |
---|
| 163 | Mary *& Back( Mary * n ) { |
---|
| 164 | return (Mary *)Back( (Fred *)n ); |
---|
| 165 | } |
---|
| 166 | Mary *& Next( Mary * n ) { |
---|
| 167 | return (Mary *)Next( (Fred *)n ); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | Sequence(Mary) mary; |
---|
[657c36f] | 171 | SeqIter(Mary) iter = { mary }; |
---|
[de2f969] | 172 | Mary & m; |
---|
| 173 | |
---|
[657c36f] | 174 | for ( ; iter | m; ) { // empty list |
---|
[de2f969] | 175 | sout | m.i | m.j | ' '; |
---|
| 176 | } |
---|
| 177 | sout | "empty" | nl; |
---|
| 178 | |
---|
| 179 | for ( i; 10 ) { |
---|
| 180 | add( mary, *new( 2 * i ) ); |
---|
| 181 | } |
---|
| 182 | |
---|
[62a7cc0] | 183 | sout | head( mary ).i | tail( mary ).i | nl; |
---|
| 184 | |
---|
[657c36f] | 185 | for ( SeqIter(Mary) iter = { mary }; iter | m; ) { |
---|
[de2f969] | 186 | sout | m.i | m.j | ' '; |
---|
| 187 | } |
---|
| 188 | sout | nl; |
---|
| 189 | |
---|
| 190 | for ( i; 9 ) { |
---|
| 191 | delete( &dropHead( mary ) ); |
---|
| 192 | } |
---|
| 193 | |
---|
[657c36f] | 194 | for ( over( iter, mary ); iter | m; ) { |
---|
[de2f969] | 195 | sout | m.i | m.j | ' '; |
---|
| 196 | } |
---|
| 197 | sout | nl; |
---|
| 198 | |
---|
| 199 | for ( i; 10 ) { |
---|
| 200 | addTail( mary, *new( 2 * i + 1 ) ); |
---|
| 201 | } |
---|
[62a7cc0] | 202 | |
---|
[657c36f] | 203 | Mary * head = new( -1 ), tail = { -2 }; |
---|
[62a7cc0] | 204 | addHead( mary, *head ); |
---|
| 205 | addTail( mary, tail ); |
---|
[657c36f] | 206 | |
---|
[62a7cc0] | 207 | sout | head( mary ).i | succ( mary, head )->i | tail( mary ).i | nl; |
---|
[657c36f] | 208 | |
---|
| 209 | for ( over( iter, mary ); iter | m; ) { |
---|
| 210 | sout | m.i | m.j | ' '; |
---|
| 211 | } |
---|
| 212 | sout | nl; |
---|
| 213 | |
---|
[62a7cc0] | 214 | remove( mary, *head ); |
---|
| 215 | remove( mary, tail ); |
---|
| 216 | delete( (Mary *)head ); |
---|
| 217 | delete( &dropTail( mary ) ); |
---|
| 218 | |
---|
[657c36f] | 219 | for ( over( iter, mary ); iter | m; ) { |
---|
[de2f969] | 220 | sout | m.i | m.j | ' '; |
---|
| 221 | } |
---|
| 222 | sout | nl; |
---|
| 223 | |
---|
[657c36f] | 224 | for ( i; 5 ) { |
---|
| 225 | delete( &dropTail( mary ) ); |
---|
| 226 | } |
---|
| 227 | for ( over( iter, mary ); iter | m; ) { |
---|
| 228 | sout | m.i | m.j | ' '; |
---|
| 229 | } |
---|
| 230 | sout | nl; |
---|
| 231 | |
---|
| 232 | for ( over( iter, mary ); iter | m; ) { |
---|
[62a7cc0] | 233 | delete( &remove( mary, m ) ); |
---|
| 234 | } |
---|
[657c36f] | 235 | for ( over( iter, mary ); iter | m; ) { |
---|
| 236 | sout | m.i | m.j | ' '; |
---|
| 237 | } |
---|
| 238 | sout | "empty" | nl; |
---|
[62a7cc0] | 239 | |
---|
| 240 | Mary & middle; |
---|
| 241 | for ( i; 10 ) { |
---|
| 242 | add( mary, *new( i ) ); |
---|
| 243 | if ( i == 4 ) { |
---|
| 244 | &middle = &tail( mary ); |
---|
| 245 | } |
---|
| 246 | } |
---|
[657c36f] | 247 | for ( SeqIter(Mary) iter = { mary }; iter | m; ) { |
---|
[de2f969] | 248 | sout | m.i | m.j | ' '; |
---|
| 249 | } |
---|
| 250 | sout | nl; |
---|
| 251 | |
---|
[62a7cc0] | 252 | Sequence(Mary) mary2; |
---|
[de2f969] | 253 | |
---|
[62a7cc0] | 254 | split( mary2, mary, middle ); |
---|
| 255 | |
---|
[657c36f] | 256 | for ( over( iter, mary ); iter | m; ) { |
---|
[de2f969] | 257 | sout | m.i | m.j | ' '; |
---|
| 258 | } |
---|
[62a7cc0] | 259 | sout | nl; |
---|
[657c36f] | 260 | for ( over( iter, mary2 ); iter | m; ) { |
---|
[62a7cc0] | 261 | sout | m.i | m.j | ' '; |
---|
| 262 | } |
---|
| 263 | sout | nl; |
---|
| 264 | |
---|
| 265 | transfer( mary, mary2 ); |
---|
[de2f969] | 266 | |
---|
[657c36f] | 267 | for ( over( iter, mary ); iter | m; ) { |
---|
[de2f969] | 268 | sout | m.i | m.j | ' '; |
---|
| 269 | } |
---|
| 270 | sout | nl; |
---|
[657c36f] | 271 | |
---|
| 272 | for ( over( iter, mary ); iter | m; ) { |
---|
[de2f969] | 273 | delete( &m ); |
---|
| 274 | } |
---|
| 275 | } |
---|