00001 //========================================================================== 00002 // CMSGHEAP.H - header for 00003 // OMNeT++ 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-2003 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 //========================================================================== 00027 00035 class SIM_API cMessageHeap : public cObject 00036 { 00037 public: 00043 class Iterator 00044 { 00045 private: 00046 cMessageHeap *q; 00047 int pos; 00048 00049 public: 00053 Iterator(const cMessageHeap& mh) {q=const_cast<cMessageHeap*>(&mh);pos=1;} //FIXME: not correct? 00054 00058 void init(const cMessageHeap& mh) {q=const_cast<cMessageHeap*>(&mh);pos=1;} 00059 00063 cMessage *operator()() {return q->h[pos];} 00064 00069 cMessage *operator++(int) {return pos<=q->n ? q->h[++pos] : NULL;} 00070 00074 bool end() const {return (bool)(pos>q->n);} 00075 }; 00076 00077 friend class Iterator; 00078 00079 private: 00080 cMessage **h; // pointer to the 'heap' (h[0] always empty) 00081 int n; // number of elements in the heap 00082 int size; // size of allocated h array 00083 unsigned long insertcntr; // counts insertions 00084 00085 // internal 00086 void shiftup(int from=1); 00087 00088 public: 00091 00095 cMessageHeap(const cMessageHeap& msgq); 00096 00100 cMessageHeap(const char *name=NULL, int size=128); 00101 00105 virtual ~cMessageHeap(); 00106 00111 cMessageHeap& operator=(const cMessageHeap& msgqueue); 00113 00116 00121 virtual cObject *dup() const {return new cMessageHeap(*this);} 00122 00127 virtual void info(char *buf); 00128 00133 virtual void forEach(ForeachFunc f); 00134 00135 // no netPack() and netUnpack() 00137 00140 00144 void insert(cMessage *event); 00145 00149 cMessage *peekFirst() const; 00150 00155 cMessage *getFirst(); 00156 00160 cMessage *get(cMessage *event); 00161 00166 void sort(); 00167 00171 void clear(); 00172 00176 int length() const {return n;} 00177 00181 bool empty() const {return n==0;} 00183 }; 00184 00185 #endif 00186