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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.
Note: See TracChangeset for help on using the changeset viewer.