Changeset 636d3715 for libcfa/src/bits
 Timestamp:
 Dec 3, 2020, 11:56:01 AM (14 months ago)
 Branches:
 armeh, jacob/cs343translation, master, newastuniqueexpr
 Children:
 89c982c, a78c3ff, cc5cc27
 Parents:
 1db306a
 Location:
 libcfa/src/bits
 Files:

 5 edited
Legend:
 Unmodified
 Added
 Removed

libcfa/src/bits/collection.hfa
r1db306a r636d3715 13 13 // return true iff *this is an element of a collection 14 14 bool listed( Colable & co ) with( co ) { // pre: this != 0 15 return next != 0 ;15 return next != 0p; 16 16 } 17 17 … … 23 23 return cp>next; 24 24 } 25 26 forall( dtype T ) { 27 T *& Next( T * n ) { 28 return (T *)Next( (Colable *)n ); 29 } 30 31 bool listed( T * n ) { 32 return Next( (Colable *)n ) != 0p; 33 } 34 } // distribution 25 35 } // distribution 36 26 37 27 38 struct Collection { … … 41 52 return root == 0p; 42 53 } 54 43 55 void * head( Collection & collection ) with( collection ) { 44 56 return root; … … 55 67 curr = 0p; 56 68 } // post: elts = null 69 70 forall( dtype T ) { 71 T * Curr( ColIter & ci ) with( ci ) { 72 return (T *)curr; 73 } 74 } // distribution 57 75 } // distribution 
libcfa/src/bits/queue.hfa
r1db306a r636d3715 14 14 return (T *)head( (Collection &)q ); 15 15 } // post: empty() & head() == 0  !empty() & head() in *q 16 17 bool empty( Queue(T) & q ) with( q ) { // 0 <=> *q contains no elements18 return empty( (Collection &)q );19 }20 21 bool listed( T * n ) {22 return Next( (Colable *)n ) != 0;23 }24 25 T *& Next( T * n ) {26 return (T *)Next( (Colable *)n );27 }28 29 T * Root( Queue(T) & q ) with( q ) {30 return (T *)root;31 }32 16 33 17 void ?{}( Queue(T) &, const Queue(T) & ) = void; // no copy … … 55 39 #endif // __CFA_DEBUG__ 56 40 if ( last ) { 57 Next( n ) = Root( q );41 Next( n ) = head( q ); 58 42 q.root = n; 59 43 } else { … … 81 65 if ( root ) { 82 66 root = Next( root ); 83 if ( Root( q ) == t ) {67 if ( head( q ) == t ) { 84 68 root = last = 0p; // only one element 85 69 } … … 132 116 root = from.root; 133 117 } else { // "to" list not empty 134 Next( last ) = Root( from );118 Next( last ) = head( from ); 135 119 } 136 120 last = from.last; … … 148 132 to.last = n; // end of "to" list 149 133 from.root = Next( n ); // start of "from" list 150 if ( n == Root( from ) ) { // last node in list ?134 if ( n == head( from ) ) { // last node in list ? 151 135 from.root = from.last = 0p; // mark "from" list empty 152 136 } else { … … 164 148 165 149 inline { 166 // wrappers to make ColIter have T167 T * Curr( QueueIter(T) & qi ) with( qi ) {168 return (T *)curr;169 }170 171 150 void ?{}( QueueIter(T) & qi ) with( qi ) { 172 151 ((ColIter &)qi){}; 
libcfa/src/bits/sequence.hfa
r1db306a r636d3715 10 10 inline { 11 11 void ?{}( Seqable & sq ) with( sq ) { 12 ((Colable & 12 ((Colable &) sq){}; 13 13 back = 0p; 14 14 } // post: ! listed() … … 34 34 } // post: empty() & head() == 0  !empty() & head() in *s 35 35 36 bool empty( Sequence(T) & s ) with( s ) { // 0 <=> *s contains no elements37 return empty( (Collection &)s );38 }39 40 bool listed( T * n ) {41 return Next( (Colable *)n ) != 0;42 }43 44 T *& Next( T * n ) {45 return (T *)Next( (Colable *)n );46 }47 48 36 T *& Back( T * n ) { 49 37 return (T *)Back( (Seqable *)n ); 50 }51 52 T * Root( Sequence(T) & s ) with( s ) {53 return (T *)root;54 38 } 55 39 … … 63 47 // Return a pointer to the last sequence element, without removing it. 64 48 T & tail( Sequence(T) & s ) with( s ) { 65 return root ? (T &)Back( Root( s ) ) : *0p; // needs cast?49 return root ? (T &)Back( head( s ) ) : *0p; // needs cast? 66 50 } // post: empty() & tail() == 0  !empty() & tail() in *s 67 51 … … 71 55 if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n ); 72 56 #endif // __CFA_DEBUG__ 73 return Next( n ) == Root( s ) ? 0p : Next( n );57 return Next( n ) == head( s ) ? 0p : Next( n ); 74 58 } // post: n == tail() & succ(n) == 0  n != tail() & *succ(n) in *s 75 59 … … 79 63 if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n ); 80 64 #endif // __CFA_DEBUG__ 81 return n == Root( s ) ? 0p : Back( n );65 return n == head( s ) ? 0p : Back( n ); 82 66 } // post: n == head() & head(n) == 0  n != head() & *pred(n) in *s 83 67 … … 88 72 if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef ); 89 73 #endif // __CFA_DEBUG__ 90 if ( &bef == Root( s ) ) { // must change root74 if ( &bef == head( s ) ) { // must change root 91 75 if ( root ) { 92 Next( &n ) = Root( s );93 Back( &n ) = Back( Root( s ) );76 Next( &n ) = head( s ); 77 Back( &n ) = Back( head( s ) ); 94 78 // inserted node must be consistent before it is seen 95 79 asm( "" : : : "memory" ); // prevent code movement across barrier 96 Back( Root( s ) ) = &n;80 Back( head( s ) ) = &n; 97 81 Next( Back( &n ) ) = &n; 98 82 } else { … … 104 88 root = &n; 105 89 } else { 106 if ( ! &bef ) &bef = Root( s );90 if ( ! &bef ) &bef = head( s ); 107 91 Next( &n ) = &bef; 108 92 Back( &n ) = Back( &bef ); … … 122 106 if ( ! &aft ) { // must change root 123 107 if ( root ) { 124 Next( &n ) = Root( s );125 Back( &n ) = Back( Root( s ) );108 Next( &n ) = head( s ); 109 Back( &n ) = Back( head( s ) ); 126 110 // inserted node must be consistent before it is seen 127 111 asm( "" : : : "memory" ); // prevent code movement across barrier 128 Back( Root( s ) ) = &n;112 Back( head( s ) ) = &n; 129 113 Next( Back( &n ) ) = &n; 130 114 } else { … … 149 133 if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n ); 150 134 #endif // __CFA_DEBUG__ 151 if ( &n == Root( s ) ) {152 if ( Next( Root( s ) ) == Root( s ) ) root = 0p;153 else root = Next( Root(s ) );135 if ( &n == head( s ) ) { 136 if ( Next( head( s ) ) == head( s ) ) root = 0p; 137 else root = Next( head(s ) ); 154 138 } // if 155 139 Back( Next( &n ) ) = Back( &n ); … … 191 175 root = from.root; 192 176 } else { // "to" list not empty 193 T * toEnd = Back( Root( s ) );194 T * fromEnd = Back( Root( from ) );177 T * toEnd = Back( head( s ) ); 178 T * fromEnd = Back( head( from ) ); 195 179 Back( root ) = fromEnd; 196 Next( fromEnd ) = Root( s );180 Next( fromEnd ) = head( s ); 197 181 Back( from.root ) = toEnd; 198 Next( toEnd ) = Root( from );182 Next( toEnd ) = head( from ); 199 183 } // if 200 184 from.root = 0p; // mark "from" list empty … … 213 197 from.root = 0p; // mark "from" list empty 214 198 } else { 215 Back( Root( from ) ) = Back( Root( to ) ); // fix "from" list216 Next( Back( Root( to ) ) ) = Root( from );217 Next( n ) = Root( to ); // fix "to" list218 Back( Root( to ) ) = n;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; 219 203 } // if 220 204 transfer( s, to ); … … 231 215 232 216 inline { 233 // wrappers to make ColIter have T 234 T * Curr( SeqIter(T) & si ) with( si ) { 235 return (T *)curr; 236 } 237 238 void ?{}( SeqIter(T) & si ) with( si ) { 239 ((ColIter &) si){}; 217 void ?{}( SeqIter(T) & si ) with( si ) { 218 ((ColIter &)si){}; 240 219 seq = 0p; 241 220 } // post: elts = null. … … 270 249 271 250 inline { 272 // wrappers to make ColIter have T273 T * Curr( SeqIterRev(T) & si ) with( si ) {274 return (T *)curr;275 }276 277 251 void ?{}( SeqIterRev(T) & si ) with( si ) { 278 252 ((ColIter &) si){}; 
libcfa/src/bits/stack.hfa
r1db306a r636d3715 14 14 } // post: empty() & head() == 0  !empty() & head() in *this 15 15 16 bool empty( Stack(T) & s ) with( s ) { // 0 <=> *this contains no elements17 return empty( (Collection &)s );18 }19 20 T *& Next( T * n ) {21 return (T *)Next( (Colable *)n );22 }23 24 T * Root( Stack(T) & s ) with( s ) {25 return (T *)root;26 }27 28 16 void ?{}( Stack(T) &, const Stack(T) & ) = void; // no copy 29 17 Stack(T) & ?=?( const Stack(T) & ) = void; // no assignment … … 33 21 } // post: empty() 34 22 35 T *top( Stack(T) & s ) with( s ) {36 return head( s );23 T & top( Stack(T) & s ) with( s ) { 24 return *head( s ); 37 25 } 38 26 39 void addHead( Stack(T) & s, T *n ) with( s ) {27 void addHead( Stack(T) & s, T & n ) with( s ) { 40 28 #ifdef __CFA_DEBUG__ 41 if ( listed( (Colable &)( *n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );29 if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n ); 42 30 #endif // __CFA_DEBUG__ 43 Next( n ) = Root( s ) ? Root( s ) :n;44 root = n;31 Next( &n ) = head( s ) ? head( s ) : &n; 32 root = &n; 45 33 } 46 34 47 void add( Stack(T) & s, T *n ) with( s ) {35 void add( Stack(T) & s, T & n ) with( s ) { 48 36 addHead( s, n ); 49 37 } 50 38 51 void push( Stack(T) & s, T *n ) with( s ) {39 void push( Stack(T) & s, T & n ) with( s ) { 52 40 addHead( s, n ); 53 41 } 54 42 55 T *drop( Stack(T) & s ) with( s ) {56 T * t =head( s );43 T & drop( Stack(T) & s ) with( s ) { 44 T & t = *head( s ); 57 45 if ( root ) { 58 46 root = ( T *)Next(root); 59 if ( Root( s ) ==t ) root = 0p; // only one element ?60 Next( t ) = 0p;47 if ( head( s ) == &t ) root = 0p; // only one element ? 48 Next( &t ) = 0p; 61 49 } // if 62 50 return t; 63 51 } 64 52 65 T *pop( Stack(T) & s ) with( s ) {53 T & pop( Stack(T) & s ) with( s ) { 66 54 return drop( s ); 67 55 } … … 76 64 77 65 inline { 78 // wrappers to make ColIter have T79 T * Curr( StackIter(T) & si ) with( si ) {80 return (T *)curr;81 }82 83 66 void ?{}( StackIter(T) & si ) with( si ) { 84 67 ((ColIter &)si){}; … … 90 73 } // post: curr = {e in s} 91 74 92 void ?{}( StackIter(T) & si, T *start ) with( si ) {93 curr = start;75 void ?{}( StackIter(T) & si, T & start ) with( si ) { 76 curr = &start; 94 77 } // post: curr = {e in s} 95 78 … … 103 86 &tp = Curr( si ); 104 87 T * n = Next( Curr( si ) ); 105 curr = (n == Curr( si )) ? 0p : n;88 curr = n == Curr( si ) ? 0p : n; 106 89 } else &tp = 0p; 107 90 return &tp != 0p; 
libcfa/src/bits/stack_example.cfa
r1db306a r636d3715 27 27 28 28 for ( i; 10 ) { 29 push( fred, new( 2 * i ) );29 push( fred, *new( 2 * i ) ); 30 30 } 31 31 … … 36 36 37 37 for ( i; 9 ) { 38 delete( pop( fred ) );38 delete( &pop( fred ) ); 39 39 } 40 40 … … 45 45 46 46 for ( i; 10 ) { 47 push( fred, new( 2 * i + 1 ) );47 push( fred, *new( 2 * i + 1 ) ); 48 48 } 49 49 for ( over( fredIter, fred ); fredIter >> f; ) { … … 78 78 79 79 for ( i; 10 ) { 80 push( mary, new( 2 * i ) );80 push( mary, *new( 2 * i ) ); 81 81 } 82 82 … … 87 87 88 88 for ( i; 9 ) { 89 delete( pop( mary ) );89 delete( &pop( mary ) ); 90 90 } 91 91 … … 96 96 97 97 for ( i; 10 ) { 98 push( mary, new( 2 * i + 1 ) );98 push( mary, *new( 2 * i + 1 ) ); 99 99 } 100 100 for ( over( maryIter, mary ); maryIter >> m; ) {
Note: See TracChangeset
for help on using the changeset viewer.