cmessageheap.h

00001 //==========================================================================
00002 //  CMESSAGEHEAP.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-2008 Andras Varga
00014   Copyright (C) 2006-2008 OpenSim Ltd.
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 __CMESSAGEHEAP_H
00021 #define __CMESSAGEHEAP_H
00022 
00023 #include "cownedobject.h"
00024 
00025 NAMESPACE_BEGIN
00026 
00027 class cMessage;
00028 
00029 
00037 class SIM_API cMessageHeap : public cOwnedObject
00038 {
00039   public:
00045     class Iterator
00046     {
00047       private:
00048         cMessageHeap *q;
00049         int pos;
00050 
00051       public:
00055         Iterator(const cMessageHeap& mh)  {q=const_cast<cMessageHeap*>(&mh);pos=0;}
00056 
00060         void init(const cMessageHeap& mh) {q=const_cast<cMessageHeap*>(&mh);pos=0;}
00061 
00065         cMessage *operator()()  {return q->peek(pos);}
00066 
00071         cMessage *operator++(int)  {return end() ? NULL : q->peek(pos++);}
00072 
00076         bool end() const  {return pos>=q->getLength();}
00077     };
00078 
00079     friend class Iterator;
00080 
00081   private:
00082     // heap data structure
00083     cMessage **h;             // pointer to the 'heap'  (h[0] always empty)
00084     int n;                    // number of elements in the heap
00085     int size;                 // size of allocated h array
00086     unsigned long insertcntr; // counts insertions
00087 
00088     // circular buffer for events scheduled for the current simtime (quite frequent)
00089     cMessage **cb;            // size of the circular buffer
00090     int cbsize;               // always power of 2
00091     int cbhead, cbtail;       // cbhead is inclusive, cbtail is exclusive
00092 
00093   private:
00094     void copy(const cMessageHeap& other);
00095 
00096     // internal: restore heap
00097     void shiftup(int from=1);
00098 
00099     int cblength() const  {return (cbtail-cbhead) & (cbsize-1);}
00100     cMessage *cbget(int k)  {return cb[(cbhead+k) & (cbsize-1)];}
00101 
00102   public:
00105 
00109     cMessageHeap(const cMessageHeap& msgq);
00110 
00114     cMessageHeap(const char *name=NULL, int size=128);
00115 
00119     virtual ~cMessageHeap();
00120 
00125     cMessageHeap& operator=(const cMessageHeap& msgqueue);
00127 
00130 
00135     virtual cMessageHeap *dup() const  {return new cMessageHeap(*this);}
00136 
00141     virtual std::string info() const;
00142 
00147     virtual void forEachChild(cVisitor *v);
00148 
00149     // no parsimPack() and parsimUnpack()
00151 
00154 
00158     void insert(cMessage *event);
00159 
00164     cMessage *peekFirst() const;
00165 
00172     cMessage *peek(int m);
00173 
00178     cMessage *removeFirst();
00179 
00184     cMessage *remove(cMessage *event);
00185 
00190     void sort();
00191 
00195     void clear();
00196 
00200     int getLength() const {return cblength() + n;}
00201 
00205     bool isEmpty() const {return cbhead==cbtail && n==0;}
00206 
00210     int length() const {return getLength();}
00211 
00215     bool empty() const {return isEmpty();}
00217 };
00218 
00219 NAMESPACE_END
00220 
00221 
00222 #endif
00223 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3