aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-07-11 21:48:41 +0000
committerMike Buland <eichlan@xagasoft.com>2007-07-11 21:48:41 +0000
commitd05de564091c24677a94e16963dad20be4b00ca0 (patch)
tree102e2a27c55011979292ff3cc3ff3f3049da1de3
parent264ce2905eee70abbb472e9d93d1dd377692dfdc (diff)
downloadlibbu++-d05de564091c24677a94e16963dad20be4b00ca0.tar.gz
libbu++-d05de564091c24677a94e16963dad20be4b00ca0.tar.bz2
libbu++-d05de564091c24677a94e16963dad20be4b00ca0.tar.xz
libbu++-d05de564091c24677a94e16963dad20be4b00ca0.zip
I guess I fixed some list stuff...
-rw-r--r--src/list.h106
-rw-r--r--src/tests/list.cpp19
2 files changed, 75 insertions, 50 deletions
diff --git a/src/list.h b/src/list.h
index 8f29e85..edbd78c 100644
--- a/src/list.h
+++ b/src/list.h
@@ -149,19 +149,24 @@ namespace Bu
149 friend class List<value, valuealloc, linkalloc>; 149 friend class List<value, valuealloc, linkalloc>;
150 private: 150 private:
151 Link *pLink; 151 Link *pLink;
152 iterator() : 152 MyType &rList;
153 pLink( NULL ) 153 bool bOffFront;
154 iterator( MyType &rList ) :
155 pLink( NULL ),
156 rList( rList )
154 { 157 {
155 } 158 }
156 159
157 iterator( Link *pLink ) : 160 iterator( Link *pLink, MyType &rList ) :
158 pLink( pLink ) 161 pLink( pLink ),
162 rList( rList )
159 { 163 {
160 } 164 }
161 165
162 public: 166 public:
163 iterator( const iterator &i ) : 167 iterator( const iterator &i ) :
164 pLink( i.pLink ) 168 pLink( i.pLink ),
169 rList( i.rList )
165 { 170 {
166 } 171 }
167 172
@@ -223,43 +228,43 @@ namespace Bu
223 return pLink->pValue; 228 return pLink->pValue;
224 } 229 }
225 230
226 /**
227 * Increment operator.
228 */
229 iterator &operator++() 231 iterator &operator++()
230 { 232 {
231 if( pLink != NULL ) 233 if( pLink == NULL )
234 pLink = (bOffFront)?(rList.pFirst):(NULL);
235 else
232 pLink = pLink->pNext; 236 pLink = pLink->pNext;
237 bOffFront = false;
233 return *this; 238 return *this;
234 } 239 }
235 240
236 /**
237 * Decrement operator.
238 */
239 iterator &operator--() 241 iterator &operator--()
240 { 242 {
241 if( pLink != NULL ) 243 if( pLink == NULL )
244 pLink = (bOffFront)?(NULL):(rList.pLast);
245 else
242 pLink = pLink->pPrev; 246 pLink = pLink->pPrev;
247 bOffFront = true;
243 return *this; 248 return *this;
244 } 249 }
245 250
246 /**
247 * Increment operator.
248 */
249 iterator &operator++( int ) 251 iterator &operator++( int )
250 { 252 {
251 if( pLink != NULL ) 253 if( pLink == NULL )
254 pLink = (bOffFront)?(rList.pFirst):(NULL);
255 else
252 pLink = pLink->pNext; 256 pLink = pLink->pNext;
257 bOffFront = false;
253 return *this; 258 return *this;
254 } 259 }
255 260
256 /**
257 * Decrement operator.
258 */
259 iterator &operator--( int ) 261 iterator &operator--( int )
260 { 262 {
261 if( pLink != NULL ) 263 if( pLink == NULL )
264 pLink = (bOffFront)?(NULL):(rList.pLast);
265 else
262 pLink = pLink->pPrev; 266 pLink = pLink->pPrev;
267 bOffFront = true;
263 return *this; 268 return *this;
264 } 269 }
265 270
@@ -283,19 +288,24 @@ namespace Bu
283 friend class List<value, valuealloc, linkalloc>; 288 friend class List<value, valuealloc, linkalloc>;
284 private: 289 private:
285 Link *pLink; 290 Link *pLink;
286 const_iterator() : 291 const MyType &rList;
287 pLink( NULL ) 292 bool bOffFront;
293 const_iterator( const MyType &rList ) :
294 pLink( NULL ),
295 rList( rList )
288 { 296 {
289 } 297 }
290 298
291 const_iterator( Link *pLink ) : 299 const_iterator( Link *pLink, const MyType &rList ) :
292 pLink( pLink ) 300 pLink( pLink ),
301 rList( rList )
293 { 302 {
294 } 303 }
295 304
296 public: 305 public:
297 const_iterator( const iterator &i ) : 306 const_iterator( const iterator &i ) :
298 pLink( i.pLink ) 307 pLink( i.pLink ),
308 rList( i.rList )
299 { 309 {
300 } 310 }
301 311
@@ -331,29 +341,41 @@ namespace Bu
331 341
332 const_iterator &operator++() 342 const_iterator &operator++()
333 { 343 {
334 if( pLink != NULL ) 344 if( pLink == NULL )
345 pLink = (bOffFront)?(rList.pFirst):(NULL);
346 else
335 pLink = pLink->pNext; 347 pLink = pLink->pNext;
348 bOffFront = false;
336 return *this; 349 return *this;
337 } 350 }
338 351
339 const_iterator &operator--() 352 const_iterator &operator--()
340 { 353 {
341 if( pLink != NULL ) 354 if( pLink == NULL )
355 pLink = (bOffFront)?(NULL):(rList.pLast);
356 else
342 pLink = pLink->pPrev; 357 pLink = pLink->pPrev;
358 bOffFront = true;
343 return *this; 359 return *this;
344 } 360 }
345 361
346 const_iterator &operator++( int ) 362 const_iterator &operator++( int )
347 { 363 {
348 if( pLink != NULL ) 364 if( pLink == NULL )
365 pLink = (bOffFront)?(rList.pFirst):(NULL);
366 else
349 pLink = pLink->pNext; 367 pLink = pLink->pNext;
368 bOffFront = false;
350 return *this; 369 return *this;
351 } 370 }
352 371
353 const_iterator &operator--( int ) 372 const_iterator &operator--( int )
354 { 373 {
355 if( pLink != NULL ) 374 if( pLink == NULL )
375 pLink = (bOffFront)?(NULL):(rList.pLast);
376 else
356 pLink = pLink->pPrev; 377 pLink = pLink->pPrev;
378 bOffFront = true;
357 return *this; 379 return *this;
358 } 380 }
359 381
@@ -376,16 +398,16 @@ namespace Bu
376 */ 398 */
377 iterator begin() 399 iterator begin()
378 { 400 {
379 return iterator( pFirst ); 401 return iterator( pFirst, *this );
380 } 402 }
381 403
382 /** 404 /**
383 * Get a const iterator pointing to the first item in the list. 405 * Get a const iterator pointing to the first item in the list.
384 *@returns (const const_iterator) 406 *@returns (const const_iterator)
385 */ 407 */
386 const const_iterator begin() const 408 const_iterator begin() const
387 { 409 {
388 return const_iterator( pFirst ); 410 return const_iterator( pFirst, *this );
389 } 411 }
390 412
391 /** 413 /**
@@ -399,27 +421,13 @@ namespace Bu
399 } 421 }
400 422
401 /** 423 /**
402 * Erase an item from the list. After erasing the iterator will point 424 * Erase an item from the list.
403 * to an invalid location and should be ignored. To erase an item from
404 * a list in a loop you should create a backup iterator.
405 *@code
406 for( List<>::iterator i = l.begin(); i != l.end(); i++ )
407 {
408 if( ...(needs delete)... )
409 {
410 List<>::iterator prev = i;
411 prev--;
412 l.erase( i );
413 i = prev;
414 continue;
415 }
416 }
417 @endcode
418 *@param i (iterator) The item to erase. 425 *@param i (iterator) The item to erase.
419 */ 426 */
420 void erase( iterator i ) 427 void erase( iterator i )
421 { 428 {
422 Link *pCur = i.pLink; 429 Link *pCur = i.pLink;
430 if( pCur == NULL ) return;
423 Link *pPrev = pCur->pPrev; 431 Link *pPrev = pCur->pPrev;
424 if( pPrev == NULL ) 432 if( pPrev == NULL )
425 { 433 {
diff --git a/src/tests/list.cpp b/src/tests/list.cpp
index 12807a5..edbe288 100644
--- a/src/tests/list.cpp
+++ b/src/tests/list.cpp
@@ -18,6 +18,21 @@ int main()
18 l.prepend( -j ); 18 l.prepend( -j );
19 } 19 }
20 20
21 {
22 Bu::List<int>::iterator i = l.begin();
23 Bu::List<int>::iterator j = i;
24 int a, b;
25 a = *j;
26 printf("end: %s\n", (j != l.end())?"no":"yes");
27 j--;
28 printf("end: %s\n", (j != l.end())?"no":"yes");
29 j++;
30 printf("end: %s\n", (j != l.end())?"no":"yes");
31 i = j;
32 b = *i;
33 printf("%d -> %d\n", a, b );
34 }
35
21 for( Bu::List<int>::iterator i = l.begin(); i != l.end(); i++ ) 36 for( Bu::List<int>::iterator i = l.begin(); i != l.end(); i++ )
22 { 37 {
23 printf("%d ", *i ); 38 printf("%d ", *i );
@@ -25,9 +40,11 @@ int main()
25 printf("\n"); 40 printf("\n");
26 for( Bu::List<int>::iterator i = l.begin(); i != l.end(); i++ ) 41 for( Bu::List<int>::iterator i = l.begin(); i != l.end(); i++ )
27 { 42 {
43 Bu::List<int>::iterator j = i; j--;
28 l.erase( i ); 44 l.erase( i );
45 i = j;
29 if( i != l.end() ) 46 if( i != l.end() )
30 printf("%d ", *i ); 47 printf("!%d ", *i );
31 } 48 }
32 49
33 printf("\n\n"); 50 printf("\n\n");