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