cllist.h

00001 //==========================================================================
00002 //  CLLIST.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cLinkedList        : linked list of pointers (cQueue-like interface)
00009 //    cLinkedListIterator: walks along a linked list
00010 //
00011 //==========================================================================
00012 
00013 /*--------------------------------------------------------------*
00014   Copyright (C) 1992-2005 Andras Varga
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
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     // a list elem
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;      // inserting at head, removal at tail
00102     int n;                    // number of items in list
00103 
00104     VoidDelFunc delfunc;      // user func to free up item (NULL-->delete)
00105     VoidDupFunc dupfunc;      // user func to dupl. item (NULL-->new+memcpy)
00106     size_t itemsize;          // used in making shallow copy if dupfunc==0
00107                               // if both dupfunc and itemsize are 0, we do
00108                               // no memory management (we treat pointers as
00109                               // mere pointers)
00110   protected:
00111     // internal use.
00112     // if both dupfunc and itemsize are 0, we do no memory management
00113     // (we treat pointers as mere pointers)
00114     Elem *find_llelem(void *item) const;
00115 
00116     // internal use
00117     void insbefore_llelem(Elem *p, void *item);
00118 
00119     // internal use
00120     void insafter_llelem(Elem *p, void *item);
00121 
00122     // internal use
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 

Generated on Sat Oct 21 17:47:55 2006 for OMNeT++/OMNEST Simulation Library by  doxygen 1.4.6