source: libcfa/src/bits/stack.hfa@ b6460bf

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since b6460bf was a3a76ea, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

modify routines to return added/removed node to allow cascading calls

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[5e82d56]1#pragma once
2
[7d4ce2a]3#include "bits/collection.hfa"
[5e82d56]4
[19de7864]5forall( 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
[a3a76ea]27 T & 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;
[a3a76ea]33 return n;
[5e82d56]34 }
35
[a3a76ea]36 T & add( Stack(T) & s, T & n ) with( s ) {
37 return addHead( s, n );
[5e82d56]38 }
39
[a3a76ea]40 T & push( Stack(T) & s, T & n ) with( s ) {
41 return addHead( s, n );
[5e82d56]42 }
43
[636d3715]44 T & drop( Stack(T) & s ) with( s ) {
[a5a67ab8]45 T & t = head( s );
[5e82d56]46 if ( root ) {
[accc5dbb]47 root = ( T *)Next( (T *)root );
[a5a67ab8]48 if ( &head( s ) == &t ) root = 0p; // only one element ?
[58870e6b]49 Next( &t ) = 0p;
[5e82d56]50 } // if
51 return t;
52 }
53
[636d3715]54 T & pop( Stack(T) & s ) with( s ) {
[5e82d56]55 return drop( s );
56 }
57 } // distribution
58} // distribution
59
60
[19de7864]61forall( dtype T | { T *& Next ( T * ); } ) {
[5e82d56]62 struct StackIter {
63 inline ColIter; // Plan 9 inheritance
64 };
65
66 inline {
67 void ?{}( StackIter(T) & si ) with( si ) {
68 ((ColIter &)si){};
69 } // post: curr == 0p
70
71 // create an iterator active in Stack s
72 void ?{}( StackIter(T) & si, Stack(T) & s ) with( si ) {
[a5a67ab8]73 curr = &head( s );
[5e82d56]74 } // post: curr = {e in s}
75
[636d3715]76 void ?{}( StackIter(T) & si, T & start ) with( si ) {
77 curr = &start;
[5e82d56]78 } // post: curr = {e in s}
79
80 // make existing iterator active in Stack q
81 void over( StackIter(T) & si, Stack(T) & s ) with( si ) {
[a5a67ab8]82 curr = &head( s );
[5e82d56]83 } // post: curr = {e in s}
84
[3d0560d]85 bool ?>>?( StackIter(T) & si, T && tp ) with( si ) {
[5e82d56]86 if ( curr ) {
[3d0560d]87 &tp = Curr( si );
[58870e6b]88 T * n = Next( Curr( si ) );
[636d3715]89 curr = n == Curr( si ) ? 0p : n;
[3d0560d]90 } else &tp = 0p;
91 return &tp != 0p;
[5e82d56]92 }
93 } // distribution
94} // distribution
Note: See TracBrowser for help on using the repository browser.