Changes in libcfa/src/bits/queue.hfa [7d4ce2a:9536761]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/queue.hfa
r7d4ce2a r9536761 3 3 #include "bits/collection.hfa" 4 4 5 forall( dtype T ) { 5 // A Queue(T) is a Collection(T) defining the ordering that nodes are returned by drop() in the same order from those 6 // added by add(). T must be a public descendant of uColable. 7 8 // The implementation is a typical singly-linked list, except the next field of the last element points to itself 9 // instead of being null. 10 11 forall( dtype T | { T *& Next ( T * ); } ) { 6 12 struct Queue { 7 13 inline Collection; // Plan 9 inheritance … … 34 40 } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *q 35 41 36 voidaddHead( Queue(T) & q, T & n ) with( q ) {42 T & addHead( Queue(T) & q, T & n ) with( q ) { 37 43 #ifdef __CFA_DEBUG__ 38 44 if ( listed( &n ) ) abort( "(Queue &)%p.addHead( %p ) : Node is already on another list.", &q, &n ); … … 45 51 Next( &n ) = &n; // last node points to itself 46 52 } 53 return n; 47 54 } 48 55 49 voidaddTail( Queue(T) & q, T & n ) with( q ) {56 T & addTail( Queue(T) & q, T & n ) with( q ) { 50 57 #ifdef __CFA_DEBUG__ 51 58 if ( listed( &n ) ) abort( "(Queue &)%p.addTail( %p ) : Node is already on another list.", &q, &n ); … … 55 62 last = &n; 56 63 Next( &n ) = &n; // last node points to itself 64 return n; 57 65 } 58 66 59 voidadd( Queue(T) & q, T & n ) with( q ) {60 addTail( q, n );67 T & add( Queue(T) & q, T & n ) with( q ) { 68 return addTail( q, n ); 61 69 } 62 70 … … 64 72 T & t = head( q ); 65 73 if ( root ) { 66 root = Next( root );74 root = Next( (T *)root ); 67 75 if ( &head( q ) == &t ) { 68 76 root = last = 0p; // only one element … … 77 85 } 78 86 79 voidremove( Queue(T) & q, T & n ) with( q ) { // O(n)87 T & remove( Queue(T) & q, T & n ) with( q ) { // O(n) 80 88 #ifdef __CFA_DEBUG__ 81 89 if ( ! listed( (Colable &)n ) ) abort( "(Queue &)%p.remove( %p ) : Node is not on a list.", &q, &n ); … … 103 111 curr = Next( curr ); 104 112 } 113 return n; 105 114 } // post: ! listed( n ) 106 115 107 T & dropTail( Queue(T) & q ) with( q ) { 116 T & dropTail( Queue(T) & q ) with( q ) { // O(n) 108 117 T & n = tail( q ); 109 118 return &n ? remove( q, n ), n : *0p; … … 142 151 } // distribution 143 152 144 forall( dtype T ) {153 forall( dtype T | { T *& Next ( T * ); } ) { 145 154 struct QueueIter { 146 155 inline ColIter; // Plan 9 inheritance … … 152 161 } // post: curr == 0p 153 162 154 // create an iterator active in Queue q163 // create an iterator active in queue q 155 164 void ?{}( QueueIter(T) & qi, Queue(T) & q ) with( qi ) { 156 165 curr = &head( q ); … … 161 170 } // post: curr = {e in q} 162 171 163 // make existing iterator active in Queue q172 // make existing iterator active in queue q 164 173 void over( QueueIter(T) & qi, Queue(T) & q ) with( qi ) { 165 174 curr = &head( q ); 166 175 } // post: curr = {e in q} 167 176 168 bool ? >>?( QueueIter(T) & qi, T && tp ) with( qi ) {177 bool ?|?( QueueIter(T) & qi, T && tp ) with( qi ) { 169 178 if ( curr ) { 170 179 &tp = Curr( qi ); … … 174 183 return &tp != 0p; 175 184 } 176 // post: elts == null & !operator >>(tp) | elts != null & *tp' in elts & elts' == elts - *tp & operator>>(tp)185 // post: elts == null & !operator|(tp) | elts != null & *tp' in elts & elts' == elts - *tp & operator|(tp) 177 186 } // distribution 178 187 } // distribution
Note: See TracChangeset
for help on using the changeset viewer.