Changeset a3a76ea


Ignore:
Timestamp:
Dec 27, 2020, 5:55:50 PM (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:
62a7cc0
Parents:
83c7e3c
Message:

modify routines to return added/removed node to allow cascading calls

Location:
libcfa/src/bits
Files:
3 edited

Legend:

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

    r83c7e3c ra3a76ea  
    3434                } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *q
    3535
    36                 void addHead( Queue(T) & q, T & n ) with( q ) {
     36                T & addHead( Queue(T) & q, T & n ) with( q ) {
    3737                        #ifdef __CFA_DEBUG__
    3838                        if ( listed( &n ) ) abort( "(Queue &)%p.addHead( %p ) : Node is already on another list.", &q, &n );
     
    4545                                Next( &n ) = &n;                                                // last node points to itself
    4646                        }
     47                        return n;
    4748                }
    4849
    49                 void addTail( Queue(T) & q, T & n ) with( q ) {
     50                T & addTail( Queue(T) & q, T & n ) with( q ) {
    5051                        #ifdef __CFA_DEBUG__
    5152                        if ( listed( &n ) ) abort( "(Queue &)%p.addTail( %p ) : Node is already on another list.", &q, &n );
     
    5556                        last = &n;
    5657                        Next( &n ) = &n;                                                        // last node points to itself
     58                        return n;
    5759                }
    5860
    59                 void add( Queue(T) & q, T & n ) with( q ) {
    60                         addTail( q, n );
     61                T & add( Queue(T) & q, T & n ) with( q ) {
     62                        return addTail( q, n );
    6163                }
    6264
     
    7779                }
    7880
    79                 void remove( Queue(T) & q, T & n ) with( q ) {  // O(n)
     81                T & remove( Queue(T) & q, T & n ) with( q ) {   // O(n)
    8082                        #ifdef __CFA_DEBUG__
    8183                        if ( ! listed( (Colable &)n ) ) abort( "(Queue &)%p.remove( %p ) : Node is not on a list.", &q, &n );
     
    103105                                curr = Next( curr );
    104106                        }
     107                        return n;
    105108                } // post: ! listed( n )
    106109
  • libcfa/src/bits/sequence.hfa

    r83c7e3c ra3a76ea  
    7777
    7878                // Insert *n into the sequence before *bef, or at the end if bef == 0.
    79                 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
     79                T & insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
    8080                        #ifdef __CFA_DEBUG__
    8181                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
     
    105105                                Next( Back( &n ) ) = &n;
    106106                        } // if
     107                        return n;
    107108                }       // post: n->listed() & *n in *s & succ(n) == bef
    108109
    109110
    110111                // Insert *n into the sequence after *aft, or at the beginning if aft == 0.
    111                 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) {   // pre: !n->listed() & *aft in *s
     112                T & insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) {    // pre: !n->listed() & *aft in *s
    112113                        #ifdef __CFA_DEBUG__
    113114                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n );
     
    135136                                Next( &aft ) = &n;
    136137                        } // if
     138                        return n;
    137139                }         // post: n->listed() & *n in *s & succ(n) == bef
    138140               
    139141                // pre: n->listed() & *n in *s
    140                 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
     142                T & remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
    141143                        #ifdef __CFA_DEBUG__
    142144                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
     
    149151                        Next( Back( &n ) ) = Next( &n );
    150152                        Next( &n ) = Back( &n ) = 0p;
     153                        return n;
    151154                }                                                       // post: !n->listed().
    152155
    153156                // Add an element to the head of the sequence.
    154                 void addHead( Sequence(T) & s, T & n ) {                // pre: !n->listed(); post: n->listed() & head() == n
    155                         insertAft( s, *0p, n );
     157                T & addHead( Sequence(T) & s, T & n ) {                 // pre: !n->listed(); post: n->listed() & head() == n
     158                        return insertAft( s, *0p, n );
    156159                }
    157160                // Add an element to the tail of the sequence.
    158                 void addTail( Sequence(T) & s, T & n ) {                // pre: !n->listed(); post: n->listed() & head() == n
    159                         insertBef( s, n, *0p );
     161                T & addTail( Sequence(T) & s, T & n ) {                 // pre: !n->listed(); post: n->listed() & head() == n
     162                        return insertBef( s, n, *0p );
    160163                }
    161164                // Add an element to the tail of the sequence.
    162                 void add( Sequence(T) & s, T & n ) {                    // pre: !n->listed(); post: n->listed() & head() == n
    163                         addTail( s, n );
     165                T & add( Sequence(T) & s, T & n ) {                             // pre: !n->listed(); post: n->listed() & head() == n
     166                        return addTail( s, n );
    164167                }
    165168                // Remove and return the head element in the sequence.
  • libcfa/src/bits/stack.hfa

    r83c7e3c ra3a76ea  
    2525                }
    2626
    27                 void addHead( Stack(T) & s, T & n ) with( s ) {
     27                T & addHead( Stack(T) & s, T & n ) with( s ) {
    2828                        #ifdef __CFA_DEBUG__
    2929                        if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
     
    3131                        Next( &n ) = &head( s ) ? &head( s ) : &n;
    3232                        root = &n;
     33                        return n;
    3334                }
    3435
    35                 void add( Stack(T) & s, T & n ) with( s ) {
    36                         addHead( s, n );
     36                T & add( Stack(T) & s, T & n ) with( s ) {
     37                        return addHead( s, n );
    3738                }
    3839
    39                 void push( Stack(T) & s, T & n ) with( s ) {
    40                         addHead( s, n );
     40                T & push( Stack(T) & s, T & n ) with( s ) {
     41                        return addHead( s, n );
    4142                }
    4243
Note: See TracChangeset for help on using the changeset viewer.