Changeset 636d3715


Ignore:
Timestamp:
Dec 3, 2020, 11:56:01 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
89c982c, a78c3ff, cc5cc27
Parents:
1db306a
Message:

more code sharing in containers

Location:
libcfa/src/bits
Files:
5 edited

Legend:

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

    r1db306a r636d3715  
    1313        // return true iff *this is an element of a collection
    1414        bool listed( Colable & co ) with( co ) {                        // pre: this != 0
    15                 return next != 0;
     15                return next != 0p;
    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
    2535} // distribution
     36
    2637
    2738struct Collection {
     
    4152                return root == 0p;
    4253        }
     54
    4355        void * head( Collection & collection ) with( collection ) {
    4456                return root;
     
    5567                curr = 0p;
    5668        } // post: elts = null
     69
     70        forall( dtype T ) {
     71                T * Curr( ColIter & ci ) with( ci ) {
     72                        return (T *)curr;
     73                }
     74        } // distribution
    5775} // distribution
  • libcfa/src/bits/queue.hfa

    r1db306a r636d3715  
    1414                        return (T *)head( (Collection &)q );
    1515                } // post: empty() & head() == 0 | !empty() & head() in *q
    16 
    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                 }
    3216
    3317                void ?{}( Queue(T) &, const Queue(T) & ) = void; // no copy
     
    5539#endif // __CFA_DEBUG__
    5640                        if ( last ) {
    57                                 Next( n ) = Root( q );
     41                                Next( n ) = head( q );
    5842                                q.root = n;
    5943                        } else {
     
    8165                        if ( root ) {
    8266                                root = Next( root );
    83                                 if ( Root( q ) == t ) {
     67                                if ( head( q ) == t ) {
    8468                                        root = last = 0p;                                       // only one element
    8569                                }
     
    132116                                root = from.root;
    133117                        } else {                                                                        // "to" list not empty
    134                                 Next( last ) = Root( from );
     118                                Next( last ) = head( from );
    135119                        }
    136120                        last = from.last;
     
    148132                        to.last = n;                                                            // end of "to" list
    149133                        from.root = Next( n );                                          // start of "from" list
    150                         if ( n == Root( from ) ) {                                      // last node in list ?
     134                        if ( n == head( from ) ) {                                      // last node in list ?
    151135                                from.root = from.last = 0p;                             // mark "from" list empty
    152136                        } else {
     
    164148
    165149        inline {
    166                 // wrappers to make ColIter have T
    167                 T * Curr( QueueIter(T) & qi ) with( qi ) {
    168                         return (T *)curr;
    169                 }
    170 
    171150                void ?{}( QueueIter(T) & qi ) with( qi ) {
    172151                        ((ColIter &)qi){};
  • libcfa/src/bits/sequence.hfa

    r1db306a r636d3715  
    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 
    4836                T *& Back( T * n ) {
    4937                        return (T *)Back( (Seqable *)n );
    50                 }
    51 
    52                 T * Root( Sequence(T) & s ) with( s ) {
    53                         return (T *)root;
    5438                }
    5539
     
    6347                // Return a pointer to the last sequence element, without removing it. 
    6448                T & tail( Sequence(T) & s ) with( s ) {
    65                         return root ? (T &)Back( Root( s ) ) : *0p;     // needs cast?
     49                        return root ? (T &)Back( head( s ) ) : *0p;     // needs cast?
    6650                }       // post: empty() & tail() == 0 | !empty() & tail() in *s
    6751
     
    7155                        if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n );
    7256#endif // __CFA_DEBUG__
    73                         return Next( n ) == Root( s ) ? 0p : Next( n );
     57                        return Next( n ) == head( s ) ? 0p : Next( n );
    7458                }       // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s
    7559
     
    7963                        if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n );
    8064#endif // __CFA_DEBUG__
    81                         return n == Root( s ) ? 0p : Back( n );
     65                        return n == head( s ) ? 0p : Back( n );
    8266                }       // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
    8367
     
    8872                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
    8973#endif // __CFA_DEBUG__
    90                         if ( &bef == Root( s ) ) {                                      // must change root
     74                        if ( &bef == head( s ) ) {                                      // must change root
    9175                                if ( root ) {
    92                                         Next( &n ) = Root( s );
    93                                         Back( &n ) = Back( Root( s ) );
     76                                        Next( &n ) = head( s );
     77                                        Back( &n ) = Back( head( s ) );
    9478                                        // inserted node must be consistent before it is seen
    9579                                        asm( "" : : : "memory" );                       // prevent code movement across barrier
    96                                         Back( Root( s ) ) = &n;
     80                                        Back( head( s ) ) = &n;
    9781                                        Next( Back( &n ) ) = &n;
    9882                                } else {
     
    10488                                root = &n;
    10589                        } else {
    106                                 if ( ! &bef ) &bef = Root( s );
     90                                if ( ! &bef ) &bef = head( s );
    10791                                Next( &n ) = &bef;
    10892                                Back( &n ) = Back( &bef );
     
    122106                        if ( ! &aft ) {                                                         // must change root
    123107                                if ( root ) {
    124                                         Next( &n ) = Root( s );
    125                                         Back( &n ) = Back( Root( s ) );
     108                                        Next( &n ) = head( s );
     109                                        Back( &n ) = Back( head( s ) );
    126110                                        // inserted node must be consistent before it is seen
    127111                                        asm( "" : : : "memory" );                       // prevent code movement across barrier
    128                                         Back( Root( s ) ) = &n;
     112                                        Back( head( s ) ) = &n;
    129113                                        Next( Back( &n ) ) = &n;
    130114                                } else {
     
    149133                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
    150134#endif // __CFA_DEBUG__
    151                         if ( &n == Root( s ) ) {
    152                                 if ( Next( Root( s ) ) == Root( s ) ) root = 0p;
    153                                 else root = Next( Root(s ) );
     135                        if ( &n == head( s ) ) {
     136                                if ( Next( head( s ) ) == head( s ) ) root = 0p;
     137                                else root = Next( head(s ) );
    154138                        } // if
    155139                        Back( Next( &n ) ) = Back( &n );
     
    191175                                root = from.root;
    192176                        } else {                                                                        // "to" list not empty
    193                                 T * toEnd = Back( Root( s ) );
    194                                 T * fromEnd = Back( Root( from ) );
     177                                T * toEnd = Back( head( s ) );
     178                                T * fromEnd = Back( head( from ) );
    195179                                Back( root ) = fromEnd;
    196                                 Next( fromEnd ) = Root( s );
     180                                Next( fromEnd ) = head( s );
    197181                                Back( from.root ) = toEnd;
    198                                 Next( toEnd ) = Root( from );
     182                                Next( toEnd ) = head( from );
    199183                        } // if
    200184                        from.root = 0p;                                                         // mark "from" list empty
     
    213197                                from.root = 0p;                                                 // mark "from" list empty
    214198                        } else {
    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;
     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;
    219203                        } // if
    220204                        transfer( s, to );
     
    231215
    232216        inline {
    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){};
     217                void ?{}( SeqIter(T) & si ) with( si ) {
     218                        ((ColIter &)si){};
    240219                        seq = 0p;
    241220                } // post: elts = null.
     
    270249
    271250        inline {
    272                 // wrappers to make ColIter have T
    273                 T * Curr( SeqIterRev(T) & si ) with( si ) {
    274                         return (T *)curr;
    275                 }
    276 
    277251                void ?{}( SeqIterRev(T) & si ) with( si ) {     
    278252                        ((ColIter &) si){};
  • libcfa/src/bits/stack.hfa

    r1db306a r636d3715  
    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 
    2816                void ?{}( Stack(T) &, const Stack(T) & ) = void; // no copy
    2917                Stack(T) & ?=?( const Stack(T) & ) = void;              // no assignment
     
    3321                } // post: empty()
    3422
    35                 T * top( Stack(T) & s ) with( s ) {
    36                         return head( s );
     23                T & top( Stack(T) & s ) with( s ) {
     24                        return *head( s );
    3725                }
    3826
    39                 void addHead( Stack(T) & s, T * n ) with( s ) {
     27                void addHead( Stack(T) & s, T & n ) with( s ) {
    4028#ifdef __CFA_DEBUG__
    41                         if ( listed( (Colable &)(*n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
     29                        if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
    4230#endif // __CFA_DEBUG__
    43                         Next( n ) = Root( s ) ? Root( s ) : n;
    44                         root = n;
     31                        Next( &n ) = head( s ) ? head( s ) : &n;
     32                        root = &n;
    4533                }
    4634
    47                 void add( Stack(T) & s, T * n ) with( s ) {
     35                void add( Stack(T) & s, T & n ) with( s ) {
    4836                        addHead( s, n );
    4937                }
    5038
    51                 void push( Stack(T) & s, T * n ) with( s ) {
     39                void push( Stack(T) & s, T & n ) with( s ) {
    5240                        addHead( s, n );
    5341                }
    5442
    55                 T * drop( Stack(T) & s ) with( s ) {
    56                         T * t = head( s );
     43                T & drop( Stack(T) & s ) with( s ) {
     44                        T & t = *head( s );
    5745                        if ( root ) {
    5846                                root = ( T *)Next(root);
    59                                 if ( Root( s ) == t ) root = 0p;                // only one element ?
    60                                 Next( t ) = 0p;
     47                                if ( head( s ) == &t ) root = 0p;               // only one element ?
     48                                Next( &t ) = 0p;
    6149                        } // if
    6250                        return t;
    6351                }
    6452
    65                 T * pop( Stack(T) & s ) with( s ) {
     53                T & pop( Stack(T) & s ) with( s ) {
    6654                        return drop( s );
    6755                }
     
    7664
    7765        inline {
    78                 // wrappers to make ColIter have T
    79                 T * Curr( StackIter(T) & si ) with( si ) {
    80                         return (T *)curr;
    81                 }
    82 
    8366                void ?{}( StackIter(T) & si ) with( si ) {
    8467                        ((ColIter &)si){};
     
    9073                } // post: curr = {e in s}
    9174
    92                 void ?{}( StackIter(T) & si, T * start ) with( si ) {
    93                         curr = start;
     75                void ?{}( StackIter(T) & si, T & start ) with( si ) {
     76                        curr = &start;
    9477                } // post: curr = {e in s}
    9578
     
    10386                                &tp = Curr( si );
    10487                                T * n = Next( Curr( si ) );
    105                                 curr = (n == Curr( si ) ) ? 0p : n;
     88                                curr = n == Curr( si ) ? 0p : n;
    10689                        } else &tp = 0p;
    10790                        return &tp != 0p;
  • libcfa/src/bits/stack_example.cfa

    r1db306a r636d3715  
    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.