[5e82d56] | 1 | #pragma once |
---|
| 2 | |
---|
| 3 | #include "collection.hfa" |
---|
| 4 | |
---|
| 5 | struct Seqable { |
---|
| 6 | inline Colable; |
---|
| 7 | Seqable * back; // pointer to previous node in the list |
---|
| 8 | }; |
---|
| 9 | |
---|
| 10 | inline { |
---|
| 11 | void ?{}( Seqable & sq ) with( sq ) { |
---|
[636d3715] | 12 | ((Colable &) sq){}; |
---|
[5e82d56] | 13 | back = 0p; |
---|
| 14 | } // post: ! listed() |
---|
| 15 | |
---|
| 16 | Seqable * getBack( Seqable & sq ) with( sq ) { |
---|
| 17 | return back; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | Seqable *& Back( Seqable * sq ) { |
---|
| 21 | return sq->back; |
---|
| 22 | } |
---|
| 23 | } // distribution |
---|
| 24 | |
---|
| 25 | forall( dtype T ) { |
---|
| 26 | struct Sequence { |
---|
| 27 | inline Collection; // Plan 9 inheritance |
---|
| 28 | }; |
---|
| 29 | |
---|
| 30 | inline { |
---|
| 31 | // wrappers to make Collection have T |
---|
| 32 | T * head( Sequence(T) & s ) with( s ) { |
---|
| 33 | return (T *)head( (Collection &)s ); |
---|
| 34 | } // post: empty() & head() == 0 | !empty() & head() in *s |
---|
| 35 | |
---|
| 36 | T *& Back( T * n ) { |
---|
| 37 | return (T *)Back( (Seqable *)n ); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy |
---|
| 41 | Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment |
---|
| 42 | |
---|
| 43 | void ?{}( Sequence(T) & s ) with( s ) { |
---|
| 44 | ((Collection &) s){}; |
---|
| 45 | } // post: isEmpty(). |
---|
| 46 | |
---|
| 47 | // Return a pointer to the last sequence element, without removing it. |
---|
[b37515b] | 48 | T & tail( Sequence(T) & s ) with( s ) { |
---|
[636d3715] | 49 | return root ? (T &)Back( head( s ) ) : *0p; // needs cast? |
---|
[5e82d56] | 50 | } // post: empty() & tail() == 0 | !empty() & tail() in *s |
---|
| 51 | |
---|
| 52 | // Return a pointer to the element after *n, or 0p if there isn't one. |
---|
| 53 | T * succ( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s |
---|
| 54 | #ifdef __CFA_DEBUG__ |
---|
| 55 | if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n ); |
---|
| 56 | #endif // __CFA_DEBUG__ |
---|
[636d3715] | 57 | return Next( n ) == head( s ) ? 0p : Next( n ); |
---|
[5e82d56] | 58 | } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s |
---|
| 59 | |
---|
| 60 | // Return a pointer to the element before *n, or 0p if there isn't one. |
---|
| 61 | T * pred( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s |
---|
| 62 | #ifdef __CFA_DEBUG__ |
---|
| 63 | if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n ); |
---|
| 64 | #endif // __CFA_DEBUG__ |
---|
[636d3715] | 65 | return n == head( s ) ? 0p : Back( n ); |
---|
[5e82d56] | 66 | } // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | // Insert *n into the sequence before *bef, or at the end if bef == 0. |
---|
[b37515b] | 70 | void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s |
---|
[5e82d56] | 71 | #ifdef __CFA_DEBUG__ |
---|
[b37515b] | 72 | if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef ); |
---|
[5e82d56] | 73 | #endif // __CFA_DEBUG__ |
---|
[636d3715] | 74 | if ( &bef == head( s ) ) { // must change root |
---|
[5e82d56] | 75 | if ( root ) { |
---|
[636d3715] | 76 | Next( &n ) = head( s ); |
---|
| 77 | Back( &n ) = Back( head( s ) ); |
---|
[5e82d56] | 78 | // inserted node must be consistent before it is seen |
---|
| 79 | asm( "" : : : "memory" ); // prevent code movement across barrier |
---|
[636d3715] | 80 | Back( head( s ) ) = &n; |
---|
[b37515b] | 81 | Next( Back( &n ) ) = &n; |
---|
[5e82d56] | 82 | } else { |
---|
[b37515b] | 83 | Next( &n ) = &n; |
---|
| 84 | Back( &n ) = &n; |
---|
[5e82d56] | 85 | } // if |
---|
| 86 | // inserted node must be consistent before it is seen |
---|
| 87 | asm( "" : : : "memory" ); // prevent code movement across barrier |
---|
[b37515b] | 88 | root = &n; |
---|
[5e82d56] | 89 | } else { |
---|
[636d3715] | 90 | if ( ! &bef ) &bef = head( s ); |
---|
[b37515b] | 91 | Next( &n ) = &bef; |
---|
| 92 | Back( &n ) = Back( &bef ); |
---|
[5e82d56] | 93 | // inserted node must be consistent before it is seen |
---|
| 94 | asm( "" : : : "memory" ); // prevent code movement across barrier |
---|
[b37515b] | 95 | Back( &bef ) = &n; |
---|
| 96 | Next( Back( &n ) ) = &n; |
---|
[5e82d56] | 97 | } // if |
---|
| 98 | } // post: n->listed() & *n in *s & succ(n) == bef |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | // Insert *n into the sequence after *aft, or at the beginning if aft == 0. |
---|
[b37515b] | 102 | void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) { // pre: !n->listed() & *aft in *s |
---|
[5e82d56] | 103 | #ifdef __CFA_DEBUG__ |
---|
[b37515b] | 104 | if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n ); |
---|
[5e82d56] | 105 | #endif // __CFA_DEBUG__ |
---|
[b37515b] | 106 | if ( ! &aft ) { // must change root |
---|
[5e82d56] | 107 | if ( root ) { |
---|
[636d3715] | 108 | Next( &n ) = head( s ); |
---|
| 109 | Back( &n ) = Back( head( s ) ); |
---|
[5e82d56] | 110 | // inserted node must be consistent before it is seen |
---|
| 111 | asm( "" : : : "memory" ); // prevent code movement across barrier |
---|
[636d3715] | 112 | Back( head( s ) ) = &n; |
---|
[b37515b] | 113 | Next( Back( &n ) ) = &n; |
---|
[5e82d56] | 114 | } else { |
---|
[b37515b] | 115 | Next( &n ) = &n; |
---|
| 116 | Back( &n ) = &n; |
---|
[5e82d56] | 117 | } // if |
---|
| 118 | asm( "" : : : "memory" ); // prevent code movement across barrier |
---|
[b37515b] | 119 | root = &n; |
---|
[5e82d56] | 120 | } else { |
---|
[b37515b] | 121 | Next( &n ) = Next( &aft ); |
---|
| 122 | Back( &n ) = &aft; |
---|
[5e82d56] | 123 | // inserted node must be consistent before it is seen |
---|
| 124 | asm( "" : : : "memory" ); // prevent code movement across barrier |
---|
[b37515b] | 125 | Back( Next( &n ) ) = &n; |
---|
| 126 | Next( &aft ) = &n; |
---|
[5e82d56] | 127 | } // if |
---|
| 128 | } // post: n->listed() & *n in *s & succ(n) == bef |
---|
| 129 | |
---|
| 130 | // pre: n->listed() & *n in *s |
---|
[b37515b] | 131 | void remove( Sequence(T) & s, T & n ) with( s ) { // O(1) |
---|
[5e82d56] | 132 | #ifdef __CFA_DEBUG__ |
---|
[b37515b] | 133 | if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n ); |
---|
[5e82d56] | 134 | #endif // __CFA_DEBUG__ |
---|
[636d3715] | 135 | if ( &n == head( s ) ) { |
---|
| 136 | if ( Next( head( s ) ) == head( s ) ) root = 0p; |
---|
| 137 | else root = Next( head(s ) ); |
---|
[5e82d56] | 138 | } // if |
---|
[b37515b] | 139 | Back( Next( &n ) ) = Back( &n ); |
---|
| 140 | Next( Back( &n ) ) = Next( &n ); |
---|
| 141 | Next( &n ) = Back( &n ) = 0p; |
---|
[5e82d56] | 142 | } // post: !n->listed(). |
---|
| 143 | |
---|
| 144 | // Add an element to the head of the sequence. |
---|
[b37515b] | 145 | void addHead( Sequence(T) & s, T & n ) { // pre: !n->listed(); post: n->listed() & head() == n |
---|
| 146 | insertAft( s, *0p, n ); |
---|
[5e82d56] | 147 | } |
---|
| 148 | // Add an element to the tail of the sequence. |
---|
[b37515b] | 149 | void addTail( Sequence(T) & s, T & n ) { // pre: !n->listed(); post: n->listed() & head() == n |
---|
| 150 | insertBef( s, n, *0p ); |
---|
[5e82d56] | 151 | } |
---|
| 152 | // Add an element to the tail of the sequence. |
---|
[b37515b] | 153 | void add( Sequence(T) & s, T & n ) { // pre: !n->listed(); post: n->listed() & head() == n |
---|
[5e82d56] | 154 | addTail( s, n ); |
---|
| 155 | } |
---|
| 156 | // Remove and return the head element in the sequence. |
---|
[b37515b] | 157 | T & dropHead( Sequence(T) & s ) { |
---|
[5e82d56] | 158 | T * n = head( s ); |
---|
[b37515b] | 159 | return n ? remove( s, *n ), *n : *0p; |
---|
[5e82d56] | 160 | } |
---|
| 161 | // Remove and return the head element in the sequence. |
---|
[b37515b] | 162 | T & drop( Sequence(T) & s ) { |
---|
[5e82d56] | 163 | return dropHead( s ); |
---|
| 164 | } |
---|
| 165 | // Remove and return the tail element in the sequence. |
---|
[b37515b] | 166 | T & dropTail( Sequence(T) & s ) { |
---|
| 167 | T & n = tail( s ); |
---|
| 168 | return &n ? remove( s, n ), n : *0p; |
---|
[5e82d56] | 169 | } |
---|
| 170 | |
---|
| 171 | // Transfer the "from" list to the end of s sequence; the "from" list is empty after the transfer. |
---|
| 172 | void transfer( Sequence(T) & s, Sequence(T) & from ) with( s ) { |
---|
| 173 | if ( empty( from ) ) return; // "from" list empty ? |
---|
| 174 | if ( empty( s ) ) { // "to" list empty ? |
---|
| 175 | root = from.root; |
---|
| 176 | } else { // "to" list not empty |
---|
[636d3715] | 177 | T * toEnd = Back( head( s ) ); |
---|
| 178 | T * fromEnd = Back( head( from ) ); |
---|
[5e82d56] | 179 | Back( root ) = fromEnd; |
---|
[636d3715] | 180 | Next( fromEnd ) = head( s ); |
---|
[5e82d56] | 181 | Back( from.root ) = toEnd; |
---|
[636d3715] | 182 | Next( toEnd ) = head( from ); |
---|
[5e82d56] | 183 | } // if |
---|
| 184 | from.root = 0p; // mark "from" list empty |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | // Transfer the "from" list up to node "n" to the end of s list; the "from" list becomes the sequence after node "n". |
---|
| 188 | // Node "n" must be in the "from" list. |
---|
| 189 | void split( Sequence(T) & s, Sequence(T) & from, T * n ) with( s ) { |
---|
| 190 | #ifdef __CFA_DEBUG__ |
---|
| 191 | if ( ! listed( n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, n ); |
---|
| 192 | #endif // __CFA_DEBUG__ |
---|
| 193 | Sequence(T) to; |
---|
| 194 | to.root = from.root; // start of "to" list |
---|
| 195 | from.root = Next( n ); // start of "from" list |
---|
| 196 | if ( to.root == from.root ) { // last node in list ? |
---|
| 197 | from.root = 0p; // mark "from" list empty |
---|
| 198 | } else { |
---|
[636d3715] | 199 | Back( head( from ) ) = Back( head( to ) ); // fix "from" list |
---|
| 200 | Next( Back( head( to ) ) ) = head( from ); |
---|
| 201 | Next( n ) = head( to ); // fix "to" list |
---|
| 202 | Back( head( to ) ) = n; |
---|
[5e82d56] | 203 | } // if |
---|
| 204 | transfer( s, to ); |
---|
| 205 | } |
---|
| 206 | } // distribution |
---|
| 207 | } // distribution |
---|
| 208 | |
---|
| 209 | forall( dtype T ) { |
---|
| 210 | // SeqIter(T) is used to iterate over a Sequence(T) in head-to-tail order. |
---|
| 211 | struct SeqIter { |
---|
| 212 | inline ColIter; |
---|
| 213 | Sequence(T) * seq; |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | inline { |
---|
[636d3715] | 217 | void ?{}( SeqIter(T) & si ) with( si ) { |
---|
| 218 | ((ColIter &)si){}; |
---|
[5e82d56] | 219 | seq = 0p; |
---|
| 220 | } // post: elts = null. |
---|
| 221 | |
---|
| 222 | void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) { |
---|
| 223 | ((ColIter &) si){}; |
---|
| 224 | seq = &s; |
---|
[3d0560d] | 225 | curr = head( s ); |
---|
[5e82d56] | 226 | } // post: elts = null. |
---|
| 227 | |
---|
| 228 | void over( SeqIter(T) & si, Sequence(T) & s ) with( si ) { |
---|
| 229 | seq = &s; |
---|
| 230 | curr = head( s ); |
---|
| 231 | } // post: elts = {e in s}. |
---|
| 232 | |
---|
[3d0560d] | 233 | bool ?>>?( SeqIter(T) & si, T && tp ) with( si ) { |
---|
[5e82d56] | 234 | if ( curr ) { |
---|
[3d0560d] | 235 | &tp = Curr( si ); |
---|
| 236 | T * n = succ( *seq, Curr( si ) ); |
---|
[5e82d56] | 237 | curr = n == head( *seq ) ? 0p : n; |
---|
[3d0560d] | 238 | } else &tp = 0p; |
---|
| 239 | return &tp != 0p; |
---|
[5e82d56] | 240 | } |
---|
| 241 | } // distribution |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | // A SeqIterRev(T) is used to iterate over a Sequence(T) in tail-to-head order. |
---|
| 245 | struct SeqIterRev { |
---|
| 246 | inline ColIter; |
---|
| 247 | Sequence(T) * seq; |
---|
| 248 | }; |
---|
| 249 | |
---|
| 250 | inline { |
---|
| 251 | void ?{}( SeqIterRev(T) & si ) with( si ) { |
---|
| 252 | ((ColIter &) si){}; |
---|
| 253 | seq = 0p; |
---|
| 254 | } // post: elts = null. |
---|
| 255 | |
---|
| 256 | void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { |
---|
| 257 | ((ColIter &) si){}; |
---|
| 258 | seq = &s; |
---|
[b37515b] | 259 | curr = &tail( s ); |
---|
[5e82d56] | 260 | } // post: elts = null. |
---|
| 261 | |
---|
| 262 | void over( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { |
---|
| 263 | seq = &s; |
---|
[b37515b] | 264 | curr = &tail( s ); |
---|
[5e82d56] | 265 | } // post: elts = {e in s}. |
---|
| 266 | |
---|
[3d0560d] | 267 | bool ?>>?( SeqIterRev(T) & si, T && tp ) with( si ) { |
---|
[5e82d56] | 268 | if ( curr ) { |
---|
[3d0560d] | 269 | &tp = Curr( si ); |
---|
| 270 | T * n = pred( *seq, Curr( si ) ); |
---|
[b37515b] | 271 | curr = n == &tail( *seq ) ? 0p : n; |
---|
[3d0560d] | 272 | } else &tp = 0p; |
---|
| 273 | return &tp != 0p; |
---|
[5e82d56] | 274 | } |
---|
| 275 | } // distribution |
---|
| 276 | } // distribution |
---|
| 277 | |
---|
| 278 | // Local Variables: // |
---|
| 279 | // compile-command: "make install" // |
---|
| 280 | // End: // |
---|