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