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-2003 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
    00037 class SIM_API cLinkedList : public cObject
    00038 {
    00039     // a list elem
    00040     struct Elem
    00041     {
    00042         void *item;
    00043         Elem *prev, *next;
    00044     };
    00045
    00046   public:
    00053     class Iterator
    00054     {
    00055       private:
    00056         Elem *p;
    00057
    00058       public:
    00064         Iterator(const cLinkedList& q, bool athead=true)
    00065                 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
    00066
    00070         void init(const cLinkedList& q, bool athead=true)
    00071                 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
    00072
    00076         void *operator()() const  {return p->item;}
    00077
    00082         bool end() const   {return (bool)(p==NULL);}
    00083
    00087         void *operator++(int)  {if (!p) return NULL; Elem *t=p; p=p->next; return t->item;}
    00088
    00092         void *operator--(int)  {if (!p) return NULL; Elem *t=p; p=p->prev; return t->item;}
    00093     };
    00094
    00095     friend class Iterator;
    00096
    00097   private:
    00098     Elem *headp, *tailp;      // inserting at head, removal at tail
    00099     int n;                    // number of items in list
    00100
    00101     VoidDelFunc delfunc;      // user func to free up item (NULL-->delete)
    00102     VoidDupFunc dupfunc;      // user func to dupl. item (NULL-->new+memcpy)
    00103     size_t itemsize;          // used in making shallow copy if dupfunc==0
    00104                               // if both dupfunc and itemsize are 0, we do
    00105                               // no memory management (we treat pointers as
    00106                               // mere pointers)
    00107   protected:
    00108     // internal use.
    00109     // if both dupfunc and itemsize are 0, we do no memory management
    00110     // (we treat pointers as mere pointers)
    00111     Elem *find_llelem(void *item) const;
    00112
    00113     // internal use
    00114     void insbefore_llelem(Elem *p, void *item);
    00115
    00116     // internal use
    00117     void insafter_llelem(Elem *p, void *item);
    00118
    00119     // internal use
    00120     void *remove_llelem(Elem *p);
    00121
    00122   public:
    00125
    00132     cLinkedList(const cLinkedList& llist);
    00133
    00137     explicit cLinkedList(const char *name=NULL);
    00138
    00142     virtual ~cLinkedList();
    00143
    00151     cLinkedList& operator=(const cLinkedList& queue);
    00153
    00156
    00162     virtual cObject *dup() const  {return new cLinkedList(*this);}
    00163
    00168     virtual void info(char *buf);
    00169
    00175     virtual int netPack();
    00176
    00182     virtual int netUnpack();
    00184
    00187
    00206     void config( VoidDelFunc _delfunc, VoidDupFunc _dupfunc, size_t _itemsize=0);
    00207
    00211     void insert(void *item);
    00212
    00217     void insertBefore(void *where, void *item);
    00218
    00223     void insertAfter(void *where, void *item);
    00224
    00229     void *head() const  {return n!=0 ? headp->item : NULL;}
    00230
    00235     void *tail() const  {return n!=0 ? tailp->item : NULL;}
    00236
    00241     void *remove(void *item);
    00242
    00247     void *pop();
    00248
    00252     int length() const {return n;}
    00253
    00257     bool empty() const {return n==0;}
    00258
    00262     bool contains(void *item) const  {return find_llelem(item)!=NULL;}
    00263
    00268     void clear();
    00270 };
    00271
    00272
    00273 #endif
    00274