Changeset e43aa14
- Timestamp:
- Dec 16, 2020, 3:40:53 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- c131a02
- Parents:
- 8b73526
- Location:
- libcfa/src/bits
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/collection.hfa
r8b73526 re43aa14 1 1 #pragma once 2 #include <stdio.h> // REMOVE THIS AFTER DEBUGGING 3 2 4 3 5 struct Colable { 4 Colable * next; // next node in the list6 struct Colable * next; // next node in the list 5 7 // invariant: (next != 0) <=> listed() 6 8 }; 7 8 inline {9 #ifdef __cforall 10 static inline { 9 11 // PUBLIC 10 12 … … 28 30 } 29 31 30 // wrappers to make Collection have T31 forall( dtype T ) {32 T *& Next( T * n ) {33 return (T *)Next( (Colable *)n );34 }32 // // wrappers to make Collection have T 33 // forall( dtype T ) { 34 // T *& Next( T * n ) { 35 // return (T *)Next( (Colable *)n ); 36 // } 35 37 36 bool listed( T * n ) { 37 return Next( (Colable *)n ) != 0p; 38 } 39 } // distribution 38 // bool listed( T * n ) { 39 // fprintf(stderr, "inappropriate listed used"); 40 // return Next( (Colable *)n ) != 0p; 41 // } 42 // } // distribution 40 43 } // distribution 41 44 … … 45 48 }; 46 49 47 inline {50 static inline { 48 51 // class invariant: root == 0 & empty() | *root in *this 49 52 void ?{}( Collection &, const Collection & ) = void; // no copy … … 68 71 }; 69 72 70 inline {73 static inline { 71 74 void ?{}( ColIter & colIter ) with( colIter ) { 72 75 curr = 0p; … … 79 82 } // distribution 80 83 } // distribution 84 #endif -
libcfa/src/bits/sequence.hfa
r8b73526 re43aa14 2 2 3 3 #include "bits/collection.hfa" 4 #include "bits/defs.hfa" 4 5 5 6 struct Seqable { 6 inline Colable;7 Seqable * back; // pointer to previous node in the list7 __cfa_anonymous_object(Colable); 8 struct Seqable * back; // pointer to previous node in the list 8 9 }; 9 10 10 inline { 11 #ifdef __cforall 12 static inline { 11 13 // PUBLIC 12 14 … … 26 28 } 27 29 28 // wrappers to make Collection have T29 forall( dtype T ) {30 T *& Back( T * n ) {31 return (T *)Back( (Seqable *)n );32 }33 } // distribution30 // // wrappers to make Collection have T 31 // forall( dtype T ) { 32 // T *& Back( T * n ) { 33 // return (T *)Back( (Seqable *)n ); 34 // } 35 // } // distribution 34 36 } // distribution 35 37 36 forall( dtype T ) {38 forall( dtype T | { T *& Back ( T * ); T *& Next ( T * ); bool listed ( T * ); } ) { 37 39 struct Sequence { 38 40 inline Collection; // Plan 9 inheritance 39 41 }; 40 42 41 inline {43 static inline { 42 44 // wrappers to make Collection have T 43 45 T & head( Sequence(T) & s ) with( s ) { … … 184 186 T * toEnd = Back( &head( s ) ); 185 187 T * fromEnd = Back( &head( from ) ); 186 Back( root ) = fromEnd;188 Back( (T *)root ) = fromEnd; 187 189 Next( fromEnd ) = &head( s ); 188 Back( from.root ) = toEnd;190 Back( (T *)from.root ) = toEnd; 189 191 Next( toEnd ) = &head( from ); 190 192 } // if … … 214 216 } // distribution 215 217 216 forall( dtype T ) { 217 // SeqIter(T) is used to iterate over a Sequence(T) in head-to-tail order. 218 struct SeqIter { 219 inline ColIter; 220 // The Sequence must be passed to pred and succ to check for the end of the Sequence and return 0p. Without 221 // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence 222 // 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 reference 224 }; 225 226 inline { 227 void ?{}( SeqIter(T) & si ) with( si ) { 228 ((ColIter &)si){}; 229 seq = 0p; 230 } // post: elts = null. 231 232 void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) { 233 ((ColIter &)si){}; 234 seq = &s; 235 curr = &head( s ); 236 } // post: elts = null. 237 238 void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) { 239 ((ColIter &)si){}; 240 seq = &s; 241 curr = &start; 242 } // post: elts = null. 243 244 void over( SeqIter(T) & si, Sequence(T) & s ) with( si ) { 245 seq = &s; 246 curr = &head( s ); 247 } // post: elts = {e in s}. 248 249 bool ?>>?( SeqIter(T) & si, T && tp ) with( si ) { 250 if ( curr ) { 251 &tp = Curr( si ); 252 T * n = succ( *seq, Curr( si ) ); 253 curr = n == &head( *seq ) ? 0p : n; 254 } else &tp = 0p; 255 return &tp != 0p; 256 } 257 } // distribution 258 259 260 // A SeqIterRev(T) is used to iterate over a Sequence(T) in tail-to-head order. 261 struct SeqIterRev { 262 inline ColIter; 263 // See above for explanation. 264 Sequence(T) * seq; // FIX ME: cannot be reference 265 }; 266 267 inline { 268 void ?{}( SeqIterRev(T) & si ) with( si ) { 269 ((ColIter &)si){}; 270 seq = 0p; 271 } // post: elts = null. 272 273 void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { 274 ((ColIter &)si){}; 275 seq = &s; 276 curr = &tail( s ); 277 } // post: elts = null. 278 279 void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) { 280 ((ColIter &)si){}; 281 seq = &s; 282 curr = &start; 283 } // post: elts = null. 284 285 void over( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { 286 seq = &s; 287 curr = &tail( s ); 288 } // post: elts = {e in s}. 289 290 bool ?>>?( SeqIterRev(T) & si, T && tp ) with( si ) { 291 if ( curr ) { 292 &tp = Curr( si ); 293 T * n = pred( *seq, Curr( si ) ); 294 curr = n == &tail( *seq ) ? 0p : n; 295 } else &tp = 0p; 296 return &tp != 0p; 297 } 298 } // distribution 299 } // distribution 218 // forall( dtype T ) { 219 // // SeqIter(T) is used to iterate over a Sequence(T) in head-to-tail order. 220 // struct SeqIter { 221 // inline ColIter; 222 // // The Sequence must be passed to pred and succ to check for the end of the Sequence and return 0p. Without 223 // // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence 224 // // to pass to succ/pred. Both stack and queue just encounter 0p since the lists are not circular. 225 // Sequence(T) * seq; // FIX ME: cannot be reference 226 // }; 227 228 // static inline { 229 // void ?{}( SeqIter(T) & si ) with( si ) { 230 // ((ColIter &)si){}; 231 // seq = 0p; 232 // } // post: elts = null. 233 234 // void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) { 235 // ((ColIter &)si){}; 236 // seq = &s; 237 // curr = &head( s ); 238 // } // post: elts = null. 239 240 // void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) { 241 // ((ColIter &)si){}; 242 // seq = &s; 243 // curr = &start; 244 // } // post: elts = null. 245 246 // void over( SeqIter(T) & si, Sequence(T) & s ) with( si ) { 247 // seq = &s; 248 // curr = &head( s ); 249 // } // post: elts = {e in s}. 250 251 // bool ?>>?( SeqIter(T) & si, T && tp ) with( si ) { 252 // if ( curr ) { 253 // &tp = Curr( si ); 254 // T * n = succ( *seq, Curr( si ) ); 255 // curr = n == &head( *seq ) ? 0p : n; 256 // } else &tp = 0p; 257 // return &tp != 0p; 258 // } 259 // } // distribution 260 261 262 // // A SeqIterRev(T) is used to iterate over a Sequence(T) in tail-to-head order. 263 // struct SeqIterRev { 264 // inline ColIter; 265 // // See above for explanation. 266 // Sequence(T) * seq; // FIX ME: cannot be reference 267 // }; 268 269 // static inline { 270 // void ?{}( SeqIterRev(T) & si ) with( si ) { 271 // ((ColIter &)si){}; 272 // seq = 0p; 273 // } // post: elts = null. 274 275 // void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { 276 // ((ColIter &)si){}; 277 // seq = &s; 278 // curr = &tail( s ); 279 // } // post: elts = null. 280 281 // void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) { 282 // ((ColIter &)si){}; 283 // seq = &s; 284 // curr = &start; 285 // } // post: elts = null. 286 287 // void over( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) { 288 // seq = &s; 289 // curr = &tail( s ); 290 // } // post: elts = {e in s}. 291 292 // bool ?>>?( SeqIterRev(T) & si, T && tp ) with( si ) { 293 // if ( curr ) { 294 // &tp = Curr( si ); 295 // T * n = pred( *seq, Curr( si ) ); 296 // curr = n == &tail( *seq ) ? 0p : n; 297 // } else &tp = 0p; 298 // return &tp != 0p; 299 // } 300 // } // distribution 301 // } // distribution 302 303 #endif
Note: See TracChangeset
for help on using the changeset viewer.