source: libcfa/src/bits/collection.hfa @ a3a76ea

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

removed listed requirement for collections since it was redundant

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#pragma once
2#include <stdio.h> // REMOVE THIS AFTER DEBUGGING
3
4
5struct Colable {
6        struct Colable * next;                                                                          // next node in the list
7        // invariant: (next != 0) <=> listed()
8};
9#ifdef __cforall
10static inline {
11        // PUBLIC
12
13        void ?{}( Colable & co ) with( co ) {
14                next = 0p;
15        } // post: ! listed()
16
17        // return true iff *this is an element of a collection
18        bool listed( Colable & co ) with( co ) {                        // pre: this != 0
19                return next != 0p;
20        }
21
22        Colable & getNext( Colable & co ) with( co ) {
23                return *next;
24        }
25
26        // PRIVATE
27
28        Colable *& Next( Colable * cp ) {
29                return cp->next;
30        }
31
32        // // wrappers to make Collection have T
33        // forall( dtype T ) {
34        //      T *& Next( T * n ) {
35        //              return (T *)Next( (Colable *)n );
36        //      }
37        // } // distribution
38} // distribution
39
40forall( dtype T | { T *& Next ( T * ); } ) {
41        bool listed( T * n ) {
42                return Next( n ) != 0p;
43        }
44}
45
46struct Collection {
47        void * root;                                                                            // pointer to root element of list
48};
49
50static inline {
51        // class invariant: root == 0 & empty() | *root in *this
52        void ?{}( Collection &, const Collection & ) = void; // no copy
53        Collection & ?=?( const Collection & ) = void;          // no assignment
54
55        void ?{}( Collection & collection ) with( collection ) {       
56                root = 0p;
57        } // post: empty()
58
59        bool empty( Collection & collection ) with( collection ) { // 0 <=> *this contains no elements
60                return root == 0p;
61        }
62
63        void * head( Collection & collection ) with( collection ) {
64                return root;
65        } // post: empty() & head() == 0 | !empty() & head() in *this
66} // distribution
67
68
69struct ColIter {
70        void * curr;                                                                            // element to be returned by >>
71};
72
73static inline {
74        void ?{}( ColIter & colIter ) with( colIter ) {
75                curr = 0p;
76        } // post: elts = null
77
78        forall( dtype T ) {
79                T * Curr( ColIter & ci ) with( ci ) {
80                        return (T *)curr;
81                }
82        } // distribution
83} // distribution
84#endif
Note: See TracBrowser for help on using the repository browser.