source:
libcfa/src/bits/collection.hfa
@
f3e87af
Last change on this file since f3e87af was 9536761, checked in by , 4 years ago | |
---|---|
|
|
File size: 1.8 KB |
Rev | Line | |
---|---|---|
[5e82d56] | 1 | #pragma once |
[e43aa14] | 2 | #include <stdio.h> // REMOVE THIS AFTER DEBUGGING |
3 | ||
[5e82d56] | 4 | |
5 | struct Colable { | |
[e43aa14] | 6 | struct Colable * next; // next node in the list |
[5e82d56] | 7 | // invariant: (next != 0) <=> listed() |
8 | }; | |
[e43aa14] | 9 | #ifdef __cforall |
10 | static inline { | |
[7c1144b] | 11 | // PUBLIC |
12 | ||
[5e82d56] | 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 | |
[636d3715] | 19 | return next != 0p; |
[5e82d56] | 20 | } |
21 | ||
[7c1144b] | 22 | Colable & getNext( Colable & co ) with( co ) { |
23 | return *next; | |
[5e82d56] | 24 | } |
25 | ||
[7c1144b] | 26 | // PRIVATE |
27 | ||
[5e82d56] | 28 | Colable *& Next( Colable * cp ) { |
29 | return cp->next; | |
30 | } | |
[636d3715] | 31 | |
[e43aa14] | 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 | |
[5e82d56] | 38 | } // distribution |
39 | ||
[19de7864] | 40 | forall( dtype T | { T *& Next ( T * ); } ) { |
41 | bool listed( T * n ) { | |
42 | return Next( n ) != 0p; | |
43 | } | |
44 | } | |
[636d3715] | 45 | |
[5e82d56] | 46 | struct Collection { |
47 | void * root; // pointer to root element of list | |
48 | }; | |
49 | ||
[e43aa14] | 50 | static inline { |
[5e82d56] | 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 | } | |
[636d3715] | 62 | |
[5e82d56] | 63 | void * head( Collection & collection ) with( collection ) { |
64 | return root; | |
65 | } // post: empty() & head() == 0 | !empty() & head() in *this | |
66 | } // distribution | |
67 | ||
68 | ||
69 | struct ColIter { | |
[9536761] | 70 | void * curr; // element returned by | |
[5e82d56] | 71 | }; |
72 | ||
[e43aa14] | 73 | static inline { |
[5e82d56] | 74 | void ?{}( ColIter & colIter ) with( colIter ) { |
75 | curr = 0p; | |
76 | } // post: elts = null | |
[636d3715] | 77 | |
78 | forall( dtype T ) { | |
79 | T * Curr( ColIter & ci ) with( ci ) { | |
80 | return (T *)curr; | |
81 | } | |
82 | } // distribution | |
[5e82d56] | 83 | } // distribution |
[9536761] | 84 | #endif |
Note: See TracBrowser
for help on using the repository browser.