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

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 dde7d6d was 58870e6b, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

switch from reference back to pointer

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