-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.h
110 lines (91 loc) · 3.14 KB
/
event.h
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
108
109
/*==========================================================================
event.h CSC 790 Project 2, E. W. Fulp 10/10/2017
Event type, models events for the calendar.
============================================================================*/
#ifndef EVENTTYPE_H
#define EVENTTYPE_H
#include <ostream>
#include <fstream>
#include <cstdio>
using namespace std;
//=enum type for the description of events=================
// Infect attempt to infect another station
// Propagate find another host
enum Event{Infect, Propagate};
//=========================================================
// EventType struct
// Event struct for the simulator driver.
//=========================================================
struct EventType
{
//=null constructor========================================
EventType(Event e = Infect, double time = 0, int toID = 0,
int fromID = 0, int type = 0): event_(e), time_(time),
toID_(toID), fromID_(fromID), wType_(type)
{ }
Event event() const { return event_; }
double time() const { return time_; }
int toID() const { return toID_; }
int fromID() const { return fromID_; }
int type() const {return wType_; }
//=data members===========================================
Event event_; // event type
double time_; // time of event
int toID_; // intended receiver of the event
int fromID_; // creator of the event
int wType_; // type of worm good or bad
};
//=overload << for Event==================================
ostream& operator<<(ostream& out, Event e)
{
switch(e)
{
case Infect:
out << "Infect";
break;
case Propagate:
out << "Propagate";
break;
default:
cerr << "Illegal event \n";
}
return out;
}
//=overload << for EventType=================================
ostream& operator<<(ostream& out, EventType e)
{
out << '[' << e.event_ << ", " << e.time_ << ", "
<< e.toID_ << ", " << e.fromID_ << ']';
return out;
}
//===========================================================
// logical operators for EventType, required for insertion
// into the event queue (STL).
//===========================================================
int operator>(EventType a, EventType b)
{
if(a.time_ == b.time_)
return (a.event_ > b.event_);
return (a.time_ > b.time_);
}
int operator<(EventType a, EventType b)
{
if(a.time_ == b.time_)
return (a.event_ < b.event_);
return (a.time_ < b.time_);
}
int operator==(EventType a, EventType b)
{ return (a.event_ == b.event_) && (a.time_ == b.time_); }
int operator>=(EventType a, EventType b)
{ return (a > b || a == b); }
int operator<=(EventType a, EventType b)
{ return (a < b || a == b); }
struct eventComparison
{
// Achtung! This function is not entirely correct, since it will not preserve the order
// of events that occur at the same time. Need to include an additional unique increasing number (ID) per event.
// For this project (worms) it does not matter, for a full explanation, see http://goo.gl/rxnHB1
bool operator () (const EventType &left, const EventType &right)
{ return left.time() > right.time(); }
};
#endif