cmsgheap.h

00001 //==========================================================================
00002 //  CMSGHEAP.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cMessageHeap : future event set, implemented as heap
00009 //
00010 //==========================================================================
00011 
00012 /*--------------------------------------------------------------*
00013   Copyright (C) 1992-2005 Andras Varga
00014 
00015   This file is distributed WITHOUT ANY WARRANTY. See the file
00016   `license' for details on this and other legal matters.
00017 *--------------------------------------------------------------*/
00018 
00019 #ifndef __CMSGHEAP_H
00020 #define __CMSGHEAP_H
00021 
00022 #include "cobject.h"
00023 
00024 class cMessage;
00025 
00026 
00034 class SIM_API cMessageHeap : public cObject
00035 {
00036   public:
00042     class Iterator
00043     {
00044       private:
00045         cMessageHeap *q;
00046         int pos;
00047 
00048       public:
00052         Iterator(const cMessageHeap& mh)  {q=const_cast<cMessageHeap*>(&mh);pos=1;}
00053 
00057         void init(const cMessageHeap& mh) {q=const_cast<cMessageHeap*>(&mh);pos=1;}
00058 
00062         cMessage *operator()()      {return q->h[pos];}
00063 
00068         cMessage *operator++(int)   {return pos<=q->n ? q->h[++pos] : NULL;}
00069 
00073         bool end() const   {return (bool)(pos>q->n);}
00074     };
00075 
00076     friend class Iterator;
00077 
00078   private:
00079     cMessage **h;             // pointer to the 'heap'  (h[0] always empty)
00080     int n;                    // number of elements in the heap
00081     int size;                 // size of allocated h array
00082     unsigned long insertcntr; // counts insertions
00083 
00084     // internal
00085     void shiftup(int from=1);
00086 
00087   public:
00090 
00094     cMessageHeap(const cMessageHeap& msgq);
00095 
00099     cMessageHeap(const char *name=NULL, int size=128);
00100 
00104     virtual ~cMessageHeap();
00105 
00110     cMessageHeap& operator=(const cMessageHeap& msgqueue);
00112 
00115 
00120     virtual cPolymorphic *dup() const  {return new cMessageHeap(*this);}
00121 
00126     virtual std::string info() const;
00127 
00132     virtual void forEachChild(cVisitor *v);
00133 
00134     // no netPack() and netUnpack()
00136 
00139 
00143     void insert(cMessage *event);
00144 
00149     cMessage *peekFirst() const;
00150 
00155     cMessage *getFirst();
00156 
00161     cMessage *get(cMessage *event);
00162 
00169     cMessage *peek(int m);
00170 
00175     void sort();
00176 
00180     void clear();
00181 
00185     int length() const {return n;}
00186 
00190     bool empty() const {return n==0;}
00192 };
00193 
00194 #endif
00195 

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