Changes in / [cc5cc27:63ec5fa]


Ignore:
Location:
libcfa/src/bits
Files:
5 edited

Legend:

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

    rcc5cc27 r63ec5fa  
    1313        // return true iff *this is an element of a collection
    1414        bool listed( Colable & co ) with( co ) {                        // pre: this != 0
    15                 return next != 0p;
     15                return next != 0;
    1616        }
    1717
     
    2323                return cp->next;
    2424        }
    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
    3525} // distribution
    36 
    3726
    3827struct Collection {
     
    5241                return root == 0p;
    5342        }
    54 
    5543        void * head( Collection & collection ) with( collection ) {
    5644                return root;
     
    6755                curr = 0p;
    6856        } // post: elts = null
    69 
    70         forall( dtype T ) {
    71                 T * Curr( ColIter & ci ) with( ci ) {
    72                         return (T *)curr;
    73                 }
    74         } // distribution
    7557} // distribution
  • libcfa/src/bits/queue.hfa

    rcc5cc27 r63ec5fa  
    1515                } // post: empty() & head() == 0 | !empty() & head() in *q
    1616
     17                bool empty( Queue(T) & q ) with( q ) {                  // 0 <=> *q contains no elements
     18                        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
    1733                void ?{}( Queue(T) &, const Queue(T) & ) = void; // no copy
    1834                Queue(T) & ?=?( const Queue(T) & ) = void;              // no assignment
     
    3955#endif // __CFA_DEBUG__
    4056                        if ( last ) {
    41                                 Next( n ) = head( q );
     57                                Next( n ) = Root( q );
    4258                                q.root = n;
    4359                        } else {
     
    6581                        if ( root ) {
    6682                                root = Next( root );
    67                                 if ( head( q ) == t ) {
     83                                if ( Root( q ) == t ) {
    6884                                        root = last = 0p;                                       // only one element
    6985                                }
     
    116132                                root = from.root;
    117133                        } else {                                                                        // "to" list not empty
    118                                 Next( last ) = head( from );
     134                                Next( last ) = Root( from );
    119135                        }
    120136                        last = from.last;
     
    132148                        to.last = n;                                                            // end of "to" list
    133149                        from.root = Next( n );                                          // start of "from" list
    134                         if ( n == head( from ) ) {                                      // last node in list ?
     150                        if ( n == Root( from ) ) {                                      // last node in list ?
    135151                                from.root = from.last = 0p;                             // mark "from" list empty
    136152                        } else {
     
    148164
    149165        inline {
     166                // wrappers to make ColIter have T
     167                T * Curr( QueueIter(T) & qi ) with( qi ) {
     168                        return (T *)curr;
     169                }
     170
    150171                void ?{}( QueueIter(T) & qi ) with( qi ) {
    151172                        ((ColIter &)qi){};
  • libcfa/src/bits/sequence.hfa

    rcc5cc27 r63ec5fa  
    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
     
    4763                // Return a pointer to the last sequence element, without removing it. 
    4864                T & tail( Sequence(T) & s ) with( s ) {
    49                         return root ? (T &)Back( head( s ) ) : *0p;     // needs cast?
     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
     
    7288                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
    7389#endif // __CFA_DEBUG__
    74                         if ( &bef == head( s ) ) {                                      // must change root
     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;
     96                                        Back( Root( s ) ) = &n;
    8197                                        Next( Back( &n ) ) = &n;
    8298                                } else {
     
    88104                                root = &n;
    89105                        } else {
    90                                 if ( ! &bef ) &bef = head( s );
     106                                if ( ! &bef ) &bef = Root( s );
    91107                                Next( &n ) = &bef;
    92108                                Back( &n ) = Back( &bef );
     
    106122                        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;
     128                                        Back( Root( s ) ) = &n;
    113129                                        Next( Back( &n ) ) = &n;
    114130                                } else {
     
    133149                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
    134150#endif // __CFA_DEBUG__
    135                         if ( &n == head( s ) ) {
    136                                 if ( Next( head( s ) ) == head( s ) ) root = 0p;
    137                                 else root = Next( head(s ) );
     151                        if ( &n == Root( s ) ) {
     152                                if ( Next( Root( s ) ) == Root( s ) ) root = 0p;
     153                                else root = Next( Root(s ) );
    138154                        } // if
    139155                        Back( Next( &n ) ) = Back( &n );
     
    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.
     
    249270
    250271        inline {
     272                // wrappers to make ColIter have T
     273                T * Curr( SeqIterRev(T) & si ) with( si ) {
     274                        return (T *)curr;
     275                }
     276
    251277                void ?{}( SeqIterRev(T) & si ) with( si ) {     
    252278                        ((ColIter &) si){};
  • libcfa/src/bits/stack.hfa

    rcc5cc27 r63ec5fa  
    1414                } // post: empty() & head() == 0 | !empty() & head() in *this
    1515
     16                bool empty( Stack(T) & s ) with( s ) {                  // 0 <=> *this contains no elements
     17                        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
    1628                void ?{}( Stack(T) &, const Stack(T) & ) = void; // no copy
    1729                Stack(T) & ?=?( const Stack(T) & ) = void;              // no assignment
     
    2133                } // post: empty()
    2234
    23                 T & top( Stack(T) & s ) with( s ) {
    24                         return *head( s );
     35                T * top( Stack(T) & s ) with( s ) {
     36                        return head( s );
    2537                }
    2638
    27                 void addHead( Stack(T) & s, T & n ) with( s ) {
     39                void addHead( Stack(T) & s, T * n ) with( s ) {
    2840#ifdef __CFA_DEBUG__
    29                         if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
     41                        if ( listed( (Colable &)(*n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
    3042#endif // __CFA_DEBUG__
    31                         Next( &n ) = head( s ) ? head( s ) : &n;
    32                         root = &n;
     43                        Next( n ) = Root( s ) ? Root( s ) : n;
     44                        root = n;
    3345                }
    3446
    35                 void add( Stack(T) & s, T & n ) with( s ) {
     47                void add( Stack(T) & s, T * n ) with( s ) {
    3648                        addHead( s, n );
    3749                }
    3850
    39                 void push( Stack(T) & s, T & n ) with( s ) {
     51                void push( Stack(T) & s, T * n ) with( s ) {
    4052                        addHead( s, n );
    4153                }
    4254
    43                 T & drop( Stack(T) & s ) with( s ) {
    44                         T & t = *head( s );
     55                T * drop( Stack(T) & s ) with( s ) {
     56                        T * t = head( s );
    4557                        if ( root ) {
    4658                                root = ( T *)Next(root);
    47                                 if ( head( s ) == &t ) root = 0p;               // only one element ?
    48                                 Next( &t ) = 0p;
     59                                if ( Root( s ) == t ) root = 0p;                // only one element ?
     60                                Next( t ) = 0p;
    4961                        } // if
    5062                        return t;
    5163                }
    5264
    53                 T & pop( Stack(T) & s ) with( s ) {
     65                T * pop( Stack(T) & s ) with( s ) {
    5466                        return drop( s );
    5567                }
     
    6476
    6577        inline {
     78                // wrappers to make ColIter have T
     79                T * Curr( StackIter(T) & si ) with( si ) {
     80                        return (T *)curr;
     81                }
     82
    6683                void ?{}( StackIter(T) & si ) with( si ) {
    6784                        ((ColIter &)si){};
     
    7390                } // post: curr = {e in s}
    7491
    75                 void ?{}( StackIter(T) & si, T & start ) with( si ) {
    76                         curr = &start;
     92                void ?{}( StackIter(T) & si, T * start ) with( si ) {
     93                        curr = start;
    7794                } // post: curr = {e in s}
    7895
     
    86103                                &tp = Curr( si );
    87104                                T * n = Next( Curr( si ) );
    88                                 curr = n == Curr( si ) ? 0p : n;
     105                                curr = (n == Curr( si ) ) ? 0p : n;
    89106                        } else &tp = 0p;
    90107                        return &tp != 0p;
  • libcfa/src/bits/stack_example.cfa

    rcc5cc27 r63ec5fa  
    2727       
    2828        for ( i; 10 ) {
    29                 push( fred, *new( 2 * i ) );
     29                push( fred, new( 2 * i ) );
    3030        }
    3131
     
    3636       
    3737        for ( i; 9 ) {
    38                 delete( &pop( fred ) );
     38                delete( pop( fred ) );
    3939        }
    4040
     
    4545       
    4646        for ( i; 10 ) {
    47                 push( fred, *new( 2 * i + 1 ) );
     47                push( fred, new( 2 * i + 1 ) );
    4848        }
    4949        for ( over( fredIter, fred ); fredIter >> f; ) {
     
    7878       
    7979        for ( i; 10 ) {
    80                 push( mary, *new( 2 * i ) );
     80                push( mary, new( 2 * i ) );
    8181        }
    8282
     
    8787       
    8888        for ( i; 9 ) {
    89                 delete( &pop( mary ) );
     89                delete( pop( mary ) );
    9090        }
    9191
     
    9696       
    9797        for ( i; 10 ) {
    98                 push( mary, *new( 2 * i + 1 ) );
     98                push( mary, new( 2 * i + 1 ) );
    9999        }
    100100        for ( over( maryIter, mary ); maryIter >> m; ) {
Note: See TracChangeset for help on using the changeset viewer.