Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

cllist.h

00001 //==========================================================================
00002 //   CLLIST.H  - header for
00003 //                             OMNeT++
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-2001 Andras Varga
00015   Technical University of Budapest, Dept. of Telecommunications,
00016   Stoczek u.2, H-1111 Budapest, Hungary.
00017 
00018   This file is distributed WITHOUT ANY WARRANTY. See the file
00019   `license' for details on this and other legal matters.
00020 *--------------------------------------------------------------*/
00021 
00022 #ifndef __CLLIST_H
00023 #define __CLLIST_H
00024 
00025 #include "cobject.h"
00026 
00027 //=== classes declared here
00028 class  cLinkedList;
00029 class  cLinkedListIterator;
00030 
00031 //==========================================================================
00032 
00033 //
00034 // Used internally by cLinkedList and cLinkedListIterator.
00035 //
00036 struct sLLElem
00037 {
00038     void *item;
00039     sLLElem *prev, *next;
00040 };
00041 
00042 //-------------------------------------------------------------------------
00043 
00054 class SIM_API cLinkedList : public cObject
00055 {
00056     friend class cLinkedListIterator;
00057 
00058   private:
00059     sLLElem *headp, *tailp;   // inserting at head, removal at tail
00060     int n;                    // number of items in list
00061 
00062     VoidDelFunc delfunc;      // user func to free up item (NULL-->delete)
00063     VoidDupFunc dupfunc;      // user func to dupl. item (NULL-->new+memcpy)
00064     size_t itemsize;          // used in making shallow copy if dupfunc==0
00065                               // if both dupfunc and itemsize are 0, we do
00066                               // no memory management (we treat pointers as
00067                               // mere pointers)
00068   protected:
00069     // internal use.
00070     // if both dupfunc and itemsize are 0, we do no memory management
00071     // (we treat pointers as mere pointers)
00072     sLLElem *find_llelem(void *item) const;
00073 
00074     // internal use
00075     void insbefore_llelem(sLLElem *p, void *item);
00076 
00077     // internal use
00078     void insafter_llelem(sLLElem *p, void *item);
00079 
00080     // internal use
00081     void *remove_llelem(sLLElem *p);
00082 
00083   public:
00086 
00093     cLinkedList(const cLinkedList& llist);
00094 
00098     explicit cLinkedList(const char *name=NULL);
00099 
00103     virtual ~cLinkedList();
00104 
00112     cLinkedList& operator=(const cLinkedList& queue);
00114 
00117 
00121     virtual const char *className() const  {return "cLinkedList";}
00122 
00128     virtual cObject *dup() const  {return new cLinkedList(*this);}
00129 
00134     virtual void info(char *buf);
00135 
00140     virtual const char *inspectorFactoryName() const {return "cLinkedListIFC";}
00141 
00147     virtual int netPack();
00148 
00154     virtual int netUnpack();
00156 
00159 
00178     void config( VoidDelFunc _delfunc, VoidDupFunc _dupfunc, size_t _itemsize=0);
00179 
00183     void insert(void *item);
00184 
00189     void insertBefore(void *where, void *item);
00190 
00195     void insertAfter(void *where, void *item);
00196 
00201     void *head() const  {return n!=0 ? headp->item : NULL;}
00202 
00207     void *tail() const  {return n!=0 ? tailp->item : NULL;}
00208 
00213     void *remove(void *item);
00214 
00219     void *pop();
00220 
00224     int length() const {return n;}
00225 
00229     bool empty() const {return n==0;}
00230 
00234     bool contains(void *item) const  {return find_llelem(item)!=NULL;}
00235 
00240     void clear();
00242 };
00243 
00244 //==========================================================================
00245 
00256 class SIM_API cLinkedListIterator
00257 {
00258   private:
00259     sLLElem *p;
00260 
00261   public:
00267     cLinkedListIterator(const cLinkedList& q, int athead=1)  //FIXME: make bool!
00268             {p=&q ? (athead ? q.headp : q.tailp) : NO(sLLElem);}
00269 
00273     void init(const cLinkedList& q, int athead=1)
00274             {p=&q ? (athead ? q.headp : q.tailp) : NO(sLLElem);}
00275 
00279     void *operator()() const        {return p->item;}
00280 
00285     bool end() const                {return (bool)(p==NULL);}
00286 
00290     void *operator++(int)  {sLLElem *t=p; if(p) p=p->next; return t->item;}
00291 
00295     void *operator--(int)  {sLLElem *t=p; if(p) p=p->prev; return t->item;}
00296 };
00297 
00298 #endif
00299 

Generated at Sat May 4 15:45:48 2002 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001