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