Changes in libcfa/src/bits/stack.hfa [7d4ce2a:9536761]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/stack.hfa
r7d4ce2a r9536761 3 3 #include "bits/collection.hfa" 4 4 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 11 forall( dtype T | { T *& Next ( T * ); } ) { 6 12 struct Stack { 7 13 inline Collection; // Plan 9 inheritance … … 25 31 } 26 32 27 voidaddHead( Stack(T) & s, T & n ) with( s ) {33 T & addHead( Stack(T) & s, T & n ) with( s ) { 28 34 #ifdef __CFA_DEBUG__ 29 35 if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n ); … … 31 37 Next( &n ) = &head( s ) ? &head( s ) : &n; 32 38 root = &n; 39 return n; 33 40 } 34 41 35 voidadd( 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 ); 37 44 } 38 45 39 voidpush( 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 ); 41 48 } 42 49 … … 44 51 T & t = head( s ); 45 52 if ( root ) { 46 root = ( T *)Next( root );53 root = ( T *)Next( (T *)root ); 47 54 if ( &head( s ) == &t ) root = 0p; // only one element ? 48 55 Next( &t ) = 0p; … … 57 64 } // distribution 58 65 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(). 59 68 60 forall( dtype T ) {69 forall( dtype T | { T *& Next ( T * ); } ) { 61 70 struct StackIter { 62 71 inline ColIter; // Plan 9 inheritance … … 68 77 } // post: curr == 0p 69 78 70 // create an iterator active in Stack s79 // create an iterator active in stack s 71 80 void ?{}( StackIter(T) & si, Stack(T) & s ) with( si ) { 72 81 curr = &head( s ); … … 77 86 } // post: curr = {e in s} 78 87 79 // make existing iterator active in Stack q88 // make existing iterator active in stack s 80 89 void over( StackIter(T) & si, Stack(T) & s ) with( si ) { 81 90 curr = &head( s ); 82 91 } // post: curr = {e in s} 83 92 84 bool ? >>?( StackIter(T) & si, T && tp ) with( si ) {93 bool ?|?( StackIter(T) & si, T && tp ) with( si ) { 85 94 if ( curr ) { 86 95 &tp = Curr( si );
Note: See TracChangeset
for help on using the changeset viewer.