-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calendar.cpp
107 lines (85 loc) · 2.4 KB
/
Calendar.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/************************************************************
* SimLib simulation library for event-based simulations *
* Author: Martin Ubl (A16N0026P) *
************************************************************/
#include "SimulationObject.h"
#include "Calendar.h"
bool TimePriorityCmp::operator()(SimulationObjectPtr const& a, SimulationObjectPtr const& b)
{
return a->GetNextSimTime() > b->GetNextSimTime();
}
CalendarPtr Calendar::Create()
{
auto ptr = std::make_shared<Calendar>();
return ptr;
}
bool Calendar::remove(const SimulationObjectPtr obj)
{
// find iterator containing specified object
auto it = c.begin();
for (; it != c.end(); it++)
{
if ((*it)->GetGUID() == obj->GetGUID())
break;
}
// if found...
if (it != c.end())
{
// erase from heap and rebuild it
c.erase(it);
std::make_heap(c.begin(), c.end(), comp);
return true;
}
return false;
}
SimulationObjectPtr CalendarList::top() const
{
// select first non-empty calendar
auto itr = begin();
while (itr != end() && (*itr)->empty())
++itr;
if (itr == end())
return nullptr;
// current top is "the best" for now
SimulationObjectPtr curTop = (*itr)->top();
++itr;
// go through the rest of calendars and find better
while (itr != end())
{
if ((*itr)->empty())
continue;
if ((*itr)->top()->GetNextSimTime() < curTop->GetNextSimTime())
curTop = (*itr)->top();
++itr;
}
return curTop;
}
void CalendarList::pop()
{
// select first non-empty calendar
auto itr = begin();
while (itr != end() && (*itr)->empty())
++itr;
if (itr == end())
return;
// current top is "the best" calendar for now
CalendarPtr curTop = (*itr);
++itr;
while (itr != end())
{
if ((*itr)->empty())
continue;
if ((*itr)->top()->GetNextSimTime() < curTop->top()->GetNextSimTime())
curTop = (*itr);
++itr;
}
curTop->pop();
}
bool CalendarList::all_empty() const
{
auto itr = begin();
while (itr != end() && (*itr)->empty())
++itr;
return itr == end();
}