00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CLLIST_H
00021 #define __CLLIST_H
00022
00023 #include "cobject.h"
00024
00025
00040 class SIM_API cLinkedList : public cObject
00041 {
00042
00043 struct Elem
00044 {
00045 void *item;
00046 Elem *prev, *next;
00047 };
00048
00049 public:
00056 class Iterator
00057 {
00058 private:
00059 Elem *p;
00060
00061 public:
00067 Iterator(const cLinkedList& q, bool athead=true)
00068 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
00069
00073 void init(const cLinkedList& q, bool athead=true)
00074 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
00075
00079 void *operator()() const {return p->item;}
00080
00085 bool end() const {return (bool)(p==NULL);}
00086
00090 void *operator++(int) {if (!p) return NULL; Elem *t=p; p=p->next; return t->item;}
00091
00095 void *operator--(int) {if (!p) return NULL; Elem *t=p; p=p->prev; return t->item;}
00096 };
00097
00098 friend class Iterator;
00099
00100 private:
00101 Elem *headp, *tailp;
00102 int n;
00103
00104 VoidDelFunc delfunc;
00105 VoidDupFunc dupfunc;
00106 size_t itemsize;
00107
00108
00109
00110 protected:
00111
00112
00113
00114 Elem *find_llelem(void *item) const;
00115
00116
00117 void insbefore_llelem(Elem *p, void *item);
00118
00119
00120 void insafter_llelem(Elem *p, void *item);
00121
00122
00123 void *remove_llelem(Elem *p);
00124
00125 public:
00128
00135 cLinkedList(const cLinkedList& llist);
00136
00140 explicit cLinkedList(const char *name=NULL);
00141
00145 virtual ~cLinkedList();
00146
00154 cLinkedList& operator=(const cLinkedList& queue);
00156
00159
00165 virtual cPolymorphic *dup() const {return new cLinkedList(*this);}
00166
00171 virtual std::string info() const;
00172
00178 virtual void netPack(cCommBuffer *buffer);
00179
00185 virtual void netUnpack(cCommBuffer *buffer);
00187
00190
00209 void config( VoidDelFunc _delfunc, VoidDupFunc _dupfunc, size_t _itemsize=0);
00210
00214 void insert(void *item);
00215
00220 void insertBefore(void *where, void *item);
00221
00226 void insertAfter(void *where, void *item);
00227
00232 void *head() const {return n!=0 ? headp->item : NULL;}
00233
00238 void *tail() const {return n!=0 ? tailp->item : NULL;}
00239
00244 void *remove(void *item);
00245
00250 void *pop();
00251
00255 int length() const {return n;}
00256
00260 bool empty() const {return n==0;}
00261
00265 bool contains(void *item) const {return find_llelem(item)!=NULL;}
00266
00271 void clear();
00273 };
00274
00275
00276 #endif
00277