| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // bits/collection.hfa -- PUBLIC
 | 
|---|
| 8 | // Intrusive singly-linked list
 | 
|---|
| 9 | //
 | 
|---|
| 10 | // Author           : Colby Alexander Parsons & Peter A. Buhr
 | 
|---|
| 11 | // Created On       : Thu Jan 21 19:46:50 2021
 | 
|---|
| 12 | // Last Modified By :
 | 
|---|
| 13 | // Last Modified On :
 | 
|---|
| 14 | // Update Count     :
 | 
|---|
| 15 | //
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #pragma once
 | 
|---|
| 18 | 
 | 
|---|
| 19 | struct Colable {
 | 
|---|
| 20 |         // next node in the list
 | 
|---|
| 21 |         // invariant: (next != 0) <=> listed()
 | 
|---|
| 22 |         struct Colable * next;
 | 
|---|
| 23 | };
 | 
|---|
| 24 | #ifdef __cforall
 | 
|---|
| 25 | static inline {
 | 
|---|
| 26 |         // PUBLIC
 | 
|---|
| 27 | 
 | 
|---|
| 28 |         void ?{}( Colable & co ) with( co ) {
 | 
|---|
| 29 |                 next = 0p;
 | 
|---|
| 30 |         } // post: ! listed()
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         // return true iff *this is an element of a collection
 | 
|---|
| 33 |         bool listed( Colable & co ) with( co ) {                        // pre: this != 0
 | 
|---|
| 34 |                 return next != 0p;
 | 
|---|
| 35 |         }
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         Colable & getNext( Colable & co ) with( co ) {
 | 
|---|
| 38 |                 return *next;
 | 
|---|
| 39 |         }
 | 
|---|
| 40 | 
 | 
|---|
| 41 |         // PRIVATE
 | 
|---|
| 42 | 
 | 
|---|
| 43 |         Colable *& Next( Colable * cp ) {
 | 
|---|
| 44 |                 return cp->next;
 | 
|---|
| 45 |         }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         // // wrappers to make Collection have T
 | 
|---|
| 48 |         // forall( T & ) {
 | 
|---|
| 49 |         //      T *& Next( T * n ) {
 | 
|---|
| 50 |         //              return (T *)Next( (Colable *)n );
 | 
|---|
| 51 |         //      }
 | 
|---|
| 52 |         // } // distribution
 | 
|---|
| 53 | } // distribution
 | 
|---|
| 54 | 
 | 
|---|
| 55 | static inline forall( T & | { T *& Next ( T * ); } ) {
 | 
|---|
| 56 |         bool listed( T * n ) {
 | 
|---|
| 57 |                 return Next( n ) != 0p;
 | 
|---|
| 58 |         }
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | struct Collection {
 | 
|---|
| 62 |         void * root;                                                                            // pointer to root element of list
 | 
|---|
| 63 | };
 | 
|---|
| 64 | 
 | 
|---|
| 65 | static inline {
 | 
|---|
| 66 |         // class invariant: root == 0 & empty() | *root in *this
 | 
|---|
| 67 |         void ?{}( Collection &, const Collection & ) = void; // no copy
 | 
|---|
| 68 |         Collection & ?=?( const Collection & ) = void;          // no assignment
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         void ?{}( Collection & collection ) with( collection ) {
 | 
|---|
| 71 |                 root = 0p;
 | 
|---|
| 72 |         } // post: empty()
 | 
|---|
| 73 | 
 | 
|---|
| 74 |         bool empty( Collection & collection ) with( collection ) { // 0 <=> *this contains no elements
 | 
|---|
| 75 |                 return root == 0p;
 | 
|---|
| 76 |         }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         void * head( Collection & collection ) with( collection ) {
 | 
|---|
| 79 |                 return root;
 | 
|---|
| 80 |         } // post: empty() & head() == 0 | !empty() & head() in *this
 | 
|---|
| 81 | } // distribution
 | 
|---|
| 82 | 
 | 
|---|
| 83 | 
 | 
|---|
| 84 | struct ColIter {
 | 
|---|
| 85 |         void * curr;                                                                            // element returned by |
 | 
|---|
| 86 | };
 | 
|---|
| 87 | 
 | 
|---|
| 88 | static inline {
 | 
|---|
| 89 |         void ?{}( ColIter & colIter ) with( colIter ) {
 | 
|---|
| 90 |                 curr = 0p;
 | 
|---|
| 91 |         } // post: elts = null
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         forall( T & ) {
 | 
|---|
| 94 |                 T * Curr( ColIter & ci ) with( ci ) {
 | 
|---|
| 95 |                         return (T *)curr;
 | 
|---|
| 96 |                 }
 | 
|---|
| 97 |         } // distribution
 | 
|---|
| 98 | } // distribution
 | 
|---|
| 99 | #endif
 | 
|---|