Ignore:
File:
1 edited

Legend:

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

    r636d3715 r5e82d56  
    1010inline {
    1111        void ?{}( Seqable & sq ) with( sq ) {
    12                 ((Colable &) sq){};
     12                ((Colable & ) sq){};
    1313                back = 0p;
    1414        } // post: ! listed()
     
    3434                } // post: empty() & head() == 0 | !empty() & head() in *s
    3535
     36                bool empty( Sequence(T) & s ) with( s ) {               // 0 <=> *s contains no elements
     37                        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
    3648                T *& Back( T * n ) {
    3749                        return (T *)Back( (Seqable *)n );
     50                }
     51
     52                T * Root( Sequence(T) & s ) with( s ) {
     53                        return (T *)root;
    3854                }
    3955
     
    4662
    4763                // Return a pointer to the last sequence element, without removing it. 
    48                 T & tail( Sequence(T) & s ) with( s ) {
    49                         return root ? (T &)Back( head( s ) ) : *0p;     // needs cast?
     64                T * tail( Sequence(T) & s ) with( s ) {
     65                        return root ? (T *)Back( Root( s ) ) : 0p;      // needs cast?
    5066                }       // post: empty() & tail() == 0 | !empty() & tail() in *s
    5167
     
    5571                        if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n );
    5672#endif // __CFA_DEBUG__
    57                         return Next( n ) == head( s ) ? 0p : Next( n );
     73                        return Next( n ) == Root( s ) ? 0p : Next( n );
    5874                }       // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s
    5975
     
    6379                        if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n );
    6480#endif // __CFA_DEBUG__
    65                         return n == head( s ) ? 0p : Back( n );
     81                        return n == Root( s ) ? 0p : Back( n );
    6682                }       // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
    6783
    6884
    6985                // Insert *n into the sequence before *bef, or at the end if bef == 0.
    70                 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
    71 #ifdef __CFA_DEBUG__
    72                         if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
    73 #endif // __CFA_DEBUG__
    74                         if ( &bef == head( s ) ) {                                      // must change root
     86                void insertBef( Sequence(T) & s, T * n, T * bef ) with( s ) { // pre: !n->listed() & *bef in *s
     87#ifdef __CFA_DEBUG__
     88                        if ( listed( n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, bef );
     89#endif // __CFA_DEBUG__
     90                        if ( bef == Root( s ) ) {                                       // must change root
    7591                                if ( root ) {
    76                                         Next( &n ) = head( s );
    77                                         Back( &n ) = Back( head( s ) );
     92                                        Next( n ) = Root( s );
     93                                        Back( n ) = Back( Root( s ) );
    7894                                        // inserted node must be consistent before it is seen
    7995                                        asm( "" : : : "memory" );                       // prevent code movement across barrier
    80                                         Back( head( s ) ) = &n;
    81                                         Next( Back( &n ) ) = &n;
     96                                        Back( Root( s ) ) = n;
     97                                        Next( Back( n ) ) = n;
    8298                                } else {
    83                                         Next( &n ) = &n;
    84                                         Back( &n ) = &n;
     99                                        Next( n ) = n;
     100                                        Back( n ) = n;
    85101                                } // if
    86102                                // inserted node must be consistent before it is seen
    87103                                asm( "" : : : "memory" );                               // prevent code movement across barrier
    88                                 root = &n;
     104                                root = n;
    89105                        } else {
    90                                 if ( ! &bef ) &bef = head( s );
    91                                 Next( &n ) = &bef;
    92                                 Back( &n ) = Back( &bef );
     106                                if ( ! bef ) bef = Root( s );
     107                                Next( n ) = bef;
     108                                Back( n ) = Back( bef );
    93109                                // inserted node must be consistent before it is seen
    94110                                asm( "" : : : "memory" );                               // prevent code movement across barrier
    95                                 Back( &bef ) = &n;
    96                                 Next( Back( &n ) ) = &n;
     111                                Back( bef ) = n;
     112                                Next( Back( n ) ) = n;
    97113                        } // if
    98114                }       // post: n->listed() & *n in *s & succ(n) == bef
     
    100116
    101117                // Insert *n into the sequence after *aft, or at the beginning if aft == 0.
    102                 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) {   // pre: !n->listed() & *aft in *s
    103 #ifdef __CFA_DEBUG__
    104                         if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n );
    105 #endif // __CFA_DEBUG__
    106                         if ( ! &aft ) {                                                         // must change root
     118                void insertAft( Sequence(T) & s, T *aft, T *n ) with( s ) {     // pre: !n->listed() & *aft in *s
     119#ifdef __CFA_DEBUG__
     120                        if ( listed( n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, aft, n );
     121#endif // __CFA_DEBUG__
     122                        if ( ! aft ) {                                                          // must change root
    107123                                if ( root ) {
    108                                         Next( &n ) = head( s );
    109                                         Back( &n ) = Back( head( s ) );
     124                                        Next( n ) = Root( s );
     125                                        Back( n ) = Back( Root( s ) );
    110126                                        // inserted node must be consistent before it is seen
    111127                                        asm( "" : : : "memory" );                       // prevent code movement across barrier
    112                                         Back( head( s ) ) = &n;
    113                                         Next( Back( &n ) ) = &n;
     128                                        Back( Root( s ) ) = n;
     129                                        Next( Back( n ) ) = n;
    114130                                } else {
    115                                         Next( &n ) = &n;
    116                                         Back( &n ) = &n;
     131                                        Next( n ) = n;
     132                                        Back( n ) = n;
    117133                                } // if
    118134                                asm( "" : : : "memory" );                               // prevent code movement across barrier
    119                                 root = &n;
     135                                root = n;
    120136                        } else {
    121                                 Next( &n ) = Next( &aft );
    122                                 Back( &n ) = &aft;
     137                                Next( n ) = Next( aft );
     138                                Back( n ) = aft;
    123139                                // inserted node must be consistent before it is seen
    124140                                asm( "" : : : "memory" );                               // prevent code movement across barrier
    125                                 Back( Next( &n ) ) = &n;
    126                                 Next( &aft ) = &n;
     141                                Back( Next( n ) ) = n;
     142                                Next( aft ) = n;
    127143                        } // if
    128144                }         // post: n->listed() & *n in *s & succ(n) == bef
    129145               
    130146                // pre: n->listed() & *n in *s
    131                 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
    132 #ifdef __CFA_DEBUG__
    133                         if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
    134 #endif // __CFA_DEBUG__
    135                         if ( &n == head( s ) ) {
    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;
     147                void remove( Sequence(T) & s, T *n ) with( s ) { // O(1)
     148#ifdef __CFA_DEBUG__
     149                        if ( ! listed( n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, n );
     150#endif // __CFA_DEBUG__
     151                        if ( n == Root( s ) ) {
     152                                if ( Next( Root( s ) ) == Root( s ) ) root = 0p;
     153                                else root = Next( Root(s ) );
     154                        } // if
     155                        Back( Next( n ) ) = Back( n );
     156                        Next( Back( n ) ) = Next( n );
     157                        Next( n ) = Back( n ) = 0p;
    142158                }                                                       // post: !n->listed().
    143159
    144160                // Add an element to the head of the sequence.
    145                 void addHead( Sequence(T) & s, T & n ) {                // pre: !n->listed(); post: n->listed() & head() == n
    146                         insertAft( s, *0p, n );
     161                void addHead( Sequence(T) & s, T *n ) {                 // pre: !n->listed(); post: n->listed() & head() == n
     162                        insertAft( s, 0, n );
    147163                }
    148164                // Add an element to the tail of the sequence.
    149                 void addTail( Sequence(T) & s, T & n ) {                // pre: !n->listed(); post: n->listed() & head() == n
    150                         insertBef( s, n, *0p );
     165                void addTail( Sequence(T) & s, T *n ) {                 // pre: !n->listed(); post: n->listed() & head() == n
     166                        insertBef( s, n, 0 );
    151167                }
    152168                // Add an element to the tail of the sequence.
    153                 void add( Sequence(T) & s, T & n ) {                    // pre: !n->listed(); post: n->listed() & head() == n
     169                void add( Sequence(T) & s, T *n ) {                             // pre: !n->listed(); post: n->listed() & head() == n
    154170                        addTail( s, n );
    155171                }
    156172                // Remove and return the head element in the sequence.
    157                 T & dropHead( Sequence(T) & s ) {
     173                T * dropHead( Sequence(T) & s ) {
    158174                        T * n = head( s );
    159                         return n ? remove( s, *n ), *n : *0p;
     175                        return n ? remove( s, n ), n : 0p;
    160176                }
    161177                // Remove and return the head element in the sequence.
    162                 T & drop( Sequence(T) & s ) {
     178                T * drop( Sequence(T) & s ) {
    163179                        return dropHead( s );
    164180                }
    165181                // Remove and return the tail element in the sequence.
    166                 T & dropTail( Sequence(T) & s ) {
    167                         T & n = tail( s );
    168                         return &n ? remove( s, n ), n : *0p;
     182                T * dropTail( Sequence(T) & s ) {
     183                        T * n = tail( s );
     184                        return n ? remove( s, n ), n : 0p;
    169185                }
    170186
     
    175191                                root = from.root;
    176192                        } else {                                                                        // "to" list not empty
    177                                 T * toEnd = Back( head( s ) );
    178                                 T * fromEnd = Back( head( from ) );
     193                                T * toEnd = Back( Root( s ) );
     194                                T * fromEnd = Back( Root( from ) );
    179195                                Back( root ) = fromEnd;
    180                                 Next( fromEnd ) = head( s );
     196                                Next( fromEnd ) = Root( s );
    181197                                Back( from.root ) = toEnd;
    182                                 Next( toEnd ) = head( from );
     198                                Next( toEnd ) = Root( from );
    183199                        } // if
    184200                        from.root = 0p;                                                         // mark "from" list empty
     
    197213                                from.root = 0p;                                                 // mark "from" list empty
    198214                        } else {
    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;
     215                                Back( Root( from ) ) = Back( Root( to ) ); // fix "from" list
     216                                Next( Back( Root( to ) ) ) = Root( from );
     217                                Next( n ) = Root( to );                                 // fix "to" list
     218                                Back( Root( to ) ) = n;
    203219                        } // if
    204220                        transfer( s, to );
     
    215231
    216232        inline {
    217                 void ?{}( SeqIter(T) & si ) with( si ) {
    218                         ((ColIter &)si){};
     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){};
    219240                        seq = 0p;
    220241                } // post: elts = null.
     
    223244                        ((ColIter &) si){};
    224245                        seq = &s;
    225                         curr = head( s );
    226246                } // post: elts = null.
    227247               
     
    231251                } // post: elts = {e in s}.
    232252
    233                 bool ?>>?( SeqIter(T) & si, T && tp ) with( si ) {
     253                bool ?>>?( SeqIter(T) & si, T *& tp ) with( si ) {
    234254                        if ( curr ) {
    235                                 &tp = Curr( si );
    236                                 T * n = succ( *seq, Curr( si ) );
     255                                tp = Curr( si );
     256                                T *n = succ( *seq, Curr( si ) );
    237257                                curr = n == head( *seq ) ? 0p : n;
    238                         } else &tp = 0p;
    239                         return &tp != 0p;
     258                        } else tp = 0p;
     259                        return tp != 0p;
    240260                }
    241261        } // distribution
     
    249269
    250270        inline {
     271                // wrappers to make ColIter have T
     272                T * Curr( SeqIterRev(T) & si ) with( si ) {
     273                        return (T *)curr;
     274                }
     275
    251276                void ?{}( SeqIterRev(T) & si ) with( si ) {     
    252277                        ((ColIter &) si){};
     
    257282                        ((ColIter &) si){};
    258283                        seq = &s;
    259                         curr = &tail( s );
    260284                } // post: elts = null.
    261285               
    262286                void over( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {
    263287                        seq = &s;
    264                         curr = &tail( s );
     288                        curr = tail( s );
    265289                } // post: elts = {e in s}.
    266290
    267                 bool ?>>?( SeqIterRev(T) & si, T && tp ) with( si ) {
     291                bool ?>>?( SeqIterRev(T) & si, T *&tp ) with( si ) {
    268292                        if ( curr ) {
    269                                 &tp = Curr( si );
    270                                 T * n = pred( *seq, Curr( si ) );
    271                                 curr = n == &tail( *seq ) ? 0p : n;
    272                         } else &tp = 0p;
    273                         return &tp != 0p;
     293                                tp = Curr( si );
     294                                T *n = pred( *seq, Curr( si ) );
     295                                curr = n == tail( *seq ) ? 0p : n;
     296                        } else tp = 0p;
     297                        return tp != 0p;
    274298                }
    275299        } // distribution
Note: See TracChangeset for help on using the changeset viewer.