Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

List.H

Go to the documentation of this file.
00001 /*   _______              __
00002     / ___/ /  ___  __ _  / /  ___
00003    / /__/ _ \/ _ \/  V \/ _ \/ _ \
00004    \___/_//_/\___/_/_/_/_.__/\___/
00005 */
00006 // CHOMBO Copyright (c) 2000-2004, The Regents of the University of
00007 // California, through Lawrence Berkeley National Laboratory (subject to
00008 // receipt of any required approvals from U.S. Dept. of Energy).  All
00009 // rights reserved.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are met:
00013 //
00014 // (1) Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 // (2) Redistributions in binary form must reproduce the above copyright
00017 // notice, this list of conditions and the following disclaimer in the
00018 // documentation and/or other materials provided with the distribution.
00019 // (3) Neither the name of Lawrence Berkeley National Laboratory, U.S.
00020 // Dept. of Energy nor the names of its contributors may be used to endorse
00021 // or promote products derived from this software without specific prior
00022 // written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035 //
00036 // You are under no obligation whatsoever to provide any bug fixes,
00037 // patches, or upgrades to the features, functionality or performance of
00038 // the source code ("Enhancements") to anyone; however, if you choose to
00039 // make your Enhancements available either publicly, or directly to
00040 // Lawrence Berkeley National Laboratory, without imposing a separate
00041 // written license agreement for such Enhancements, then you hereby grant
00042 // the following license: a non-exclusive, royalty-free perpetual license
00043 // to install, use, modify, prepare derivative works, incorporate into
00044 // other computer software, distribute, and sublicense such Enhancements or
00045 // derivative works thereof, in binary and source code form.
00046 //
00047 // TRADEMARKS. Product and company names mentioned herein may be the
00048 // trademarks of their respective owners.  Any rights not expressly granted
00049 // herein are reserved.
00050 //
00051 
00052 #ifndef _LIST_H_
00053 #define _LIST_H_
00054 
00055 #include "MayDay.H"
00056 #include "Pool.H"
00057 
00058 template <class T> class ListLink;
00059 template <class T> class ListIterator;
00060 template <class T> class List;
00061 
00062 // this is so this internal class doesn't appear in doxygen documentation
00063 #ifndef DOXYGEN
00064 
00065 // Internal helper class for the List class
00066 template <class T>
00067 class ListLink
00068 {
00069 private:
00070     friend class List<T>;
00071     friend class ListIterator<T>;
00072 
00073     ListLink (const T&     _val,
00074               ListLink<T>* _pre,
00075               ListLink<T>* _suc)
00076       :
00077       val(_val),
00078       pre(_pre),
00079       suc(_suc)
00080     {}
00081 
00082 private:
00083     T            val;
00084     ListLink<T>* pre;
00085     ListLink<T>* suc;
00086 };
00087 
00088 #endif // doxygen
00089 
00091 
00095 template <class T>
00096 class ListIterator
00097 {
00098 public:
00099 
00101   inline ListIterator (const List<T>& aList);
00102 
00104   inline ListIterator (const ListIterator<T>& rhs);
00105 
00107   inline void rewind ();
00108 
00110 
00113   inline void begin();
00114 
00116   inline const T& operator() () const;
00117 
00118   inline T& operator() () ;
00119 
00121   inline const T& operator* () const;
00122 
00124 
00130   inline operator bool () const;
00131 
00133 
00136   inline bool ok() const ;
00137 
00139   inline bool operator! () const;
00140 
00142   inline const T& value () const;
00143 
00145   const T& value ();
00146 
00148 
00153   inline ListIterator<T>& operator++ ();
00154 
00156 
00161   inline ListIterator<T>& operator-- ();
00162 
00164 
00169   inline ListIterator<T> operator-- (int);
00170 
00172 
00177   inline ListIterator<T> operator++ (int);
00178 
00180 
00184   inline bool operator== (const ListIterator<T>&) const;
00185 
00187   inline bool operator!= (const ListIterator<T>&) const;
00188 
00189 protected:
00190 
00194   inline ListIterator (const List<T>& _list,
00195                        ListLink<T>*   _p);
00196 
00200   const List<T>& list;
00201 
00205   ListLink<T>* p;
00206 
00207 private:
00208     friend class List<T>;
00209     //
00210     // These are disallowed.
00211     //
00212     ListIterator ();
00213     ListIterator<T>& operator= (const ListIterator<T>&);
00214 };
00215 
00217 
00246 template <class T>
00247 class List
00248 {
00249 public:
00250 
00252   inline List ();
00253 
00255   List (const List<T>& rhs);
00256 
00258   List<T>& operator= (const List<T>& rhs);
00259 
00261   inline ~List();
00262 
00264   inline void prepend (const T& value);
00265 
00267   inline void append (const T& value);
00268 
00270   void add (const T& value);
00271 
00273   void join (const List<T>& src);
00274 
00276 
00282   void catenate (List<T>& src);
00283 
00285   void clear ();
00286 
00288 
00292   inline List<T>* copy () const;
00293 
00295   inline T& firstElement () const;
00296 
00298   inline T& lastElement () const;
00299 
00301 
00304   bool includes (const T& value) const;
00305 
00307 
00312   bool operator== (const List<T>& rhs) const;
00313 
00315   bool operator!= (const List<T>& rhs) const;
00316 
00318   inline bool isEmpty () const;
00319 
00321   inline bool isNotEmpty () const;
00322 
00324   int length () const;
00325 
00327   inline void removeFirst ();
00328 
00330   inline void removeLast ();
00331 
00333   inline const T& operator[] (const ListIterator<T>& li) const;
00334 
00336   inline T& operator[] (const ListIterator<T>& li);
00337 
00339   void remove (const T& value);
00340 
00342   void remove (const List<T>& lst);
00343 
00345   void remove (ListIterator<T>& lit);
00346 
00348   void transfer(ListIterator<T>& lit);
00349 
00351   inline void replace (ListIterator<T>& li,
00352                        const T&         val);
00353 
00355   inline void addAfter (ListIterator<T>& lit,
00356                         const T&         val);
00357 
00359   inline void addBefore (ListIterator<T>& lit,
00360                          const T&         val);
00361 
00363   inline ListIterator<T> listIterator () const;
00364 
00366   inline ListIterator<T> first () const;
00367 
00369   inline ListIterator<T> last () const;
00370 
00371 protected:
00372 
00376   void remove (ListLink<T> *ln);
00377 
00378   void removeLink(ListLink<T> *ln);
00379 
00383   ListLink<T>* addBefore (ListLink<T>* ln,
00384                           const T&     val);
00385 
00389   ListLink<T>* addAfter (ListLink<T>* ln,
00390                          const T&     val);
00391 
00395   ListLink<T>* head;
00396 
00400   ListLink<T>* tail;
00401 
00405   friend class ListIterator<T>;
00406 
00411   static Pool linkPool;
00412 };
00413 
00414 //
00415 // Inlines.
00416 //
00417 
00418 //
00419 // The ListIterator stuff.
00420 //
00421 
00422 template <class T>
00423 inline
00424 ListIterator<T>::ListIterator (const List<T>& _list,
00425                                ListLink<T>*   _p)
00426   :
00427   list(_list),
00428   p(_p)
00429 {}
00430 
00431 template <class T>
00432 inline
00433 ListIterator<T>::ListIterator (const List<T>& aList)
00434   : list(aList)
00435 {
00436   p = list.head;
00437 }
00438 
00439 template <class T>
00440 inline
00441 ListIterator<T>::ListIterator (const ListIterator<T>& li)
00442   :
00443   list(li.list),
00444   p(li.p)
00445 {}
00446 
00447 template <class T>
00448 inline
00449 void
00450 ListIterator<T>::rewind ()
00451 {
00452   p = list.head;
00453 }
00454 
00455 template <class T>
00456 inline
00457 void
00458 ListIterator<T>::begin ()
00459 {
00460   p = list.head;
00461 }
00462 
00463 template <class T>
00464 inline
00465 const T&
00466 ListIterator<T>::operator() () const
00467 {
00468  CH_assert(p != 0);
00469   return p->val;
00470 }
00471 
00472 template <class T>
00473 inline
00474 T&
00475 ListIterator<T>::operator() ()
00476 {
00477  CH_assert(p != 0);
00478   return p->val;
00479 }
00480 
00481 template <class T>
00482 inline
00483 const T&
00484 ListIterator<T>::operator* () const
00485 {
00486  CH_assert(p != 0);
00487   return p->val;
00488 }
00489 
00490 template <class T>
00491 inline
00492 bool
00493 ListIterator<T>::ok() const
00494 {
00495   return p != 0 ? true : false;
00496 }
00497 
00498 template <class T>
00499 inline
00500 ListIterator<T>::operator bool () const
00501 {
00502   return ok() ;
00503 }
00504 
00505 template <class T>
00506 inline
00507 bool
00508 ListIterator<T>::operator! () const
00509 {
00510   return p == 0 ? true : false;
00511 }
00512 
00513 template <class T>
00514 inline
00515 const T&
00516 ListIterator<T>::value () const
00517 {
00518  CH_assert(p != 0);
00519   return p->val;
00520 }
00521 
00522 template <class T>
00523 inline
00524 ListIterator<T>&
00525 ListIterator<T>::operator++ ()
00526 {
00527   if (p)
00528     p = p->suc;
00529   return *this;
00530 }
00531 
00532 template <class T>
00533 inline
00534 ListIterator<T>&
00535 ListIterator<T>::operator-- ()
00536 {
00537   if (p)
00538     p = p->pre;
00539   return *this;
00540 }
00541 
00542 template <class T>
00543 inline
00544 ListIterator<T>
00545 ListIterator<T>::operator++ (int)
00546 {
00547   const ListIterator<T> li = *this;
00548   ++(*this);
00549   return li;
00550 }
00551 
00552 template <class T>
00553 inline
00554 ListIterator<T>
00555 ListIterator<T>::operator-- (int)
00556 {
00557   const ListIterator<T> li = *this;
00558   --(*this);
00559   return li;
00560 }
00561 
00562 template <class T>
00563 inline
00564 bool
00565 ListIterator<T>::operator== (const ListIterator<T>& _li) const
00566 {
00567   return (&list == &_li.list && p == _li.p) ? true : false;
00568 }
00569 
00570 template <class T>
00571 inline
00572 bool
00573 ListIterator<T>::operator!= (const ListIterator<T>& _li) const
00574 {
00575   return ! ListIterator<T>::operator==(_li);
00576 }
00577 
00578 //
00579 // List stuff.
00580 //
00581 
00582 template <class T>
00583 inline
00584 List<T>::List ()
00585   :
00586   head(0),
00587   tail(0)
00588 {}
00589 
00590 template <class T>
00591 inline
00592 List<T>::~List ()
00593 {
00594   clear();
00595 }
00596 
00597 template <class T>
00598 inline
00599 void
00600 List<T>::prepend (const T& value)
00601 {
00602   addBefore(head, value);
00603 }
00604 
00605 template <class T>
00606 inline
00607 void
00608 List<T>::append (const T& value)
00609 {
00610   addAfter(tail, value);
00611 }
00612 
00613 template <class T>
00614 inline
00615 List<T>*
00616 List<T>::copy () const
00617 {
00618   List<T>* newlist = new List<T>(*this);
00619   if (newlist == 0)
00620     MayDay::Error("Out of memory in List::copy ");
00621   return newlist;
00622 }
00623 
00624 template <class T>
00625 inline
00626 T&
00627 List<T>::firstElement () const
00628 {
00629  CH_assert(head != 0);
00630   return head->val;
00631 }
00632 
00633 template <class T>
00634 inline
00635 T&
00636 List<T>::lastElement () const
00637 {
00638  CH_assert(tail != 0);
00639   return tail->val;
00640 }
00641 
00642 template <class T>
00643 inline
00644 bool
00645 List<T>::isEmpty () const
00646 {
00647   return head == 0 && tail == 0;
00648 }
00649 
00650 template <class T>
00651 inline
00652 bool
00653 List<T>::isNotEmpty () const
00654 {
00655   return !isEmpty();
00656 }
00657 
00658 template <class T>
00659 inline
00660 void
00661 List<T>::removeFirst ()
00662 {
00663   remove(head);
00664 }
00665 
00666 template <class T>
00667 inline
00668 void
00669 List<T>::removeLast ()
00670 {
00671   remove(tail);
00672 }
00673 
00674 template <class T>
00675 inline
00676 const T&
00677 List<T>::operator[] (const ListIterator<T>& li) const
00678 {
00679  CH_assert(li.p != 0);
00680   return li.p->val;
00681 }
00682 
00683 template <class T>
00684 inline
00685 T&
00686 List<T>::operator[] (const ListIterator<T>& li)
00687 {
00688  CH_assert(li.p != 0);
00689   return li.p->val;
00690 }
00691 
00692 template <class T>
00693 inline
00694 void
00695 List<T>::replace (ListIterator<T>& li,
00696                   const T&         _val)
00697 {
00698  CH_assert(li.p != 0);
00699   li.p->val = _val;
00700 }
00701 
00702 template <class T>
00703 inline
00704 void
00705 List<T>::addAfter (ListIterator<T>& lit,
00706                    const T&         val)
00707 {
00708   addAfter(lit.p, val);
00709 }
00710 
00711 template <class T>
00712 inline
00713 void
00714 List<T>::addBefore (ListIterator<T>& lit,
00715                     const T&         val)
00716 {
00717   addBefore(lit.p, val);
00718 }
00719 
00720 template <class T>
00721 inline
00722 ListIterator<T>
00723 List<T>::first () const
00724 {
00725   return ListIterator<T>(*this,head);
00726 }
00727 
00728 template <class T>
00729 inline
00730 ListIterator<T>
00731 List<T>::listIterator () const
00732 {
00733   return ListIterator<T>(*this,head);  //[NOTE: same as first()]
00734 }
00735 
00736 template <class T>
00737 inline
00738 ListIterator<T>
00739 List<T>::last () const
00740 {
00741   return ListIterator<T>(*this,tail);
00742 }
00743 
00744 #include "ListImplem.H"
00745 
00746 #endif /*_LIST_H_*/

Generated on Wed Oct 5 12:08:14 2005 for Chombo&AMRIdealMHD by  doxygen 1.4.1