Changeset 3e3f236 for libcfa/src/bits/sequence.hfa
- Timestamp:
- Dec 10, 2020, 4:00:29 PM (3 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 97aca3d, b3a0df6
- Parents:
- 6a45bd78 (diff), 297cf18 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/sequence.hfa
r6a45bd78 r3e3f236 1 1 #pragma once 2 2 3 #include " collection.hfa"3 #include "bits/collection.hfa" 4 4 5 5 struct Seqable { … … 9 9 10 10 inline { 11 // PUBLIC 12 11 13 void ?{}( Seqable & sq ) with( sq ) { 12 ((Colable &) 14 ((Colable &)sq){}; 13 15 back = 0p; 14 16 } // post: ! listed() … … 18 20 } 19 21 22 // PRIVATE 23 20 24 Seqable *& Back( Seqable * sq ) { 21 25 return sq->back; 22 26 } 27 28 // wrappers to make Collection have T 29 forall( dtype T ) { 30 T *& Back( T * n ) { 31 return (T *)Back( (Seqable *)n ); 32 } 33 } // distribution 23 34 } // distribution 24 35 … … 34 45 } // post: empty() & head() == 0 | !empty() & head() in *s 35 46 36 T *& Back( T * n ) {37 return (T *)Back( (Seqable *)n );38 }39 40 47 void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy 41 48 Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment 42 49 43 50 void ?{}( Sequence(T) & s ) with( s ) { 44 ((Collection &) 51 ((Collection &)s){}; 45 52 } // post: isEmpty(). 46 53 … … 50 57 } // post: empty() & tail() == 0 | !empty() & tail() in *s 51 58 52 // Return a pointer to the element after *n, or 0p if there isn't one.59 // Return a pointer to the element after *n, or 0p if list empty. 53 60 T * succ( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s 54 #ifdef __CFA_DEBUG__61 #ifdef __CFA_DEBUG__ 55 62 if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n ); 56 #endif // __CFA_DEBUG__63 #endif // __CFA_DEBUG__ 57 64 return Next( n ) == &head( s ) ? 0p : Next( n ); 58 65 } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s … … 60 67 // Return a pointer to the element before *n, or 0p if there isn't one. 61 68 T * pred( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s 62 #ifdef __CFA_DEBUG__69 #ifdef __CFA_DEBUG__ 63 70 if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n ); 64 #endif // __CFA_DEBUG__71 #endif // __CFA_DEBUG__ 65 72 return n == &head( s ) ? 0p : Back( n ); 66 73 } // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s … … 69 76 // Insert *n into the sequence before *bef, or at the end if bef == 0. 70 77 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s 71 #ifdef __CFA_DEBUG__78 #ifdef __CFA_DEBUG__ 72 79 if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef ); 73 #endif // __CFA_DEBUG__80 #endif // __CFA_DEBUG__ 74 81 if ( &bef == &head( s ) ) { // must change root 75 82 if ( root ) { … … 101 108 // Insert *n into the sequence after *aft, or at the beginning if aft == 0. 102 109 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) { // pre: !n->listed() & *aft in *s 103 #ifdef __CFA_DEBUG__110 #ifdef __CFA_DEBUG__ 104 111 if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n ); 105 #endif // __CFA_DEBUG__112 #endif // __CFA_DEBUG__ 106 113 if ( ! &aft ) { // must change root 107 114 if ( root ) { … … 130 137 // pre: n->listed() & *n in *s 131 138 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1) 132 #ifdef __CFA_DEBUG__139 #ifdef __CFA_DEBUG__ 133 140 if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n ); 134 #endif // __CFA_DEBUG__141 #endif // __CFA_DEBUG__ 135 142 if ( &n == &head( s ) ) { 136 143 if ( Next( &head( s ) ) == &head( s ) ) root = 0p; … … 188 195 // Node "n" must be in the "from" list. 189 196 void split( Sequence(T) & s, Sequence(T) & from, T & n ) with( s ) { 190 #ifdef __CFA_DEBUG__197 #ifdef __CFA_DEBUG__ 191 198 if ( ! listed( &n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, &n ); 192 #endif // __CFA_DEBUG__199 #endif // __CFA_DEBUG__ 193 200 Sequence(T) to; 194 201 to.root = from.root; // start of "to" list … … 199 206 Back( &head( from ) ) = Back( &head( to ) ); // fix "from" list 200 207 Next( Back( &head( to ) ) ) = &head( from ); 201 Next( &n ) = &head( to ); 208 Next( &n ) = &head( to ); // fix "to" list 202 209 Back( &head( to ) ) = &n; 203 210 } // if … … 214 221 // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence 215 222 // to pass to succ/pred. Both stack and queue just encounter 0p since the lists are not circular. 216 Sequence(T) * seq; 223 Sequence(T) * seq; // FIX ME: cannot be reference 217 224 }; 218 225 … … 224 231 225 232 void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) { 226 ((ColIter &) 233 ((ColIter &)si){}; 227 234 seq = &s; 228 235 curr = &head( s ); … … 230 237 231 238 void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) { 232 ((ColIter &) 239 ((ColIter &)si){}; 233 240 seq = &s; 234 241 curr = &start; … … 255 262 inline ColIter; 256 263 // See above for explanation. 257 Sequence(T) * seq; 264 Sequence(T) * seq; // FIX ME: cannot be reference 258 265 }; 259 266 260 267 inline { 261 268 void ?{}( SeqIterRev(T) & si ) with( si ) { 262 ((ColIter &) 269 ((ColIter &)si){}; 263 270 seq = 0p; 264 271 } // post: elts = null. 265 272 266 273 void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { 267 ((ColIter &) 274 ((ColIter &)si){}; 268 275 seq = &s; 269 276 curr = &tail( s ); … … 271 278 272 279 void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) { 273 ((ColIter &) 280 ((ColIter &)si){}; 274 281 seq = &s; 275 282 curr = &start; … … 291 298 } // distribution 292 299 } // distribution 293 294 // Local Variables: //295 // compile-command: "cfa sequence.hfa" //296 // End: //
Note: See TracChangeset
for help on using the changeset viewer.