00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _LIST_H_
00012 #define _LIST_H_
00013
00014 #include "MayDay.H"
00015 #include "Pool.H"
00016
00017 #include "BaseNamespaceHeader.H"
00018
00019 template <class T> class ListLink;
00020 template <class T> class ListIterator;
00021 template <class T> class List;
00022
00023
00024 #ifndef DOXYGEN
00025
00026
00027 template <class T>
00028 class ListLink
00029 {
00030 private:
00031 friend class List<T>;
00032 friend class ListIterator<T>;
00033
00034 ListLink (const T& _val,
00035 ListLink<T>* _pre,
00036 ListLink<T>* _suc)
00037 :
00038 val(_val),
00039 pre(_pre),
00040 suc(_suc)
00041 {}
00042
00043 void swap(ListLink<T>* a);
00044
00045 T val;
00046 ListLink<T>* pre;
00047 ListLink<T>* suc;
00048 };
00049
00050 #endif // doxygen
00051
00052
00053
00054
00055
00056
00057 template <class T>
00058 class ListIterator
00059 {
00060 public:
00061
00062
00063 inline ListIterator (const List<T>& aList);
00064
00065
00066 inline ListIterator (const ListIterator<T>& rhs);
00067
00068
00069 inline void rewind ();
00070
00071
00072
00073
00074
00075 inline void begin();
00076
00077
00078 inline const T& operator() () const;
00079
00080 inline T& operator() () ;
00081
00082
00083 inline const T& operator* () const;
00084
00085
00086
00087
00088
00089
00090
00091
00092 inline operator bool () const;
00093
00094
00095
00096
00097
00098 inline bool ok() const ;
00099
00100
00101 inline bool operator! () const;
00102
00103
00104 inline const T& value () const;
00105
00106
00107 const T& value ();
00108
00109
00110
00111
00112
00113
00114
00115 inline ListIterator<T>& operator++ ();
00116
00117
00118
00119
00120
00121
00122
00123 inline ListIterator<T>& operator-- ();
00124
00125
00126
00127
00128
00129
00130
00131 inline ListIterator<T> operator-- (int);
00132
00133
00134
00135
00136
00137
00138
00139 inline ListIterator<T> operator++ (int);
00140
00141
00142
00143
00144
00145
00146 inline bool operator== (const ListIterator<T>&) const;
00147
00148
00149 inline bool operator!= (const ListIterator<T>&) const;
00150
00151 protected:
00152
00153
00154
00155
00156 inline ListIterator (const List<T>& _list,
00157 ListLink<T>* _p);
00158
00159
00160
00161
00162 const List<T>& list;
00163
00164
00165
00166
00167 ListLink<T>* p;
00168
00169 private:
00170 friend class List<T>;
00171
00172
00173
00174 ListIterator ();
00175 ListIterator<T>& operator= (const ListIterator<T>&);
00176 };
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 template <class T>
00209 class List
00210 {
00211 public:
00212
00213
00214 inline List ();
00215
00216 inline List (bool usePool);
00217
00218
00219 List (const List<T>& rhs);
00220
00221
00222 List<T>& operator= (const List<T>& rhs);
00223
00224
00225 inline ~List();
00226
00227
00228 inline void prepend (const T& value);
00229
00230
00231 inline void append (const T& value);
00232
00233
00234 void add (const T& value);
00235
00236
00237 void join (const List<T>& src);
00238
00239
00240
00241
00242
00243
00244
00245
00246 void catenate (List<T>& src);
00247
00248
00249 void clear ();
00250
00251
00252
00253
00254
00255
00256 inline List<T>* copy () const;
00257
00258
00259 inline T& firstElement () const;
00260
00261
00262 inline T& lastElement () const;
00263
00264
00265
00266
00267
00268 bool includes (const T& value) const;
00269
00270
00271
00272
00273
00274
00275
00276 bool operator== (const List<T>& rhs) const;
00277
00278
00279 bool operator!= (const List<T>& rhs) const;
00280
00281
00282 inline bool isEmpty () const;
00283
00284
00285 inline bool isNotEmpty () const;
00286
00287
00288 int length () const;
00289
00290
00291 inline void removeFirst ();
00292
00293
00294 inline void removeLast ();
00295
00296
00297 inline const T& operator[] (const ListIterator<T>& li) const;
00298
00299
00300 inline T& operator[] (const ListIterator<T>& li);
00301
00302
00303 void remove (const T& value);
00304
00305
00306 void remove (const List<T>& lst);
00307
00308
00309 void remove (ListIterator<T>& lit);
00310
00311
00312 void transfer(ListIterator<T>& lit);
00313
00314
00315 inline void replace (ListIterator<T>& li,
00316 const T& val);
00317
00318
00319 inline void addAfter (ListIterator<T>& lit,
00320 const T& val);
00321
00322
00323 inline void addBefore (ListIterator<T>& lit,
00324 const T& val);
00325
00326
00327 inline ListIterator<T> listIterator () const;
00328
00329
00330 inline ListIterator<T> first () const;
00331
00332
00333 inline ListIterator<T> last () const;
00334
00335
00336 void sort();
00337
00338 void checkLinks() const;
00339 protected:
00340
00341
00342
00343
00344 void remove (ListLink<T> *ln);
00345
00346 void removeLink(ListLink<T> *ln);
00347
00348
00349
00350
00351 ListLink<T>* addBefore (ListLink<T>* ln,
00352 const T& val);
00353
00354
00355
00356
00357 ListLink<T>* addAfter (ListLink<T>* ln,
00358 const T& val);
00359
00360
00361
00362
00363 ListLink<T>* head;
00364
00365
00366
00367
00368 ListLink<T>* tail;
00369
00370
00371
00372
00373 friend class ListIterator<T>;
00374
00375
00376
00377
00378
00379 static Pool linkPool;
00380
00381 bool m_usePool;
00382 };
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 template <class T>
00393 inline
00394 ListIterator<T>::ListIterator (const List<T>& _list,
00395 ListLink<T>* _p)
00396 :
00397 list(_list),
00398 p(_p)
00399 {
00400 }
00401
00402 template <class T>
00403 inline
00404 ListIterator<T>::ListIterator (const List<T>& aList)
00405 : list(aList)
00406 {
00407 p = list.head;
00408 }
00409
00410 template <class T>
00411 inline
00412 ListIterator<T>::ListIterator (const ListIterator<T>& li)
00413 :
00414 list(li.list),
00415 p(li.p)
00416 {
00417 }
00418
00419 template <class T>
00420 inline
00421 void
00422 ListIterator<T>::rewind ()
00423 {
00424 p = list.head;
00425 }
00426
00427 template <class T>
00428 inline
00429 void
00430 ListIterator<T>::begin ()
00431 {
00432 p = list.head;
00433 }
00434
00435 template <class T>
00436 inline
00437 const T&
00438 ListIterator<T>::operator() () const
00439 {
00440 CH_assert(p != 0);
00441 return p->val;
00442 }
00443
00444 template <class T>
00445 inline
00446 T&
00447 ListIterator<T>::operator() ()
00448 {
00449 CH_assert(p != 0);
00450 return p->val;
00451 }
00452
00453 template <class T>
00454 inline
00455 const T&
00456 ListIterator<T>::operator* () const
00457 {
00458 CH_assert(p != 0);
00459 return p->val;
00460 }
00461
00462 template <class T>
00463 inline
00464 bool
00465 ListIterator<T>::ok() const
00466 {
00467 return p != 0 ? true : false;
00468 }
00469
00470 template <class T>
00471 inline
00472 ListIterator<T>::operator bool () const
00473 {
00474 return ok() ;
00475 }
00476
00477 template <class T>
00478 inline
00479 bool
00480 ListIterator<T>::operator! () const
00481 {
00482 return p == 0 ? true : false;
00483 }
00484
00485 template <class T>
00486 inline
00487 const T&
00488 ListIterator<T>::value () const
00489 {
00490 CH_assert(p != 0);
00491 return p->val;
00492 }
00493
00494 template <class T>
00495 inline
00496 ListIterator<T>&
00497 ListIterator<T>::operator++ ()
00498 {
00499 if (p)
00500 p = p->suc;
00501 return *this;
00502 }
00503
00504 template <class T>
00505 inline
00506 ListIterator<T>&
00507 ListIterator<T>::operator-- ()
00508 {
00509 if (p)
00510 p = p->pre;
00511 return *this;
00512 }
00513
00514 template <class T>
00515 inline
00516 ListIterator<T>
00517 ListIterator<T>::operator++ (int)
00518 {
00519 const ListIterator<T> li = *this;
00520 ++(*this);
00521 return li;
00522 }
00523
00524 template <class T>
00525 inline
00526 ListIterator<T>
00527 ListIterator<T>::operator-- (int)
00528 {
00529 const ListIterator<T> li = *this;
00530 --(*this);
00531 return li;
00532 }
00533
00534 template <class T>
00535 inline
00536 bool
00537 ListIterator<T>::operator== (const ListIterator<T>& _li) const
00538 {
00539 return (&list == &_li.list && p == _li.p) ? true : false;
00540 }
00541
00542 template <class T>
00543 inline
00544 bool
00545 ListIterator<T>::operator!= (const ListIterator<T>& _li) const
00546 {
00547 return ! ListIterator<T>::operator==(_li);
00548 }
00549
00550
00551
00552
00553
00554 template <class T>
00555 inline
00556 List<T>::List ()
00557 :
00558 head(0),
00559 tail(0),
00560 m_usePool(true)
00561 {
00562 }
00563
00564 template <class T>
00565 inline
00566 List<T>::~List ()
00567 {
00568 clear();
00569 }
00570
00571 template <class T>
00572 inline
00573 void
00574 List<T>::prepend (const T& value)
00575 {
00576 addBefore(head, value);
00577 }
00578
00579 template <class T>
00580 inline
00581 void
00582 List<T>::append (const T& value)
00583 {
00584 addAfter(tail, value);
00585 }
00586
00587
00588 template <class T>
00589 inline
00590 List<T>*
00591 List<T>::copy () const
00592 {
00593 List<T>* newlist = new List<T>(*this);
00594 if (newlist == 0)
00595 MayDay::Error("Out of memory in List::copy ");
00596 return newlist;
00597 }
00598
00599 template <class T>
00600 inline
00601 T&
00602 List<T>::firstElement () const
00603 {
00604 CH_assert(head != 0);
00605 return head->val;
00606 }
00607
00608 template <class T>
00609 inline
00610 T&
00611 List<T>::lastElement () const
00612 {
00613 CH_assert(tail != 0);
00614 return tail->val;
00615 }
00616
00617 template <class T>
00618 inline
00619 bool
00620 List<T>::isEmpty () const
00621 {
00622 return head == 0 && tail == 0;
00623 }
00624
00625 template <class T>
00626 inline
00627 bool
00628 List<T>::isNotEmpty () const
00629 {
00630 return !isEmpty();
00631 }
00632
00633 template <class T>
00634 inline
00635 void
00636 List<T>::removeFirst ()
00637 {
00638 remove(head);
00639 }
00640
00641 template <class T>
00642 inline
00643 void
00644 List<T>::removeLast ()
00645 {
00646 remove(tail);
00647 }
00648
00649 template <class T>
00650 inline
00651 const T&
00652 List<T>::operator[] (const ListIterator<T>& li) const
00653 {
00654 CH_assert(li.p != 0);
00655 return li.p->val;
00656 }
00657
00658 template <class T>
00659 inline
00660 T&
00661 List<T>::operator[] (const ListIterator<T>& li)
00662 {
00663 CH_assert(li.p != 0);
00664 return li.p->val;
00665 }
00666
00667 template <class T>
00668 inline
00669 void
00670 List<T>::replace (ListIterator<T>& li,
00671 const T& _val)
00672 {
00673 CH_assert(li.p != 0);
00674 li.p->val = _val;
00675 }
00676
00677 template <class T>
00678 inline
00679 void
00680 List<T>::addAfter (ListIterator<T>& lit,
00681 const T& val)
00682 {
00683 addAfter(lit.p, val);
00684 }
00685
00686 template <class T>
00687 inline
00688 void
00689 List<T>::addBefore (ListIterator<T>& lit,
00690 const T& val)
00691 {
00692 addBefore(lit.p, val);
00693 }
00694
00695 template <class T>
00696 inline
00697 ListIterator<T>
00698 List<T>::first () const
00699 {
00700 return ListIterator<T>(*this,head);
00701 }
00702
00703 template <class T>
00704 inline
00705 ListIterator<T>
00706 List<T>::listIterator () const
00707 {
00708 return ListIterator<T>(*this,head);
00709 }
00710
00711 template <class T>
00712 inline
00713 ListIterator<T>
00714 List<T>::last () const
00715 {
00716 return ListIterator<T>(*this,tail);
00717 }
00718
00719 #include "BaseNamespaceFooter.H"
00720
00721 #include "ListImplem.H"
00722
00723 #endif