OMNeT++ Simulation Library  6.0.3
ceventheap.h
1 //==========================================================================
2 // CEVENTHEAP.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CEVENTHEAP_H
17 #define __OMNETPP_CEVENTHEAP_H
18 
19 #include "cfutureeventset.h"
20 
21 namespace omnetpp {
22 
35 class SIM_API cEventHeap : public cFutureEventSet
36 {
37  private:
38  // heap data structure
39  cEvent **heap = nullptr; // heap array (heap[0] always empty)
40  int heapLength = 0; // number of elements on the heap
41  int heapCapacity = 0; // allocated size of the heap[] array
42  eventnumber_t insertCount = 0; // counts insertions; needed because heap's insert is not stable (does not keep order)
43 
44  // circular buffer for events scheduled for the current simtime (quite frequent); acts as FIFO
45  cEvent **cb = nullptr; // size of the circular buffer
46  int cbsize = 4; // always power of 2
47  int cbhead = 0, cbtail = 0; // cbhead is inclusive, cbtail is exclusive
48  bool useCb = true; // for disabling cb
49 
50  private:
51  void copy(const cEventHeap& other);
52 
53  // internal: restore heap
54  void shiftup(int from=1);
55 
56  int cblength() const {return (cbtail-cbhead) & (cbsize-1);}
57  cEvent *cbget(int k) {return cb[(cbhead+k) & (cbsize-1)];}
58  void cbgrow();
59 
60  void heapInsert(cEvent *event);
61  void cbInsert(cEvent *event);
62  void flushCb();
63 
64  public:
65  // internal:
66  bool getUseCb() const {return useCb;}
67  void setUseCb(bool b) {ASSERT(cbhead==cbtail); useCb = b;}
68 
69  // utility function for checking heap sanity
70  virtual void checkHeap();
71 
72  public:
75 
79  cEventHeap(const cEventHeap& msgq);
80 
84  cEventHeap(const char *name=nullptr, int initialCapacity=128);
85 
89  virtual ~cEventHeap();
90 
95  cEventHeap& operator=(const cEventHeap& other);
97 
100 
105  virtual cEventHeap *dup() const override {return new cEventHeap(*this);}
106 
111  virtual std::string str() const override;
112 
117  virtual void forEachChild(cVisitor *v) override;
118 
119  // no parsimPack() and parsimUnpack()
121 
127  virtual void insert(cEvent *event) override;
128 
133  virtual cEvent *peekFirst() const override;
134 
139  virtual cEvent *removeFirst() override;
140 
144  virtual void putBackFirst(cEvent *event) override;
145 
150  virtual cEvent *remove(cEvent *event) override;
151 
155  virtual bool isEmpty() const override {return cbhead==cbtail && heapLength==0;}
156 
160  virtual void clear() override;
162 
165 
169  virtual int getLength() const override {return cblength() + heapLength;}
170 
177  virtual cEvent *get(int k) override;
178 
183  virtual void sort() override;
184 };
185 
186 } // namespace omnetpp
187 
188 
189 #endif
190 
omnetpp::cEventHeap::isEmpty
virtual bool isEmpty() const override
Definition: ceventheap.h:155
omnetpp::cEvent
Represents an event in the discrete event simulator.
Definition: cevent.h:46
omnetpp::cFutureEventSet
Abstract base class for the future event set (FES), a central data structure for discrete event simul...
Definition: cfutureeventset.h:32
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::eventnumber_t
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition: simkerneldefs.h:78
omnetpp::cEventHeap::getLength
virtual int getLength() const override
Definition: ceventheap.h:169
omnetpp::cEventHeap
The default, binary heap based implementation of the future event set.
Definition: ceventheap.h:35
omnetpp::cEventHeap::dup
virtual cEventHeap * dup() const override
Definition: ceventheap.h:105