Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/bits/sequence.hfa

    r7c1144b ra32cbac2  
    99
    1010inline {
    11         // PUBLIC
    12 
    1311        void ?{}( Seqable & sq ) with( sq ) {
    1412                ((Colable &) sq){};
     
    2018        }
    2119
    22         // PRIVATE
    23 
    2420        Seqable *& Back( Seqable * sq ) {
    2521                return sq->back;
    2622        }
    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 
    34                 T & head( Sequence(T) & s ) with( s ) {
    35                         return *(T *)head( (Collection &)s );
    36                 } // post: empty() & head() == 0 | !empty() & head() in *s
    37         } // distribution
    3823} // distribution
    3924
     
    4429
    4530        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
    4640                void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy
    4741                Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment
     
    5347                // Return a pointer to the last sequence element, without removing it. 
    5448                T & tail( Sequence(T) & s ) with( s ) {
    55                         return root ? (T &)*Back( head( s ) ) : *0p;
     49                        return root ? (T &)*Back( &head( s ) ) : *0p;
    5650                }       // post: empty() & tail() == 0 | !empty() & tail() in *s
    5751
    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.
    5953                T * succ( Sequence(T) & s, T * n ) with( s ) {  // pre: *n in *s
    60                         #ifdef __CFA_DEBUG__
     54#ifdef __CFA_DEBUG__
    6155                        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 );
    6458                } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s
    6559
    6660                // Return a pointer to the element before *n, or 0p if there isn't one.
    6761                T * pred( Sequence(T) & s, T * n ) with( s ) {  // pre: *n in *s
    68                         #ifdef __CFA_DEBUG__
     62#ifdef __CFA_DEBUG__
    6963                        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 );
    7266                }       // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
    7367
     
    7569                // Insert *n into the sequence before *bef, or at the end if bef == 0.
    7670                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__
    7872                        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__
    8074                        if ( &bef == &head( s ) ) {                                     // must change root
    8175                                if ( root ) {
    82                                         Next( n ) = &head( s );
    83                                         Back( n ) = Back( head( s ) );
     76                                        Next( &n ) = &head( s );
     77                                        Back( &n ) = Back( &head( s ) );
    8478                                        // inserted node must be consistent before it is seen
    8579                                        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;
    8882                                } else {
    89                                         Next( n ) = &n;
    90                                         Back( n ) = &n;
     83                                        Next( &n ) = &n;
     84                                        Back( &n ) = &n;
    9185                                } // if
    9286                                // inserted node must be consistent before it is seen
     
    9589                        } else {
    9690                                if ( ! &bef ) &bef = &head( s );
    97                                 Next( n ) = &bef;
    98                                 Back( n ) = Back( bef );
     91                                Next( &n ) = &bef;
     92                                Back( &n ) = Back( &bef );
    9993                                // inserted node must be consistent before it is seen
    10094                                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;
    10397                        } // if
    10498                }       // post: n->listed() & *n in *s & succ(n) == bef
     
    107101                // Insert *n into the sequence after *aft, or at the beginning if aft == 0.
    108102                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__
    110104                        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__
    112106                        if ( ! &aft ) {                                                         // must change root
    113107                                if ( root ) {
    114                                         Next( n ) = &head( s );
    115                                         Back( n ) = Back( head( s ) );
     108                                        Next( &n ) = &head( s );
     109                                        Back( &n ) = Back( &head( s ) );
    116110                                        // inserted node must be consistent before it is seen
    117111                                        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;
    120114                                } else {
    121                                         Next( n ) = &n;
    122                                         Back( n ) = &n;
     115                                        Next( &n ) = &n;
     116                                        Back( &n ) = &n;
    123117                                } // if
    124118                                asm( "" : : : "memory" );                               // prevent code movement across barrier
    125119                                root = &n;
    126120                        } else {
    127                                 Next( n ) = Next( aft );
    128                                 Back( n ) = &aft;
     121                                Next( &n ) = Next( &aft );
     122                                Back( &n ) = &aft;
    129123                                // inserted node must be consistent before it is seen
    130124                                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;
    133127                        } // if
    134128                }         // post: n->listed() & *n in *s & succ(n) == bef
     
    136130                // pre: n->listed() & *n in *s
    137131                void remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
    138                         #ifdef __CFA_DEBUG__
     132#ifdef __CFA_DEBUG__
    139133                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
    140                         #endif // __CFA_DEBUG__
     134#endif // __CFA_DEBUG__
    141135                        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;
    148142                }                                                       // post: !n->listed().
    149143
     
    181175                                root = from.root;
    182176                        } 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 );
    189183                        } // if
    190184                        from.root = 0p;                                                         // mark "from" list empty
     
    194188                // Node "n" must be in the "from" list.
    195189                void split( Sequence(T) & s, Sequence(T) & from, T & n ) with( s ) {
    196                         #ifdef __CFA_DEBUG__
     190#ifdef __CFA_DEBUG__
    197191                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, &n );
    198                         #endif // __CFA_DEBUG__
     192#endif // __CFA_DEBUG__
    199193                        Sequence(T) to;
    200194                        to.root = from.root;                                            // start of "to" list
    201                         from.root = Next( n );                                          // start of "from" list
     195                        from.root = Next( &n );                                         // start of "from" list
    202196                        if ( to.root == from.root ) {                           // last node in list ?
    203197                                from.root = 0p;                                                 // mark "from" list empty
    204198                        } else {
    205                                 Back( head( from ) ) = Back( head( to ) ); // fix "from" list
    206                                 Next( *Back( head( to ) ) ) = &head( from );
    207                                 Next( n ) = &head( to );                                // fix "to" list
    208                                 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;
    209203                        } // if
    210204                        transfer( s, to );
     
    220214                // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence
    221215                // 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 reference
     216                Sequence(T) * seq;
    223217        };
    224218
     
    261255                inline ColIter;
    262256                // See above for explanation.
    263                 Sequence(T) * seq;                                                              // FIX ME: cannot be reference
     257                Sequence(T) * seq;
    264258        };
    265259
     
    297291        } // distribution
    298292} // distribution
     293
     294// Local Variables: //
     295// compile-command: "cfa sequence.hfa" //
     296// End: //
Note: See TracChangeset for help on using the changeset viewer.