00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00021 #ifndef __LRT_LIST__
00022 #define __LRT_LIST__
00023
00024 #include "rtiterator.h"
00025 #include "rtdefs.h"
00026
00027 namespace lrt{
00028
00035 template<class T> class List
00036 {
00037 public:
00038 class Node
00039 {
00040 public:
00041 inline Node* getNext() const { return next; }
00042 inline Node* getPrev() const { return prev; }
00043 inline T& accessElement() { return element; }
00044 inline const T& accessElement() const { return element; }
00045 private:
00046 Node( const T &element ) : element(element), next(0), prev(0) {}
00047 ~Node() {}
00048
00049 inline void setNext( Node* nn) { next = nn; }
00050 inline void setPrev( Node* nn) { prev = nn; }
00051
00052 friend class List<T>;
00053 T element;
00054 Node *next;
00055 Node *prev;
00056 };
00057
00060 class IteratorImpl : public lrt::IIteratorImpl<T>
00061 {
00062 public:
00063 virtual ~IteratorImpl() {}
00064
00065
00066 virtual void goToNext() { if(element != 0) element = element->getNext(); }
00067 virtual void goToPrev() { if(element != 0) element = element->getPrev(); }
00068 virtual bool equals(const IIteratorImpl<T> *it) const
00069 {
00070 const IteratorImpl* i = LRT_DYNAMIC_CAST(const IteratorImpl*)(it);
00071 if(!i) return false;
00072 return (element == i->element);
00073 }
00074
00075 virtual bool hasNext() const { if(!element) return false; return (element->getNext() != 0); }
00076 virtual bool hasPrev() const { if(!element) return false; return (element->getPrev() != 0); }
00077 virtual bool hasElement() const { return (element != 0); }
00078
00079 virtual T& get() { return element->accessElement(); }
00080 virtual const T& get() const { return element->accessElement(); }
00081
00082 virtual bool canModify() { return canMod; }
00083 virtual void remove() { if(!element) return; Node* el = element; element = list->remove(el); }
00084
00085 private:
00086 friend class List<T>;
00087 virtual IIteratorImpl<T>* clone() const { return new IteratorImpl(list, element, canMod); }
00088
00089 IteratorImpl(List<T>* const l, Node* el, bool cm) : list(l), element(el), canMod(cm) {}
00090 List<T>* const list;
00091 Node *element;
00092 bool canMod;
00093 };
00094
00096 List();
00098 List(const List<T> &);
00100 List<T> &operator=(const List<T> &);
00102 ~List();
00103
00107 T& append( const T& elem );
00111 T& prepend( const T& elem );
00117 T& insertBefore( Iterator<T> &pos, const T& elem );
00123 T& insertAfter ( Iterator<T> &pos, const T& elem );
00126 inline void remove( Iterator<T> &pos );
00129 Node* remove(Node* element);
00130
00132 void clear();
00133
00135 inline int length() const;
00137 inline int getSize() const;
00138
00139 inline Node* first();
00140 inline const Node* first() const;
00141 inline Node* last();
00142 inline const Node* last() const;
00143
00147 inline Iterator<T> begin() const;
00151 inline Iterator<T> end() const;
00155 inline Iterator<T> begin();
00159 inline Iterator<T> end();
00160 private:
00161 friend class IteratorImpl;
00162 int len;
00163 Node *_begin, *_end;
00164 };
00165
00166
00167
00168
00169
00170
00171
00172
00178 template<class T> void deleteNode(List<T>& list, typename List<T>::Node*& n);
00179
00180 }
00181
00182 #include "rtlist.templ.cpp"
00183
00184 #endif
00185