Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

List.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 
00029 #ifndef _LIST_H_
00030 #define _LIST_H_
00031 
00032 #include "MayDay.H"
00033 
00034 
00035 template <class T> class ListLink;
00036 template <class T> class ListIterator;
00037 template <class T> class List;
00038 
00039 // this is so this internal class doesn't appear in doxygen documentation
00040 #ifndef DOXYGEN
00041 
00042 
00043 // Internal helper class for the List class
00044 template <class T>
00045 class ListLink
00046 {
00047 private:
00048     friend class List<T>;
00049     friend class ListIterator<T>;
00050 
00051     ListLink (const T&     _val,
00052               ListLink<T>* _pre,
00053               ListLink<T>* _suc)
00054         : val(_val),
00055           pre(_pre),
00056           suc(_suc)
00057     {}
00058 
00059 private:
00060     T            val;
00061     ListLink<T>* pre;
00062     ListLink<T>* suc;
00063 };
00064 
00065 #endif // doxygen 
00066 
00067 
00069 
00073 template <class T>
00074 class ListIterator
00075 {
00076 public:
00077   
00079   inline ListIterator (const List<T>& aList);
00080   
00082   inline ListIterator (const ListIterator<T>& rhs);
00083   
00085   inline void rewind ();
00086 
00088 
00091   inline void begin();
00092 
00094   inline const T& operator() () const;
00095  
00096   inline T& operator() () ;
00097 
00099   inline const T& operator* () const;
00100 
00102 
00108   inline operator bool () const;
00109 
00111 
00114   inline bool ok() const ;
00115   
00117   inline bool operator! () const;
00118 
00120   inline const T& value () const;
00121   
00123   const T& value ();
00124   
00126 
00131   inline ListIterator<T>& operator++ ();
00132 
00134 
00139   inline ListIterator<T>& operator-- ();
00140 
00142 
00147   inline ListIterator<T> operator-- (int);
00148 
00150 
00155   inline ListIterator<T> operator++ (int);
00156   
00158 
00162   inline bool operator== (const ListIterator<T>&) const;
00163   
00164   
00166   inline bool operator!= (const ListIterator<T>&) const;
00167 
00168 protected:
00169 
00173   inline ListIterator (const List<T>& _list,
00174                        ListLink<T>*   _p);
00175 
00179   const List<T>& list;
00180 
00184   ListLink<T>* p;
00185 
00186 private:
00187     friend class List<T>;
00188     //
00189     // These are disallowed.
00190     //
00191     ListIterator ();
00192     ListIterator<T>& operator= (const ListIterator<T>&);
00193 };
00194 
00195 
00197 
00226 template <class T>
00227 class List
00228 {
00229 public:
00230     
00232   inline List ();
00233   
00234   
00236   List (const List<T>& rhs);
00237   
00239   List<T>& operator= (const List<T>& rhs);
00240 
00241 
00243   inline ~List();
00244 
00246   inline void prepend (const T& value);
00247 
00249   inline void append (const T& value);
00250 
00252   void add (const T& value);
00253   
00255   void join (const List<T>& src);
00256 
00258 
00264   void catenate (List<T>& src);
00265 
00267   void clear ();
00268 
00270 
00274   inline List<T>* copy () const;
00275 
00276 
00278   inline T& firstElement () const;
00279 
00281   inline T& lastElement () const;
00282   
00284 
00287   bool includes (const T& value) const;
00288   
00290 
00295   bool operator== (const List<T>& rhs) const;
00296   
00298   bool operator!= (const List<T>& rhs) const;
00299   
00300   
00302   inline bool isEmpty () const;
00303   
00305   inline bool isNotEmpty () const;
00306   
00308   int length () const;
00309   
00310   
00312   inline void removeFirst ();
00313   
00315   inline void removeLast ();
00316   
00318   inline const T& operator[] (const ListIterator<T>& li) const;
00319   
00321   inline T& operator[] (const ListIterator<T>& li);
00322 
00324   void remove (const T& value);
00325   
00327   void remove (const List<T>& lst);
00328 
00330   void remove (ListIterator<T>& lit);
00331 
00333   inline void replace (ListIterator<T>& li,
00334                        const T&         val);
00335   
00337   inline void addAfter (ListIterator<T>& lit,
00338                         const T&         val);
00339   
00341   inline void addBefore (ListIterator<T>& lit,
00342                          const T&         val);
00343 
00345   inline ListIterator<T> listIterator () const;
00346 
00348   inline ListIterator<T> first () const;
00349 
00350 
00352   inline ListIterator<T> last () const;
00353   
00354 protected:
00355 
00359   void remove (ListLink<T> *ln);
00360 
00364   ListLink<T>* addBefore (ListLink<T>* ln,
00365                           const T&     val);
00366 
00370   ListLink<T>* addAfter (ListLink<T>* ln,
00371                          const T&     val);
00372 
00376   ListLink<T>* head;
00377 
00378 
00382   ListLink<T>* tail;
00383 
00387   friend class ListIterator<T>;
00388   
00389 };
00390 
00391 //
00392 // Inlines.
00393 //
00394 
00395 //
00396 // The ListIterator stuff.
00397 //
00398 
00399 template <class T>
00400 inline
00401 ListIterator<T>::ListIterator (const List<T>& _list,
00402                                ListLink<T>*   _p)
00403   : list(_list),
00404     p(_p)
00405 {}
00406 
00407 template <class T>
00408 inline
00409 ListIterator<T>::ListIterator (const List<T>& aList)
00410   : list(aList)
00411 {
00412   p = list.head;
00413 }
00414 
00415 template <class T>
00416 inline
00417 ListIterator<T>::ListIterator (const ListIterator<T>& li)
00418   : list(li.list),
00419     p(li.p)
00420 {}
00421 
00422 template <class T>
00423 inline
00424 void
00425 ListIterator<T>::rewind ()
00426 {
00427   p = list.head;
00428 }
00429 
00430 template <class T>
00431 inline
00432 void
00433 ListIterator<T>::begin ()
00434 {
00435   p = list.head;
00436 }
00437 
00438 template <class T>
00439 inline
00440 const T&
00441 ListIterator<T>::operator() () const
00442 {
00443   assert(p != 0);
00444   return p->val;
00445 }
00446 
00447 template <class T>
00448 inline
00449 T&
00450 ListIterator<T>::operator() () 
00451 {
00452   assert(p != 0);
00453   return p->val;
00454 }
00455 
00456 template <class T>
00457 inline
00458 const T&
00459 ListIterator<T>::operator* () const
00460 {
00461   assert(p != 0);
00462   return p->val;
00463 }
00464 
00465 template <class T>
00466 inline
00467 bool
00468 ListIterator<T>::ok() const
00469 {
00470   return p != 0 ? true : false;
00471 }
00472 
00473 template <class T>
00474 inline
00475 ListIterator<T>::operator bool () const
00476 {
00477   return ok() ;
00478 }
00479 
00480 template <class T>
00481 inline
00482 bool
00483 ListIterator<T>::operator! () const
00484 {
00485   return p == 0 ? true : false;
00486 }
00487 
00488 template <class T>
00489 inline
00490 const T&
00491 ListIterator<T>::value () const
00492 {
00493   assert(p != 0);
00494   return p->val;
00495 }
00496 
00497 template <class T>
00498 inline
00499 ListIterator<T>&
00500 ListIterator<T>::operator++ ()
00501 {
00502   if (p)
00503     p = p->suc;
00504   return *this;
00505 }
00506 
00507 template <class T>
00508 inline
00509 ListIterator<T>&
00510 ListIterator<T>::operator-- ()
00511 {
00512   if (p)
00513     p = p->pre;
00514   return *this;
00515 }
00516 
00517 template <class T>
00518 inline
00519 ListIterator<T>
00520 ListIterator<T>::operator++ (int)
00521 {
00522   const ListIterator<T> li = *this;
00523   ++(*this);
00524   return li;
00525 }
00526 
00527 template <class T>
00528 inline
00529 ListIterator<T>
00530 ListIterator<T>::operator-- (int)
00531 {
00532   const ListIterator<T> li = *this;
00533   --(*this);
00534   return li;
00535 }
00536 
00537 template <class T>
00538 inline
00539 bool
00540 ListIterator<T>::operator== (const ListIterator<T>& _li) const
00541 {
00542   return (&list == &_li.list && p == _li.p) ? true : false;
00543 }
00544 
00545 template <class T>
00546 inline
00547 bool
00548 ListIterator<T>::operator!= (const ListIterator<T>& _li) const
00549 {
00550   return ! ListIterator<T>::operator==(_li);
00551 }
00552 
00553 //
00554 // List stuff.
00555 //
00556 
00557 template <class T>
00558 inline
00559 List<T>::List ()
00560     : head(0),
00561       tail(0)
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 template <class T>
00588 inline
00589 List<T>*
00590 List<T>::copy () const
00591 {
00592   List<T>* newlist = new List<T>(*this);
00593   if (newlist == 0)
00594     MayDay::Error("Out of memory in List::copy ");
00595   return newlist;
00596 }
00597 
00598 template <class T>
00599 inline
00600 T&
00601 List<T>::firstElement () const
00602 {
00603   assert(head != 0);
00604   return head->val;
00605 }
00606 
00607 template <class T>
00608 inline
00609 T&
00610 List<T>::lastElement () const
00611 {
00612   assert(tail != 0);
00613   return tail->val;
00614 }
00615 
00616 template <class T>
00617 inline
00618 bool
00619 List<T>::isEmpty () const
00620 {
00621   return head == 0 && tail == 0;
00622 }
00623 
00624 template <class T>
00625 inline
00626 bool
00627 List<T>::isNotEmpty () const
00628 {
00629   return !isEmpty();
00630 }
00631 
00632 template <class T>
00633 inline
00634 void
00635 List<T>::removeFirst ()
00636 {
00637   remove(head);
00638 }
00639 
00640 template <class T>
00641 inline
00642 void
00643 List<T>::removeLast ()
00644 {
00645   remove(tail);
00646 }
00647 
00648 template <class T>
00649 inline
00650 const T&
00651 List<T>::operator[] (const ListIterator<T>& li) const
00652 {
00653   assert(li.p != 0);
00654   return li.p->val;
00655 }
00656 
00657 template <class T>
00658 inline
00659 T&
00660 List<T>::operator[] (const ListIterator<T>& li)
00661 {
00662   assert(li.p != 0);
00663   return li.p->val;
00664 }
00665 
00666 template <class T>
00667 inline
00668 void
00669 List<T>::replace (ListIterator<T>& li,
00670                   const T&         _val)
00671 {
00672   assert(li.p != 0);
00673   li.p->val = _val;
00674 }
00675 
00676 template <class T>
00677 inline
00678 void
00679 List<T>::addAfter (ListIterator<T>& lit,
00680                    const T&         val)
00681 {
00682   addAfter(lit.p, val);
00683 }
00684 
00685 template <class T>
00686 inline
00687 void
00688 List<T>::addBefore (ListIterator<T>& lit,
00689                     const T&         val)
00690 {
00691   addBefore(lit.p, val);
00692 }
00693 
00694 template <class T>
00695 inline
00696 ListIterator<T>
00697 List<T>::first () const
00698 {
00699   return ListIterator<T>(*this,head);
00700 }
00701 
00702 template <class T>
00703 inline
00704 ListIterator<T>
00705 List<T>::listIterator () const
00706 {
00707   return ListIterator<T>(*this,head);  //[NOTE: same as first()]
00708 }
00709 
00710 template <class T>
00711 inline
00712 ListIterator<T>
00713 List<T>::last () const
00714 {
00715   return ListIterator<T>(*this,tail);
00716 }
00717 
00718 #include "ListImplem.H"
00719 
00720 #endif /*_LIST_H_*/

Generated on Wed Jun 2 13:53:34 2004 for Chombo&INSwithParticles by doxygen 1.3.2