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

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 ddcedfe was 3d0560d, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

clean up all new collections and fix sequence iterator bug

  • Property mode set to 100644
File size: 2.6 KB
Line 
1#pragma once
2
3#include "collection.hfa"
4
5forall( dtype T ) {
6 struct Stack {
7 inline Collection; // Plan 9 inheritance
8 };
9
10 inline {
11 // wrappers to make Collection have T
12 T * head( Stack(T) & s ) with( s ) {
13 return (T *)head( (Collection &)s );
14 } // post: empty() & head() == 0 | !empty() & head() in *this
15
16 bool empty( Stack(T) & s ) with( s ) { // 0 <=> *this contains no elements
17 return empty( (Collection &)s );
18 }
19
20 T *& Next( T * n ) {
21 return (T *)Next( (Colable *)n );
22 }
23
24 T * Root( Stack(T) & s ) with( s ) {
25 return (T *)root;
26 }
27
28 void ?{}( Stack(T) &, const Stack(T) & ) = void; // no copy
29 Stack(T) & ?=?( const Stack(T) & ) = void; // no assignment
30
31 void ?{}( Stack(T) & s ) with( s ) {
32 ((Collection &)s){};
33 } // post: empty()
34
35 T * top( Stack(T) & s ) with( s ) {
36 return head( s );
37 }
38
39 void addHead( Stack(T) & s, T * n ) with( s ) {
40#ifdef __CFA_DEBUG__
41 if ( listed( (Colable &)(*n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
42#endif // __CFA_DEBUG__
43 Next( n ) = Root( s ) ? Root( s ) : n;
44 root = n;
45 }
46
47 void add( Stack(T) & s, T * n ) with( s ) {
48 addHead( s, n );
49 }
50
51 void push( Stack(T) & s, T * n ) with( s ) {
52 addHead( s, n );
53 }
54
55 T * drop( Stack(T) & s ) with( s ) {
56 T * t = head( s );
57 if ( root ) {
58 root = ( T *)Next(root);
59 if ( Root( s ) == t ) root = 0p; // only one element ?
60 Next( t ) = 0p;
61 } // if
62 return t;
63 }
64
65 T * pop( Stack(T) & s ) with( s ) {
66 return drop( s );
67 }
68 } // distribution
69} // distribution
70
71
72forall( dtype T ) {
73 struct StackIter {
74 inline ColIter; // Plan 9 inheritance
75 };
76
77 inline {
78 // wrappers to make ColIter have T
79 T * Curr( StackIter(T) & si ) with( si ) {
80 return (T *)curr;
81 }
82
83 void ?{}( StackIter(T) & si ) with( si ) {
84 ((ColIter &)si){};
85 } // post: curr == 0p
86
87 // create an iterator active in Stack s
88 void ?{}( StackIter(T) & si, Stack(T) & s ) with( si ) {
89 curr = head( s );
90 } // post: curr = {e in s}
91
92 void ?{}( StackIter(T) & si, T * start ) with( si ) {
93 curr = start;
94 } // post: curr = {e in s}
95
96 // make existing iterator active in Stack q
97 void over( StackIter(T) & si, Stack(T) & s ) with( si ) {
98 curr = head( s );
99 } // post: curr = {e in s}
100
101 bool ?>>?( StackIter(T) & si, T && tp ) with( si ) {
102 if ( curr ) {
103 &tp = Curr( si );
104 T * n = Next( Curr( si ) );
105 curr = (n == Curr( si ) ) ? 0p : n;
106 } else &tp = 0p;
107 return &tp != 0p;
108 }
109 } // distribution
110} // distribution
111
112// Local Variables: //
113// compile-command: "make install" //
114// End: //
Note: See TracBrowser for help on using the repository browser.