Ignore:
File:
1 edited

Legend:

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

    r7d4ce2a r9536761  
    33#include "bits/collection.hfa"
    44
    5 forall( dtype T ) {
     5// A Queue(T) is a Collection(T) defining the ordering that nodes are returned by drop() in the same order from those
     6// added by add(). T must be a public descendant of uColable.
     7
     8// The implementation is a typical singly-linked list, except the next field of the last element points to itself
     9// instead of being null.
     10
     11forall( dtype T | { T *& Next ( T * ); } ) {
    612        struct Queue {
    713                inline Collection;                                                              // Plan 9 inheritance
     
    3440                } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *q
    3541
    36                 void addHead( Queue(T) & q, T & n ) with( q ) {
     42                T & addHead( Queue(T) & q, T & n ) with( q ) {
    3743                        #ifdef __CFA_DEBUG__
    3844                        if ( listed( &n ) ) abort( "(Queue &)%p.addHead( %p ) : Node is already on another list.", &q, &n );
     
    4551                                Next( &n ) = &n;                                                // last node points to itself
    4652                        }
     53                        return n;
    4754                }
    4855
    49                 void addTail( Queue(T) & q, T & n ) with( q ) {
     56                T & addTail( Queue(T) & q, T & n ) with( q ) {
    5057                        #ifdef __CFA_DEBUG__
    5158                        if ( listed( &n ) ) abort( "(Queue &)%p.addTail( %p ) : Node is already on another list.", &q, &n );
     
    5562                        last = &n;
    5663                        Next( &n ) = &n;                                                        // last node points to itself
     64                        return n;
    5765                }
    5866
    59                 void add( Queue(T) & q, T & n ) with( q ) {
    60                         addTail( q, n );
     67                T & add( Queue(T) & q, T & n ) with( q ) {
     68                        return addTail( q, n );
    6169                }
    6270
     
    6472                        T & t = head( q );
    6573                        if ( root ) {
    66                                 root = Next( root );
     74                                root = Next( (T *)root );
    6775                                if ( &head( q ) == &t ) {
    6876                                        root = last = 0p;                                       // only one element
     
    7785                }
    7886
    79                 void remove( Queue(T) & q, T & n ) with( q ) {  // O(n)
     87                T & remove( Queue(T) & q, T & n ) with( q ) {   // O(n)
    8088                        #ifdef __CFA_DEBUG__
    8189                        if ( ! listed( (Colable &)n ) ) abort( "(Queue &)%p.remove( %p ) : Node is not on a list.", &q, &n );
     
    103111                                curr = Next( curr );
    104112                        }
     113                        return n;
    105114                } // post: ! listed( n )
    106115
    107                 T & dropTail( Queue(T) & q ) with( q ) { // O(n)
     116                T & dropTail( Queue(T) & q ) with( q ) {                // O(n)
    108117                        T & n = tail( q );
    109118                        return &n ? remove( q, n ), n : *0p;
     
    142151} // distribution
    143152
    144 forall( dtype T ) {
     153forall( dtype T | { T *& Next ( T * ); } ) {
    145154        struct QueueIter {
    146155                inline ColIter;                                                                 // Plan 9 inheritance
     
    152161                } // post: curr == 0p
    153162
    154                 // create an iterator active in Queue q
     163                // create an iterator active in queue q
    155164                void ?{}( QueueIter(T) & qi, Queue(T) & q ) with( qi ) {
    156165                        curr = &head( q );
     
    161170                } // post: curr = {e in q}
    162171
    163                 // make existing iterator active in Queue q
     172                // make existing iterator active in queue q
    164173                void over( QueueIter(T) & qi, Queue(T) & q ) with( qi ) {
    165174                        curr = &head( q );
    166175                } // post: curr = {e in q}
    167176
    168                 bool ?>>?( QueueIter(T) & qi, T && tp ) with( qi ) {
     177                bool ?|?( QueueIter(T) & qi, T && tp ) with( qi ) {
    169178                        if ( curr ) {
    170179                                &tp = Curr( qi );
     
    174183                        return &tp != 0p;
    175184                }
    176                 // post: elts == null & !operator>>(tp) | elts != null & *tp' in elts & elts' == elts - *tp & operator>>(tp)
     185                // post: elts == null & !operator|(tp) | elts != null & *tp' in elts & elts' == elts - *tp & operator|(tp)
    177186        } // distribution
    178187} // distribution
Note: See TracChangeset for help on using the changeset viewer.