Changes in libcfa/src/bits/sequence.hfa [7d4ce2a:a32cbac2]
- File:
-
- 1 edited
-
libcfa/src/bits/sequence.hfa (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/sequence.hfa
r7d4ce2a ra32cbac2 1 1 #pragma once 2 2 3 #include " bits/collection.hfa"3 #include "collection.hfa" 4 4 5 5 struct Seqable { … … 9 9 10 10 inline { 11 // PUBLIC12 13 11 void ?{}( Seqable & sq ) with( sq ) { 14 ((Colable &) sq){};12 ((Colable &) sq){}; 15 13 back = 0p; 16 14 } // post: ! listed() … … 20 18 } 21 19 22 // PRIVATE23 24 20 Seqable *& Back( Seqable * sq ) { 25 21 return sq->back; 26 22 } 27 28 // wrappers to make Collection have T29 forall( dtype T ) {30 T *& Back( T * n ) {31 return (T *)Back( (Seqable *)n );32 }33 } // distribution34 23 } // distribution 35 24 … … 45 34 } // post: empty() & head() == 0 | !empty() & head() in *s 46 35 36 T *& Back( T * n ) { 37 return (T *)Back( (Seqable *)n ); 38 } 39 47 40 void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy 48 41 Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment 49 42 50 43 void ?{}( Sequence(T) & s ) with( s ) { 51 ((Collection &) s){};44 ((Collection &) s){}; 52 45 } // post: isEmpty(). 53 46 … … 57 50 } // post: empty() & tail() == 0 | !empty() & tail() in *s 58 51 59 // Return a pointer to the element after *n, or 0p if list empty.52 // Return a pointer to the element after *n, or 0p if there isn't one. 60 53 T * succ( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s 61 #ifdef __CFA_DEBUG__54 #ifdef __CFA_DEBUG__ 62 55 if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n ); 63 #endif // __CFA_DEBUG__56 #endif // __CFA_DEBUG__ 64 57 return Next( n ) == &head( s ) ? 0p : Next( n ); 65 58 } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s … … 67 60 // Return a pointer to the element before *n, or 0p if there isn't one. 68 61 T * pred( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s 69 #ifdef __CFA_DEBUG__62 #ifdef __CFA_DEBUG__ 70 63 if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n ); 71 #endif // __CFA_DEBUG__64 #endif // __CFA_DEBUG__ 72 65 return n == &head( s ) ? 0p : Back( n ); 73 66 } // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s … … 76 69 // Insert *n into the sequence before *bef, or at the end if bef == 0. 77 70 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s 78 #ifdef __CFA_DEBUG__71 #ifdef __CFA_DEBUG__ 79 72 if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef ); 80 #endif // __CFA_DEBUG__73 #endif // __CFA_DEBUG__ 81 74 if ( &bef == &head( s ) ) { // must change root 82 75 if ( root ) { … … 108 101 // Insert *n into the sequence after *aft, or at the beginning if aft == 0. 109 102 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) { // pre: !n->listed() & *aft in *s 110 #ifdef __CFA_DEBUG__103 #ifdef __CFA_DEBUG__ 111 104 if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n ); 112 #endif // __CFA_DEBUG__105 #endif // __CFA_DEBUG__ 113 106 if ( ! &aft ) { // must change root 114 107 if ( root ) { … … 137 130 // pre: n->listed() & *n in *s 138 131 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1) 139 #ifdef __CFA_DEBUG__132 #ifdef __CFA_DEBUG__ 140 133 if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n ); 141 #endif // __CFA_DEBUG__134 #endif // __CFA_DEBUG__ 142 135 if ( &n == &head( s ) ) { 143 136 if ( Next( &head( s ) ) == &head( s ) ) root = 0p; … … 195 188 // Node "n" must be in the "from" list. 196 189 void split( Sequence(T) & s, Sequence(T) & from, T & n ) with( s ) { 197 #ifdef __CFA_DEBUG__190 #ifdef __CFA_DEBUG__ 198 191 if ( ! listed( &n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, &n ); 199 #endif // __CFA_DEBUG__192 #endif // __CFA_DEBUG__ 200 193 Sequence(T) to; 201 194 to.root = from.root; // start of "to" list … … 206 199 Back( &head( from ) ) = Back( &head( to ) ); // fix "from" list 207 200 Next( Back( &head( to ) ) ) = &head( from ); 208 Next( &n ) = &head( to ); // fix "to" list201 Next( &n ) = &head( to ); // fix "to" list 209 202 Back( &head( to ) ) = &n; 210 203 } // if … … 221 214 // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence 222 215 // to pass to succ/pred. Both stack and queue just encounter 0p since the lists are not circular. 223 Sequence(T) * seq; // FIX ME: cannot be reference216 Sequence(T) * seq; 224 217 }; 225 218 … … 231 224 232 225 void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) { 233 ((ColIter &) si){};226 ((ColIter &) si){}; 234 227 seq = &s; 235 228 curr = &head( s ); … … 237 230 238 231 void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) { 239 ((ColIter &) si){};232 ((ColIter &) si){}; 240 233 seq = &s; 241 234 curr = &start; … … 262 255 inline ColIter; 263 256 // See above for explanation. 264 Sequence(T) * seq; // FIX ME: cannot be reference257 Sequence(T) * seq; 265 258 }; 266 259 267 260 inline { 268 261 void ?{}( SeqIterRev(T) & si ) with( si ) { 269 ((ColIter &) si){};262 ((ColIter &) si){}; 270 263 seq = 0p; 271 264 } // post: elts = null. 272 265 273 266 void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { 274 ((ColIter &) si){};267 ((ColIter &) si){}; 275 268 seq = &s; 276 269 curr = &tail( s ); … … 278 271 279 272 void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) { 280 ((ColIter &) si){};273 ((ColIter &) si){}; 281 274 seq = &s; 282 275 curr = &start; … … 298 291 } // distribution 299 292 } // distribution 293 294 // Local Variables: // 295 // compile-command: "cfa sequence.hfa" // 296 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.