[5e82d56] | 1 | #pragma once
|
---|
| 2 |
|
---|
[7d4ce2a] | 3 | #include "bits/collection.hfa"
|
---|
[5e82d56] | 4 |
|
---|
[19de7864] | 5 | forall( dtype T | { T *& Next ( T * ); } ) {
|
---|
[5e82d56] | 6 | struct Stack {
|
---|
| 7 | inline Collection; // Plan 9 inheritance
|
---|
| 8 | };
|
---|
| 9 |
|
---|
| 10 | inline {
|
---|
| 11 | // wrappers to make Collection have T
|
---|
[a5a67ab8] | 12 | T & head( Stack(T) & s ) with( s ) {
|
---|
| 13 | return *(T *)head( (Collection &)s );
|
---|
[5e82d56] | 14 | } // post: empty() & head() == 0 | !empty() & head() in *this
|
---|
| 15 |
|
---|
| 16 | void ?{}( Stack(T) &, const Stack(T) & ) = void; // no copy
|
---|
| 17 | Stack(T) & ?=?( const Stack(T) & ) = void; // no assignment
|
---|
| 18 |
|
---|
| 19 | void ?{}( Stack(T) & s ) with( s ) {
|
---|
| 20 | ((Collection &)s){};
|
---|
| 21 | } // post: empty()
|
---|
| 22 |
|
---|
[636d3715] | 23 | T & top( Stack(T) & s ) with( s ) {
|
---|
[a5a67ab8] | 24 | return head( s );
|
---|
[5e82d56] | 25 | }
|
---|
| 26 |
|
---|
[636d3715] | 27 | void addHead( Stack(T) & s, T & n ) with( s ) {
|
---|
[7c1144b] | 28 | #ifdef __CFA_DEBUG__
|
---|
[636d3715] | 29 | if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
|
---|
[7c1144b] | 30 | #endif // __CFA_DEBUG__
|
---|
[58870e6b] | 31 | Next( &n ) = &head( s ) ? &head( s ) : &n;
|
---|
[636d3715] | 32 | root = &n;
|
---|
[5e82d56] | 33 | }
|
---|
| 34 |
|
---|
[636d3715] | 35 | void add( Stack(T) & s, T & n ) with( s ) {
|
---|
[5e82d56] | 36 | addHead( s, n );
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[636d3715] | 39 | void push( Stack(T) & s, T & n ) with( s ) {
|
---|
[5e82d56] | 40 | addHead( s, n );
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[636d3715] | 43 | T & drop( Stack(T) & s ) with( s ) {
|
---|
[a5a67ab8] | 44 | T & t = head( s );
|
---|
[5e82d56] | 45 | if ( root ) {
|
---|
[accc5dbb] | 46 | root = ( T *)Next( (T *)root );
|
---|
[a5a67ab8] | 47 | if ( &head( s ) == &t ) root = 0p; // only one element ?
|
---|
[58870e6b] | 48 | Next( &t ) = 0p;
|
---|
[5e82d56] | 49 | } // if
|
---|
| 50 | return t;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[636d3715] | 53 | T & pop( Stack(T) & s ) with( s ) {
|
---|
[5e82d56] | 54 | return drop( s );
|
---|
| 55 | }
|
---|
| 56 | } // distribution
|
---|
| 57 | } // distribution
|
---|
| 58 |
|
---|
| 59 |
|
---|
[19de7864] | 60 | forall( dtype T | { T *& Next ( T * ); } ) {
|
---|
[5e82d56] | 61 | struct StackIter {
|
---|
| 62 | inline ColIter; // Plan 9 inheritance
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | inline {
|
---|
| 66 | void ?{}( StackIter(T) & si ) with( si ) {
|
---|
| 67 | ((ColIter &)si){};
|
---|
| 68 | } // post: curr == 0p
|
---|
| 69 |
|
---|
| 70 | // create an iterator active in Stack s
|
---|
| 71 | void ?{}( StackIter(T) & si, Stack(T) & s ) with( si ) {
|
---|
[a5a67ab8] | 72 | curr = &head( s );
|
---|
[5e82d56] | 73 | } // post: curr = {e in s}
|
---|
| 74 |
|
---|
[636d3715] | 75 | void ?{}( StackIter(T) & si, T & start ) with( si ) {
|
---|
| 76 | curr = &start;
|
---|
[5e82d56] | 77 | } // post: curr = {e in s}
|
---|
| 78 |
|
---|
| 79 | // make existing iterator active in Stack q
|
---|
| 80 | void over( StackIter(T) & si, Stack(T) & s ) with( si ) {
|
---|
[a5a67ab8] | 81 | curr = &head( s );
|
---|
[5e82d56] | 82 | } // post: curr = {e in s}
|
---|
| 83 |
|
---|
[3d0560d] | 84 | bool ?>>?( StackIter(T) & si, T && tp ) with( si ) {
|
---|
[5e82d56] | 85 | if ( curr ) {
|
---|
[3d0560d] | 86 | &tp = Curr( si );
|
---|
[58870e6b] | 87 | T * n = Next( Curr( si ) );
|
---|
[636d3715] | 88 | curr = n == Curr( si ) ? 0p : n;
|
---|
[3d0560d] | 89 | } else &tp = 0p;
|
---|
| 90 | return &tp != 0p;
|
---|
[5e82d56] | 91 | }
|
---|
| 92 | } // distribution
|
---|
| 93 | } // distribution
|
---|