Generated on Mon Jun 23 16:24:56 2008 for BIU-1.7.0 by doxygen 1.5.1

src/biu/VirtualList.hh

Go to the documentation of this file.
00001 /************************************************************
00002  * 
00003  * Implementation of virtual lists that can be iterated STL-ish
00004  *
00005  * @author Sebastian Will <will@@informatik.uni-freiburg.de>
00006  *
00007  * Copyright Sebastian Will, 2006
00008  *
00009  ************************************************************/
00010 
00011 #ifndef VIRTUAL_LIST_H
00012 #define VIRTUAL_LIST_H
00013 
00014 namespace biu {
00015 
00016     //------------------------------------------------------------
00017     // CLASS VirtualList
00018     
00041     template<class T> class VirtualList {
00042 
00043     public:
00044         //------------------------------------------------------------
00045         // Nested CLASS ItState
00046         
00049         class ItState { 
00050             friend class VirtualList<T>;
00051         protected:
00052             ItState() {}
00053         public:
00054             ItState(const ItState &) {}
00055             virtual ~ItState();
00056             
00058             virtual ItState * clone() const {return new ItState(*this);}
00059         };
00060   
00061         //------------------------------------------------------------
00062         // Nested CLASS Iterator
00063         
00074         class Iterator {
00075             friend class VirtualList<T>;
00076         public:
00077             const VirtualList<T> *parent; 
00078             ItState *itstate;             
00079             T *elem;                      
00080         private:
00084             Iterator( const VirtualList<T> *parent_,
00085                       ItState *itstate_,
00086                       T* elem_ )
00087                 : parent(parent_), itstate(itstate_), elem(elem_) {}
00088 
00089         public:
00092             Iterator(const Iterator &it)
00093                 : parent(it.parent),
00094                   itstate(it.itstate->clone()),
00095                   elem(it.elem->clone())
00096                 {}
00097             
00098             virtual ~Iterator();
00099             
00101             const Iterator &operator ++() {
00102                 T* nextElem = parent->next(itstate,elem);
00103                 if (0==nextElem && 0!=elem) delete elem;
00104                 elem=nextElem;
00105                 return *this;
00106             }
00107         
00111             bool operator != (const Iterator &it) const {
00112                 return this->elem!=0 || it.elem!=0; 
00113             }
00114             
00117             Iterator &operator =(const Iterator &it) {
00118                 parent=it.parent;
00119             
00120                 if (itstate) delete itstate;
00121                 itstate=(it.itstate)?it.itstate->clone():0;
00122             
00123                 if (elem) delete elem;
00124                 elem=(it.elem)?it.elem->clone():0;
00125             
00126                 return *this;
00127             }
00128 
00130             const T& operator *() const {
00131                 return *elem;
00132             }
00133 
00135             const T* operator ->() const {
00136                 return elem;
00137             }
00138 
00139         };
00140       
00141       VirtualList() {}
00142       virtual ~VirtualList() {}
00143 
00145         Iterator begin() const {
00146             ItState *itstate = 0;
00147         
00148             T *f = first(&itstate);
00149         
00150             return Iterator(this,itstate,f);
00151         }
00152   
00155         Iterator end() const {
00156             return Iterator(this,0,0);
00157         }
00158 
00159     protected:
00160         // ------------------------------------------------------------
00161         // abstract methods that define the iteration method
00162         //
00163 
00170         virtual T* first(ItState **itstate) const = 0;
00171   
00172         // --------------------
00180         virtual T* next(ItState *itstate, T* const elem) const = 0;
00181     
00182     };
00183 
00184     template<class T>
00185     VirtualList<T>::ItState::~ItState() {}
00186 
00187     template<class T>
00188     VirtualList<T>::Iterator::~Iterator() {
00189         if (elem) delete elem;
00190         if (itstate) delete itstate;
00191     }
00192 
00193 } // namespace biu
00194 
00195 #endif // VIRTUAL_LIST_H