Changes in / [ec5d599:ad915e0]
- Location:
- libcfa/src/bits
- Files:
-
- 8 edited
-
collection.hfa (modified) (3 diffs)
-
multi_list.cfa (modified) (1 diff)
-
queue.hfa (modified) (7 diffs)
-
queue_example.cfa (modified) (1 diff)
-
sequence.hfa (modified) (13 diffs)
-
sequence_example.cfa (modified) (1 diff)
-
stack.hfa (modified) (4 diffs)
-
stack_example.cfa (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/collection.hfa
rec5d599 rad915e0 7 7 8 8 inline { 9 // PUBLIC10 11 9 void ?{}( Colable & co ) with( co ) { 12 10 next = 0p; … … 18 16 } 19 17 20 Colable &getNext( Colable & co ) with( co ) {21 return *next;18 Colable * getNext( Colable & co ) with( co ) { 19 return next; 22 20 } 23 24 // PRIVATE25 21 26 22 Colable *& Next( Colable * cp ) { … … 28 24 } 29 25 30 // wrappers to make Collection have T31 26 forall( dtype T ) { 32 T *& Next( T &n ) {33 return (T *)Next( (Colable *) &n );27 T *& Next( T * n ) { 28 return (T *)Next( (Colable *)n ); 34 29 } 35 30 -
libcfa/src/bits/multi_list.cfa
rec5d599 rad915e0 57 57 58 58 SeqIter(TaskDL) sqiter; 59 TaskDL & dl; // iterator index59 TaskDL & dl; 60 60 TaskSL & sl; 61 61 -
libcfa/src/bits/queue.hfa
rec5d599 rad915e0 28 28 29 29 T * succ( Queue(T) & q, T * n ) with( q ) { // pre: *n in *q 30 #ifdef __CFA_DEBUG__30 #ifdef __CFA_DEBUG__ 31 31 if ( ! listed( n ) ) abort( "(Queue &)%p.succ( %p ) : Node is not on a list.", &q, n ); 32 #endif // __CFA_DEBUG__33 return (Next( *n ) == n) ? 0p : Next( *n );32 #endif // __CFA_DEBUG__ 33 return (Next( n ) == n) ? 0p : Next( n ); 34 34 } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *q 35 35 36 36 void addHead( Queue(T) & q, T & n ) with( q ) { 37 #ifdef __CFA_DEBUG__37 #ifdef __CFA_DEBUG__ 38 38 if ( listed( &n ) ) abort( "(Queue &)%p.addHead( %p ) : Node is already on another list.", &q, &n ); 39 #endif // __CFA_DEBUG__39 #endif // __CFA_DEBUG__ 40 40 if ( last ) { 41 Next( n ) = &head( q );41 Next( &n ) = &head( q ); 42 42 q.root = &n; 43 43 } else { 44 44 root = last = &n; 45 Next( n ) = &n; // last node points to itself45 Next( &n ) = &n; // last node points to itself 46 46 } 47 47 } 48 48 49 49 void addTail( Queue(T) & q, T & n ) with( q ) { 50 #ifdef __CFA_DEBUG__50 #ifdef __CFA_DEBUG__ 51 51 if ( listed( &n ) ) abort( "(Queue &)%p.addTail( %p ) : Node is already on another list.", &q, &n ); 52 #endif // __CFA_DEBUG__53 if ( last ) Next( *last ) = &n;52 #endif // __CFA_DEBUG__ 53 if ( last ) Next( last ) = &n; 54 54 else root = &n; 55 55 last = &n; 56 Next( n ) = &n; // last node points to itself56 Next( &n ) = &n; // last node points to itself 57 57 } 58 58 … … 64 64 T & t = head( q ); 65 65 if ( root ) { 66 root = Next( *root );66 root = Next( root ); 67 67 if ( &head( q ) == &t ) { 68 68 root = last = 0p; // only one element 69 69 } 70 Next( t ) = 0p;70 Next( &t ) = 0p; 71 71 } 72 72 return t; … … 78 78 79 79 void remove( Queue(T) & q, T & n ) with( q ) { // O(n) 80 #ifdef __CFA_DEBUG__80 #ifdef __CFA_DEBUG__ 81 81 if ( ! listed( (Colable &)n ) ) abort( "(Queue &)%p.remove( %p ) : Node is not on a list.", &q, &n ); 82 #endif // __CFA_DEBUG__83 T & prev = *0p;84 T & curr = *(T *)root;82 #endif // __CFA_DEBUG__ 83 T * prev = 0p; 84 T * curr = (T *)root; 85 85 for ( ;; ) { 86 if ( &n == &curr ) { // found => remove86 if ( &n == curr ) { // found => remove 87 87 if ( (T *)root == &n ) { 88 88 dropHead( q ); 89 89 } else if ( last == &n ) { 90 last = &prev;91 Next( *last ) = last;90 last = prev; 91 Next( last ) = last; 92 92 } else { 93 93 Next( prev ) = Next( curr ); 94 94 } 95 Next( n ) = 0p;95 Next( &n ) = 0p; 96 96 break; 97 97 } 98 #ifdef __CFA_DEBUG__ 98 99 // not found => error 99 #ifdef __CFA_DEBUG__ 100 if ( &curr == last ) abort( "(Queue &)%p.remove( %p ) : Node is not in list.", &q, &n ); 101 #endif // __CFA_DEBUG__ 102 &prev = &curr; 103 &curr = Next( curr ); 100 if (curr == last) abort( "(Queue &)%p.remove( %p ) : Node is not in list.", &q, &n ); 101 #endif // __CFA_DEBUG__ 102 prev = curr; 103 curr = Next( curr ); 104 104 } 105 105 } // post: ! listed( n ) … … 116 116 root = from.root; 117 117 } else { // "to" list not empty 118 Next( *last ) = &head( from );118 Next( last ) = &head( from ); 119 119 } 120 120 last = from.last; … … 125 125 // Node "n" must be in the "from" list. 126 126 void split( Queue(T) & q, Queue(T) & from, T & n ) with( q ) { 127 #ifdef __CFA_DEBUG__127 #ifdef __CFA_DEBUG__ 128 128 if ( ! listed( (Colable &)n ) ) abort( "(Queue &)%p.split( %p ) : Node is not on a list.", &q, &n ); 129 #endif // __CFA_DEBUG__129 #endif // __CFA_DEBUG__ 130 130 Queue(T) to; 131 131 to.root = from.root; // start of "to" list 132 132 to.last = &n; // end of "to" list 133 from.root = Next( n ); // start of "from" list133 from.root = Next( &n ); // start of "from" list 134 134 if ( &n == &head( from ) ) { // last node in list ? 135 135 from.root = from.last = 0p; // mark "from" list empty 136 136 } else { 137 Next( n ) = &n;// fix end of "to" list137 Next( &n ) = &n; // fix end of "to" list 138 138 } 139 139 transfer( q, to ); … … 169 169 if ( curr ) { 170 170 &tp = Curr( qi ); 171 T * n = Next( *Curr( qi ) );171 T * n = Next( Curr( qi ) ); 172 172 curr = (n == Curr( qi ) ) ? 0p : n; 173 173 } else &tp = 0p; … … 177 177 } // distribution 178 178 } // distribution 179 180 // Local Variables: // 181 // compile-command: "cfa queue.cfa" // 182 // End: // -
libcfa/src/bits/queue_example.cfa
rec5d599 rad915e0 107 107 } 108 108 } 109 110 // Local Variables: // 111 // compile-command: "cfa queue_example.cfa" // 112 // End: // -
libcfa/src/bits/sequence.hfa
rec5d599 rad915e0 9 9 10 10 inline { 11 // PUBLIC12 13 11 void ?{}( Seqable & sq ) with( sq ) { 14 12 ((Colable &) sq){}; … … 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 34 T & head( Sequence(T) & s ) with( s ) {35 return *(T *)head( (Collection &)s );36 } // post: empty() & head() == 0 | !empty() & head() in *s37 } // distribution38 23 } // distribution 39 24 … … 44 29 45 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 46 40 void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy 47 41 Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment … … 53 47 // Return a pointer to the last sequence element, without removing it. 54 48 T & tail( Sequence(T) & s ) with( s ) { 55 return root ? (T &)*Back( head( s ) ) : *0p;49 return root ? (T &)*Back( &head( s ) ) : *0p; 56 50 } // post: empty() & tail() == 0 | !empty() & tail() in *s 57 51 58 // 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. 59 53 T * succ( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s 60 #ifdef __CFA_DEBUG__54 #ifdef __CFA_DEBUG__ 61 55 if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n ); 62 #endif // __CFA_DEBUG__63 return Next( *n ) == &head( s ) ? 0p : Next( *n );56 #endif // __CFA_DEBUG__ 57 return Next( n ) == &head( s ) ? 0p : Next( n ); 64 58 } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s 65 59 66 60 // Return a pointer to the element before *n, or 0p if there isn't one. 67 61 T * pred( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s 68 #ifdef __CFA_DEBUG__62 #ifdef __CFA_DEBUG__ 69 63 if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n ); 70 #endif // __CFA_DEBUG__71 return n == &head( s ) ? 0p : Back( *n );64 #endif // __CFA_DEBUG__ 65 return n == &head( s ) ? 0p : Back( n ); 72 66 } // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s 73 67 … … 75 69 // Insert *n into the sequence before *bef, or at the end if bef == 0. 76 70 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s 77 #ifdef __CFA_DEBUG__71 #ifdef __CFA_DEBUG__ 78 72 if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef ); 79 #endif // __CFA_DEBUG__73 #endif // __CFA_DEBUG__ 80 74 if ( &bef == &head( s ) ) { // must change root 81 75 if ( root ) { 82 Next( n ) = &head( s );83 Back( n ) = Back(head( s ) );76 Next( &n ) = &head( s ); 77 Back( &n ) = Back( &head( s ) ); 84 78 // inserted node must be consistent before it is seen 85 79 asm( "" : : : "memory" ); // prevent code movement across barrier 86 Back( head( s ) ) = &n;87 Next( *Back(n ) ) = &n;80 Back( &head( s ) ) = &n; 81 Next( Back( &n ) ) = &n; 88 82 } else { 89 Next( n ) = &n;90 Back( n ) = &n;83 Next( &n ) = &n; 84 Back( &n ) = &n; 91 85 } // if 92 86 // inserted node must be consistent before it is seen … … 95 89 } else { 96 90 if ( ! &bef ) &bef = &head( s ); 97 Next( n ) = &bef;98 Back( n ) = Back(bef );91 Next( &n ) = &bef; 92 Back( &n ) = Back( &bef ); 99 93 // inserted node must be consistent before it is seen 100 94 asm( "" : : : "memory" ); // prevent code movement across barrier 101 Back( bef ) = &n;102 Next( *Back(n ) ) = &n;95 Back( &bef ) = &n; 96 Next( Back( &n ) ) = &n; 103 97 } // if 104 98 } // post: n->listed() & *n in *s & succ(n) == bef … … 107 101 // Insert *n into the sequence after *aft, or at the beginning if aft == 0. 108 102 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) { // pre: !n->listed() & *aft in *s 109 #ifdef __CFA_DEBUG__103 #ifdef __CFA_DEBUG__ 110 104 if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n ); 111 #endif // __CFA_DEBUG__105 #endif // __CFA_DEBUG__ 112 106 if ( ! &aft ) { // must change root 113 107 if ( root ) { 114 Next( n ) = &head( s );115 Back( n ) = Back(head( s ) );108 Next( &n ) = &head( s ); 109 Back( &n ) = Back( &head( s ) ); 116 110 // inserted node must be consistent before it is seen 117 111 asm( "" : : : "memory" ); // prevent code movement across barrier 118 Back( head( s ) ) = &n;119 Next( *Back(n ) ) = &n;112 Back( &head( s ) ) = &n; 113 Next( Back( &n ) ) = &n; 120 114 } else { 121 Next( n ) = &n;122 Back( n ) = &n;115 Next( &n ) = &n; 116 Back( &n ) = &n; 123 117 } // if 124 118 asm( "" : : : "memory" ); // prevent code movement across barrier 125 119 root = &n; 126 120 } else { 127 Next( n ) = Next(aft );128 Back( n ) = &aft;121 Next( &n ) = Next( &aft ); 122 Back( &n ) = &aft; 129 123 // inserted node must be consistent before it is seen 130 124 asm( "" : : : "memory" ); // prevent code movement across barrier 131 Back( *Next(n ) ) = &n;132 Next( aft ) = &n;125 Back( Next( &n ) ) = &n; 126 Next( &aft ) = &n; 133 127 } // if 134 128 } // post: n->listed() & *n in *s & succ(n) == bef … … 136 130 // pre: n->listed() & *n in *s 137 131 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1) 138 #ifdef __CFA_DEBUG__132 #ifdef __CFA_DEBUG__ 139 133 if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n ); 140 #endif // __CFA_DEBUG__134 #endif // __CFA_DEBUG__ 141 135 if ( &n == &head( s ) ) { 142 if ( Next( head( s ) ) == &head( s ) ) root = 0p;143 else root = Next( head( s ) );144 } // if 145 Back( *Next( n ) ) = Back(n );146 Next( *Back( n ) ) = Next(n );147 Next( n ) = Back(n ) = 0p;136 if ( Next( &head( s ) ) == &head( s ) ) root = 0p; 137 else root = Next( &head( s ) ); 138 } // if 139 Back( Next( &n ) ) = Back( &n ); 140 Next( Back( &n ) ) = Next( &n ); 141 Next( &n ) = Back( &n ) = 0p; 148 142 } // post: !n->listed(). 149 143 … … 181 175 root = from.root; 182 176 } else { // "to" list not empty 183 T * toEnd = Back( head( s ) );184 T * fromEnd = Back( head( from ) );185 Back( *root ) = fromEnd;186 Next( *fromEnd ) = &head( s );187 Back( *from.root ) = toEnd;188 Next( *toEnd ) = &head( from );177 T * toEnd = Back( &head( s ) ); 178 T * fromEnd = Back( &head( from ) ); 179 Back( root ) = fromEnd; 180 Next( fromEnd ) = &head( s ); 181 Back( from.root ) = toEnd; 182 Next( toEnd ) = &head( from ); 189 183 } // if 190 184 from.root = 0p; // mark "from" list empty … … 194 188 // Node "n" must be in the "from" list. 195 189 void split( Sequence(T) & s, Sequence(T) & from, T & n ) with( s ) { 196 #ifdef __CFA_DEBUG__190 #ifdef __CFA_DEBUG__ 197 191 if ( ! listed( &n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, &n ); 198 #endif // __CFA_DEBUG__192 #endif // __CFA_DEBUG__ 199 193 Sequence(T) to; 200 194 to.root = from.root; // start of "to" list 201 from.root = Next( n ); // start of "from" list195 from.root = Next( &n ); // start of "from" list 202 196 if ( to.root == from.root ) { // last node in list ? 203 197 from.root = 0p; // mark "from" list empty 204 198 } else { 205 Back( head( from ) ) = Back(head( to ) ); // fix "from" list206 Next( *Back(head( to ) ) ) = &head( from );207 Next( n ) = &head( to );// fix "to" list208 Back( head( 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; 209 203 } // if 210 204 transfer( s, to ); … … 220 214 // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence 221 215 // to pass to succ/pred. Both stack and queue just encounter 0p since the lists are not circular. 222 Sequence(T) * seq; // FIX ME: cannot be reference216 Sequence(T) * seq; 223 217 }; 224 218 … … 261 255 inline ColIter; 262 256 // See above for explanation. 263 Sequence(T) * seq; // FIX ME: cannot be reference257 Sequence(T) * seq; 264 258 }; 265 259 … … 297 291 } // distribution 298 292 } // distribution 293 294 // Local Variables: // 295 // compile-command: "cfa sequence.hfa" // 296 // End: // -
libcfa/src/bits/sequence_example.cfa
rec5d599 rad915e0 137 137 } 138 138 } 139 140 // Local Variables: // 141 // compile-command: "cfa sequence_example.cfa" // 142 // End: // -
libcfa/src/bits/stack.hfa
rec5d599 rad915e0 26 26 27 27 void addHead( Stack(T) & s, T & n ) with( s ) { 28 #ifdef __CFA_DEBUG__28 #ifdef __CFA_DEBUG__ 29 29 if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n ); 30 #endif // __CFA_DEBUG__31 Next( n ) = &head( s ) ? &head( s ) : &n;30 #endif // __CFA_DEBUG__ 31 Next( &n ) = &head( s ) ? &head( s ) : &n; 32 32 root = &n; 33 33 } … … 44 44 T & t = head( s ); 45 45 if ( root ) { 46 root = ( T *)Next( *root);46 root = ( T *)Next(root); 47 47 if ( &head( s ) == &t ) root = 0p; // only one element ? 48 Next( t ) = 0p;48 Next( &t ) = 0p; 49 49 } // if 50 50 return t; … … 85 85 if ( curr ) { 86 86 &tp = Curr( si ); 87 T * n = Next( *Curr( si ) );87 T * n = Next( Curr( si ) ); 88 88 curr = n == Curr( si ) ? 0p : n; 89 89 } else &tp = 0p; … … 92 92 } // distribution 93 93 } // distribution 94 95 // Local Variables: // 96 // compile-command: "make install" // 97 // End: // -
libcfa/src/bits/stack_example.cfa
rec5d599 rad915e0 107 107 } 108 108 } 109 110 // Local Variables: // 111 // compile-command: "cfa stack_example.cfa" // 112 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.