Main Page Modules Class Hierarchy Compound List File List Compound Members

# cqueue.h

    00001 //==========================================================================
    00002 //   CQUEUE.H  - header for
    00003 //                             OMNeT++
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 //
    00007 //  Declaration of the following classes:
    00008 //    cQueue        : (optionally) sorted queue of cObjects
    00009 //    cQueueIterator: walks along a queue
    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 __CQUEUE_H
    00021 #define __CQUEUE_H
    00022
    00023 #include "cobject.h"
    00024
    00025
    00047 class SIM_API cQueue : public cObject
    00048 {
    00049   private:
    00050     struct QElem
    00051     {
    00052         cObject *obj;
    00053         QElem *prev, *next;
    00054     };
    00055
    00056   public:
    00060     class Iterator
    00061     {
    00062       private:
    00063         QElem *p;
    00064
    00065       public:
    00071         Iterator(const cQueue& q, bool athead=true)
    00072                 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
    00073
    00077         void init(const cQueue& q, bool athead=true)
    00078                 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
    00079
    00083         cObject& operator[](int)  {return p ? *(p->obj) : *(cObject *)NULL;}
    00084
    00088         cObject *operator()()  {return p ? p->obj : NULL;}
    00089
    00093         bool end() const   {return (bool)(p==NULL);}
    00094
    00100         cObject *operator++(int)  {if (!p) return NULL; cObject *r=p->obj; p=p->next; return r;}
    00101
    00107         cObject *operator--(int)  {if (!p) return NULL; cObject *r=p->obj; p=p->prev; return r;}
    00108     };
    00109
    00110     friend class Iterator;
    00111
    00112   private:
    00113     QElem *headp, *tailp;           // inserting at head, removal at tail
    00114     int n;                          // number of items in queue
    00115     CompareFunc compare;            // compare function
    00116     bool asc;                       // order: true=ascending
    00117
    00118   protected:
    00119     // internal functions
    00120     QElem *find_qelem(cObject *obj) const;
    00121     void insbefore_qelem(QElem *p, cObject *obj);
    00122     void insafter_qelem(QElem *p, cObject *obj);
    00123     cObject *remove_qelem(QElem *p);
    00124
    00125   public:
    00128
    00134     cQueue(const cQueue& queue);
    00135
    00140     explicit cQueue(const char *name=NULL, CompareFunc cmp=NULL, bool a=false);
    00141
    00145     virtual ~cQueue();
    00146
    00154     cQueue& operator=(const cQueue& queue);
    00156
    00159
    00165     virtual cObject *dup() const  {return new cQueue(*this);}
    00166
    00171     virtual void info(char *buf);
    00172
    00177     virtual void forEach(ForeachFunc f);
    00178
    00184     virtual int netPack();
    00185
    00191     virtual int netUnpack();
    00193
    00196
    00201     virtual void setup(CompareFunc cmp, bool a=false);
    00202
    00207     virtual void insert(cObject *obj);
    00208
    00214     virtual void insertBefore(cObject *where, cObject *obj);
    00215
    00221     virtual void insertAfter(cObject *where, cObject *obj);
    00222
    00227     virtual cObject *remove(cObject *obj);
    00228
    00233     virtual cObject *pop();
    00234
    00239     virtual void clear();
    00241
    00244
    00249     virtual cObject *head() const;
    00250
    00255     virtual cObject *tail() const;
    00256
    00260     virtual int length() const;
    00261
    00265     bool empty() const {return length()==0;}
    00266
    00270     virtual bool contains(cObject *obj) const;
    00272 };
    00273
    00274 #endif
    00275