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