OMNeT++ Simulation Library  5.6.1
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; // heap array (heap[0] always empty)
40  int heapLength; // number of elements on the heap
41  int heapCapacity; // allocated size of the heap[] array
42  eventnumber_t insertCount; // 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; // size of the circular buffer
46  int cbsize; // always power of 2
47  int cbhead, cbtail; // cbhead is inclusive, cbtail is exclusive
48  bool useCb; // 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  public:
72 
76  cEventHeap(const cEventHeap& msgq);
77 
81  cEventHeap(const char *name=nullptr, int initialCapacity=128);
82 
86  virtual ~cEventHeap();
87 
92  cEventHeap& operator=(const cEventHeap& other);
94 
97 
102  virtual cEventHeap *dup() const override {return new cEventHeap(*this);}
103 
108  virtual std::string str() const override;
109 
114  virtual void forEachChild(cVisitor *v) override;
115 
116  // no parsimPack() and parsimUnpack()
118 
124  virtual void insert(cEvent *event) override;
125 
130  virtual cEvent *peekFirst() const override;
131 
136  virtual cEvent *removeFirst() override;
137 
141  virtual void putBackFirst(cEvent *event) override;
142 
147  virtual cEvent *remove(cEvent *event) override;
148 
152  virtual bool isEmpty() const override {return cbhead==cbtail && heapLength==0;}
153 
157  virtual void clear() override;
159 
162 
166  virtual int getLength() const override {return cblength() + heapLength;}
167 
174  virtual cEvent *get(int k) override;
175 
180  virtual void sort() override;
181 };
182 
183 } // namespace omnetpp
184 
185 
186 #endif
187 
Abstract base class for the future event set (FES), a central data structure for discrete event simul...
Definition: cfutureeventset.h:32
Represents an event in the discrete event simulator.
Definition: cevent.h:43
virtual bool isEmpty() const override
Definition: ceventheap.h:152
The default, binary heap based implementation of the future event set.
Definition: ceventheap.h:35
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition: simkerneldefs.h:78
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Definition: cabstracthistogram.h:21
virtual int getLength() const override
Definition: ceventheap.h:166
virtual cEventHeap * dup() const override
Definition: ceventheap.h:102