Ignore:
File:
1 edited

Legend:

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

    r7d4ce2a ra32cbac2  
    11#pragma once
    22
    3 #include "bits/collection.hfa"
     3#include "collection.hfa"
    44
    55struct Seqable {
     
    99
    1010inline {
    11         // PUBLIC
    12 
    1311        void ?{}( Seqable & sq ) with( sq ) {
    14                 ((Colable &)sq){};
     12                ((Colable &) sq){};
    1513                back = 0p;
    1614        } // post: ! listed()
     
    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         } // distribution
    3423} // distribution
    3524
     
    4534                } // post: empty() & head() == 0 | !empty() & head() in *s
    4635
     36                T *& Back( T * n ) {
     37                        return (T *)Back( (Seqable *)n );
     38                }
     39
    4740                void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy
    4841                Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment
    4942
    5043                void ?{}( Sequence(T) & s ) with( s ) {
    51                         ((Collection &)s){};
     44                        ((Collection &) s){};
    5245                }       // post: isEmpty().
    5346
     
    5750                }       // post: empty() & tail() == 0 | !empty() & tail() in *s
    5851
    59                 // 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.
    6053                T * succ( Sequence(T) & s, T * n ) with( s ) {  // pre: *n in *s
    61                         #ifdef __CFA_DEBUG__
     54#ifdef __CFA_DEBUG__
    6255                        if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n );
    63                         #endif // __CFA_DEBUG__
     56#endif // __CFA_DEBUG__
    6457                        return Next( n ) == &head( s ) ? 0p : Next( n );
    6558                } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s
     
    6760                // Return a pointer to the element before *n, or 0p if there isn't one.
    6861                T * pred( Sequence(T) & s, T * n ) with( s ) {  // pre: *n in *s
    69                         #ifdef __CFA_DEBUG__
     62#ifdef __CFA_DEBUG__
    7063                        if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n );
    71                         #endif // __CFA_DEBUG__
     64#endif // __CFA_DEBUG__
    7265                        return n == &head( s ) ? 0p : Back( n );
    7366                }       // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
     
    7669                // Insert *n into the sequence before *bef, or at the end if bef == 0.
    7770                void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
    78                         #ifdef __CFA_DEBUG__
     71#ifdef __CFA_DEBUG__
    7972                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
    80                         #endif // __CFA_DEBUG__
     73#endif // __CFA_DEBUG__
    8174                        if ( &bef == &head( s ) ) {                                     // must change root
    8275                                if ( root ) {
     
    108101                // Insert *n into the sequence after *aft, or at the beginning if aft == 0.
    109102                void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) {   // pre: !n->listed() & *aft in *s
    110                         #ifdef __CFA_DEBUG__
     103#ifdef __CFA_DEBUG__
    111104                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n );
    112                         #endif // __CFA_DEBUG__
     105#endif // __CFA_DEBUG__
    113106                        if ( ! &aft ) {                                                         // must change root
    114107                                if ( root ) {
     
    137130                // pre: n->listed() & *n in *s
    138131                void remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
    139                         #ifdef __CFA_DEBUG__
     132#ifdef __CFA_DEBUG__
    140133                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
    141                         #endif // __CFA_DEBUG__
     134#endif // __CFA_DEBUG__
    142135                        if ( &n == &head( s ) ) {
    143136                                if ( Next( &head( s ) ) == &head( s ) ) root = 0p;
     
    195188                // Node "n" must be in the "from" list.
    196189                void split( Sequence(T) & s, Sequence(T) & from, T & n ) with( s ) {
    197                         #ifdef __CFA_DEBUG__
     190#ifdef __CFA_DEBUG__
    198191                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, &n );
    199                         #endif // __CFA_DEBUG__
     192#endif // __CFA_DEBUG__
    200193                        Sequence(T) to;
    201194                        to.root = from.root;                                            // start of "to" list
     
    206199                                Back( &head( from ) ) = Back( &head( to ) ); // fix "from" list
    207200                                Next( Back( &head( to ) ) ) = &head( from );
    208                                 Next( &n ) = &head( to );                               // fix "to" list
     201                                Next( &n ) = &head( to );                                       // fix "to" list
    209202                                Back( &head( to ) ) = &n;
    210203                        } // if
     
    221214                // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence
    222215                // 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
     216                Sequence(T) * seq;
    224217        };
    225218
     
    231224
    232225                void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) {
    233                         ((ColIter &)si){};
     226                        ((ColIter &) si){};
    234227                        seq = &s;
    235228                        curr = &head( s );
     
    237230
    238231                void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) {
    239                         ((ColIter &)si){};
     232                        ((ColIter &) si){};
    240233                        seq = &s;
    241234                        curr = &start;
     
    262255                inline ColIter;
    263256                // See above for explanation.
    264                 Sequence(T) * seq;                                                              // FIX ME: cannot be reference
     257                Sequence(T) * seq;
    265258        };
    266259
    267260        inline {
    268261                void ?{}( SeqIterRev(T) & si ) with( si ) {     
    269                         ((ColIter &)si){};
     262                        ((ColIter &) si){};
    270263                        seq = 0p;
    271264                } // post: elts = null.
    272265
    273266                void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {   
    274                         ((ColIter &)si){};
     267                        ((ColIter &) si){};
    275268                        seq = &s;
    276269                        curr = &tail( s );
     
    278271
    279272                void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) {
    280                         ((ColIter &)si){};
     273                        ((ColIter &) si){};
    281274                        seq = &s;
    282275                        curr = &start;
     
    298291        } // distribution
    299292} // distribution
     293
     294// Local Variables: //
     295// compile-command: "cfa sequence.hfa" //
     296// End: //
Note: See TracChangeset for help on using the changeset viewer.