source: libcfa/src/bits/sequence.hfa@ e43aa14

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 e43aa14 was e43aa14, checked in by Colby Alexander Parsons <caparsons@…>, 5 years ago

refactored collection and sequence to work with structs that don't have seqable/colable as their first field

  • Property mode set to 100644
File size: 10.0 KB
Line 
1#pragma once
2
3#include "bits/collection.hfa"
4#include "bits/defs.hfa"
5
6struct Seqable {
7 __cfa_anonymous_object(Colable);
8 struct Seqable * back; // pointer to previous node in the list
9};
10
11#ifdef __cforall
12static inline {
13 // PUBLIC
14
15 void ?{}( Seqable & sq ) with( sq ) {
16 ((Colable &)sq){};
17 back = 0p;
18 } // post: ! listed()
19
20 Seqable & getBack( Seqable & sq ) with( sq ) {
21 return *back;
22 }
23
24 // PRIVATE
25
26 Seqable *& Back( Seqable * sq ) {
27 return sq->back;
28 }
29
30 // // wrappers to make Collection have T
31 // forall( dtype T ) {
32 // T *& Back( T * n ) {
33 // return (T *)Back( (Seqable *)n );
34 // }
35 // } // distribution
36} // distribution
37
38forall( dtype T | { T *& Back ( T * ); T *& Next ( T * ); bool listed ( T * ); } ) {
39 struct Sequence {
40 inline Collection; // Plan 9 inheritance
41 };
42
43 static inline {
44 // wrappers to make Collection have T
45 T & head( Sequence(T) & s ) with( s ) {
46 return *(T *)head( (Collection &)s );
47 } // post: empty() & head() == 0 | !empty() & head() in *s
48
49 void ?{}( Sequence(T) &, const Sequence(T) & ) = void; // no copy
50 Sequence(T) & ?=?( const Sequence(T) & ) = void; // no assignment
51
52 void ?{}( Sequence(T) & s ) with( s ) {
53 ((Collection &)s){};
54 } // post: isEmpty().
55
56 // Return a pointer to the last sequence element, without removing it.
57 T & tail( Sequence(T) & s ) with( s ) {
58 return root ? (T &)*Back( &head( s ) ) : *0p;
59 } // post: empty() & tail() == 0 | !empty() & tail() in *s
60
61 // Return a pointer to the element after *n, or 0p if list empty.
62 T * succ( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s
63 #ifdef __CFA_DEBUG__
64 if ( ! listed( n ) ) abort( "(Sequence &)%p.succ( %p ) : Node is not on a list.", &s, n );
65 #endif // __CFA_DEBUG__
66 return Next( n ) == &head( s ) ? 0p : Next( n );
67 } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s
68
69 // Return a pointer to the element before *n, or 0p if there isn't one.
70 T * pred( Sequence(T) & s, T * n ) with( s ) { // pre: *n in *s
71 #ifdef __CFA_DEBUG__
72 if ( ! listed( n ) ) abort( "(Sequence &)%p.pred( %p ) : Node is not on a list.", &s, n );
73 #endif // __CFA_DEBUG__
74 return n == &head( s ) ? 0p : Back( n );
75 } // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
76
77
78 // Insert *n into the sequence before *bef, or at the end if bef == 0.
79 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
80 #ifdef __CFA_DEBUG__
81 if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
82 #endif // __CFA_DEBUG__
83 if ( &bef == &head( s ) ) { // must change root
84 if ( root ) {
85 Next( &n ) = &head( s );
86 Back( &n ) = Back( &head( s ) );
87 // inserted node must be consistent before it is seen
88 asm( "" : : : "memory" ); // prevent code movement across barrier
89 Back( &head( s ) ) = &n;
90 Next( Back( &n ) ) = &n;
91 } else {
92 Next( &n ) = &n;
93 Back( &n ) = &n;
94 } // if
95 // inserted node must be consistent before it is seen
96 asm( "" : : : "memory" ); // prevent code movement across barrier
97 root = &n;
98 } else {
99 if ( ! &bef ) &bef = &head( s );
100 Next( &n ) = &bef;
101 Back( &n ) = Back( &bef );
102 // inserted node must be consistent before it is seen
103 asm( "" : : : "memory" ); // prevent code movement across barrier
104 Back( &bef ) = &n;
105 Next( Back( &n ) ) = &n;
106 } // if
107 } // post: n->listed() & *n in *s & succ(n) == bef
108
109
110 // Insert *n into the sequence after *aft, or at the beginning if aft == 0.
111 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) { // pre: !n->listed() & *aft in *s
112 #ifdef __CFA_DEBUG__
113 if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n );
114 #endif // __CFA_DEBUG__
115 if ( ! &aft ) { // must change root
116 if ( root ) {
117 Next( &n ) = &head( s );
118 Back( &n ) = Back( &head( s ) );
119 // inserted node must be consistent before it is seen
120 asm( "" : : : "memory" ); // prevent code movement across barrier
121 Back( &head( s ) ) = &n;
122 Next( Back( &n ) ) = &n;
123 } else {
124 Next( &n ) = &n;
125 Back( &n ) = &n;
126 } // if
127 asm( "" : : : "memory" ); // prevent code movement across barrier
128 root = &n;
129 } else {
130 Next( &n ) = Next( &aft );
131 Back( &n ) = &aft;
132 // inserted node must be consistent before it is seen
133 asm( "" : : : "memory" ); // prevent code movement across barrier
134 Back( Next( &n ) ) = &n;
135 Next( &aft ) = &n;
136 } // if
137 } // post: n->listed() & *n in *s & succ(n) == bef
138
139 // pre: n->listed() & *n in *s
140 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
141 #ifdef __CFA_DEBUG__
142 if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
143 #endif // __CFA_DEBUG__
144 if ( &n == &head( s ) ) {
145 if ( Next( &head( s ) ) == &head( s ) ) root = 0p;
146 else root = Next( &head( s ) );
147 } // if
148 Back( Next( &n ) ) = Back( &n );
149 Next( Back( &n ) ) = Next( &n );
150 Next( &n ) = Back( &n ) = 0p;
151 } // post: !n->listed().
152
153 // Add an element to the head of the sequence.
154 void addHead( Sequence(T) & s, T & n ) { // pre: !n->listed(); post: n->listed() & head() == n
155 insertAft( s, *0p, n );
156 }
157 // Add an element to the tail of the sequence.
158 void addTail( Sequence(T) & s, T & n ) { // pre: !n->listed(); post: n->listed() & head() == n
159 insertBef( s, n, *0p );
160 }
161 // Add an element to the tail of the sequence.
162 void add( Sequence(T) & s, T & n ) { // pre: !n->listed(); post: n->listed() & head() == n
163 addTail( s, n );
164 }
165 // Remove and return the head element in the sequence.
166 T & dropHead( Sequence(T) & s ) {
167 T & n = head( s );
168 return &n ? remove( s, n ), n : *0p;
169 }
170 // Remove and return the head element in the sequence.
171 T & drop( Sequence(T) & s ) {
172 return dropHead( s );
173 }
174 // Remove and return the tail element in the sequence.
175 T & dropTail( Sequence(T) & s ) {
176 T & n = tail( s );
177 return &n ? remove( s, n ), n : *0p;
178 }
179
180 // Transfer the "from" list to the end of s sequence; the "from" list is empty after the transfer.
181 void transfer( Sequence(T) & s, Sequence(T) & from ) with( s ) {
182 if ( empty( from ) ) return; // "from" list empty ?
183 if ( empty( s ) ) { // "to" list empty ?
184 root = from.root;
185 } else { // "to" list not empty
186 T * toEnd = Back( &head( s ) );
187 T * fromEnd = Back( &head( from ) );
188 Back( (T *)root ) = fromEnd;
189 Next( fromEnd ) = &head( s );
190 Back( (T *)from.root ) = toEnd;
191 Next( toEnd ) = &head( from );
192 } // if
193 from.root = 0p; // mark "from" list empty
194 }
195
196 // Transfer the "from" list up to node "n" to the end of s list; the "from" list becomes the sequence after node "n".
197 // Node "n" must be in the "from" list.
198 void split( Sequence(T) & s, Sequence(T) & from, T & n ) with( s ) {
199 #ifdef __CFA_DEBUG__
200 if ( ! listed( &n ) ) abort( "(Sequence &)%p.split( %p ) : Node is not on a list.", &s, &n );
201 #endif // __CFA_DEBUG__
202 Sequence(T) to;
203 to.root = from.root; // start of "to" list
204 from.root = Next( &n ); // start of "from" list
205 if ( to.root == from.root ) { // last node in list ?
206 from.root = 0p; // mark "from" list empty
207 } else {
208 Back( &head( from ) ) = Back( &head( to ) ); // fix "from" list
209 Next( Back( &head( to ) ) ) = &head( from );
210 Next( &n ) = &head( to ); // fix "to" list
211 Back( &head( to ) ) = &n;
212 } // if
213 transfer( s, to );
214 }
215 } // distribution
216} // distribution
217
218// forall( dtype T ) {
219// // SeqIter(T) is used to iterate over a Sequence(T) in head-to-tail order.
220// struct SeqIter {
221// inline ColIter;
222// // The Sequence must be passed to pred and succ to check for the end of the Sequence and return 0p. Without
223// // passing the sequence, traversing would require its length. Thus the iterator needs a pointer to the sequence
224// // to pass to succ/pred. Both stack and queue just encounter 0p since the lists are not circular.
225// Sequence(T) * seq; // FIX ME: cannot be reference
226// };
227
228// static inline {
229// void ?{}( SeqIter(T) & si ) with( si ) {
230// ((ColIter &)si){};
231// seq = 0p;
232// } // post: elts = null.
233
234// void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) {
235// ((ColIter &)si){};
236// seq = &s;
237// curr = &head( s );
238// } // post: elts = null.
239
240// void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) {
241// ((ColIter &)si){};
242// seq = &s;
243// curr = &start;
244// } // post: elts = null.
245
246// void over( SeqIter(T) & si, Sequence(T) & s ) with( si ) {
247// seq = &s;
248// curr = &head( s );
249// } // post: elts = {e in s}.
250
251// bool ?>>?( SeqIter(T) & si, T && tp ) with( si ) {
252// if ( curr ) {
253// &tp = Curr( si );
254// T * n = succ( *seq, Curr( si ) );
255// curr = n == &head( *seq ) ? 0p : n;
256// } else &tp = 0p;
257// return &tp != 0p;
258// }
259// } // distribution
260
261
262// // A SeqIterRev(T) is used to iterate over a Sequence(T) in tail-to-head order.
263// struct SeqIterRev {
264// inline ColIter;
265// // See above for explanation.
266// Sequence(T) * seq; // FIX ME: cannot be reference
267// };
268
269// static inline {
270// void ?{}( SeqIterRev(T) & si ) with( si ) {
271// ((ColIter &)si){};
272// seq = 0p;
273// } // post: elts = null.
274
275// void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {
276// ((ColIter &)si){};
277// seq = &s;
278// curr = &tail( s );
279// } // post: elts = null.
280
281// void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) {
282// ((ColIter &)si){};
283// seq = &s;
284// curr = &start;
285// } // post: elts = null.
286
287// void over( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {
288// seq = &s;
289// curr = &tail( s );
290// } // post: elts = {e in s}.
291
292// bool ?>>?( SeqIterRev(T) & si, T && tp ) with( si ) {
293// if ( curr ) {
294// &tp = Curr( si );
295// T * n = pred( *seq, Curr( si ) );
296// curr = n == &tail( *seq ) ? 0p : n;
297// } else &tp = 0p;
298// return &tp != 0p;
299// }
300// } // distribution
301// } // distribution
302
303#endif
Note: See TracBrowser for help on using the repository browser.