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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c74e601 was a78c3ff, checked in by Colby Alexander Parsons <caparsons@…>, 4 years ago

switched queue and sequence to use references

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