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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since fd54fef was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

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