Ignore:
File:
1 edited

Legend:

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

    r7d4ce2a r9536761  
    33#include "bits/collection.hfa"
    44
    5 forall( dtype T ) {
     5// A Stack(T) is a Collection(T) defining the ordering that nodes are returned by drop() in the reverse 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 Stack {
    713                inline Collection;                                                              // Plan 9 inheritance
     
    2531                }
    2632
    27                 void addHead( Stack(T) & s, T & n ) with( s ) {
     33                T & addHead( Stack(T) & s, T & n ) with( s ) {
    2834                        #ifdef __CFA_DEBUG__
    2935                        if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
     
    3137                        Next( &n ) = &head( s ) ? &head( s ) : &n;
    3238                        root = &n;
     39                        return n;
    3340                }
    3441
    35                 void add( Stack(T) & s, T & n ) with( s ) {
    36                         addHead( s, n );
     42                T & add( Stack(T) & s, T & n ) with( s ) {
     43                        return addHead( s, n );
    3744                }
    3845
    39                 void push( Stack(T) & s, T & n ) with( s ) {
    40                         addHead( s, n );
     46                T & push( Stack(T) & s, T & n ) with( s ) {
     47                        return addHead( s, n );
    4148                }
    4249
     
    4451                        T & t = head( s );
    4552                        if ( root ) {
    46                                 root = ( T *)Next( root );
     53                                root = ( T *)Next( (T *)root );
    4754                                if ( &head( s ) == &t ) root = 0p;              // only one element ?
    4855                                Next( &t ) = 0p;
     
    5764} // distribution
    5865
     66// A StackIter(T) is a subclass of ColIter(T) that generates the elements of a Stack(T).  It returns the elements in the
     67// order returned by drop().
    5968
    60 forall( dtype T ) {
     69forall( dtype T | { T *& Next ( T * ); } ) {
    6170        struct StackIter {
    6271                inline ColIter;                                                                 // Plan 9 inheritance
     
    6877                } // post: curr == 0p
    6978
    70                 // create an iterator active in Stack s
     79                // create an iterator active in stack s
    7180                void ?{}( StackIter(T) & si, Stack(T) & s ) with( si ) {
    7281                        curr = &head( s );
     
    7786                } // post: curr = {e in s}
    7887
    79                 // make existing iterator active in Stack q
     88                // make existing iterator active in stack s
    8089                void over( StackIter(T) & si, Stack(T) & s ) with( si ) {
    8190                        curr = &head( s );
    8291                } // post: curr = {e in s}
    8392
    84                 bool ?>>?( StackIter(T) & si, T && tp ) with( si ) {
     93                bool ?|?( StackIter(T) & si, T && tp ) with( si ) {
    8594                        if ( curr ) {
    8695                                &tp = Curr( si );
Note: See TracChangeset for help on using the changeset viewer.