-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.cpp
46 lines (35 loc) · 1 KB
/
Simulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Created by zeronsix on 5/12/19.
//
#include "Simulator.h"
Time Simulator::m_simTime = 0;
Time Simulator::m_nowTime = 0;
std::multimap<Time, EventHandler> Simulator::m_eventQueue;
std::mt19937 Simulator::m_engine;
void Simulator::Configure(Time simTime, int seed) {
m_simTime = simTime;
m_engine.seed(seed);
}
void Simulator::Schedule(Time time, EventHandler handler) {
LOG_FUNCTION();
m_eventQueue.insert(std::pair<Time, EventHandler>{m_nowTime + time, handler});
}
Time Simulator::Now() {
return m_nowTime;
}
void Simulator::Run() {
while (!m_eventQueue.empty() && m_nowTime <= m_simTime) {
auto it = m_eventQueue.begin();
if (it->first > m_simTime) {
m_eventQueue.clear();
break;
}
m_nowTime = it->first;
it->second();
m_eventQueue.erase(it);
}
}
int Simulator::GenerateUniformRandomVariable(int min, int max) {
std::uniform_int_distribution<> distribution{min, max};
return distribution(m_engine);
}