-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventQueue.cpp
More file actions
62 lines (53 loc) · 2.05 KB
/
EventQueue.cpp
File metadata and controls
62 lines (53 loc) · 2.05 KB
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
/* Hunter Trautz and Gabe Aponte, EventQueue.cpp */
#include "EventQueue.h"
/** Removes the event that is at the head of the queue from the queue and sets the next event in line as the head
* @Param *queue pointer to the queue that we want to remove the head event from
* @Return void, removes the head of the queue
*/
void removeEventHelper(EventQueue *queue){
queue = queue->nextEvent;
}
/** Removes the event that is at the head of the queue from the queue and sets the next event in line as the head
* This function is so that you can easily call removeEventHelper on an object
* @Return void, removes the head of the queue
*/
void EventQueue::removeEvent(){
removeEventHelper(this);
}
/** Adds an event to the event queue
* @Param *queue pointer to the eventQueue
* @Param *event Pointer to the given event we want to add
* @Return void, add the event to the event queue
*/
void addEventHelper(EventQueue *queue, Event *event){
if(queue->nextEvent == NULL){
//if the queue's first event is not pointing to anything, initalize a new event for it to point to
queue->nextEvent = new EventQueue();
//if the queue does not have a first event, assign the event that was passed in as the head
if(queue->event == NULL){
queue->event = event;
}else //otherwise just add the event to the queue {
queue->nextEvent->event = event;
}
else{
//if the queue is normal insert the event in a place where the times line up
if(event->time < queue->event->time){
//Insert it here
EventQueue* temp = new EventQueue();
*temp = *queue;
queue->event = event;
queue->nextEvent = temp;
}else{
//call the function again with the next event and reapet until the times line up
addEventHelper(queue->nextEvent, event);
}
}
}
/** Adds a event to the correct position in the queue
* This function is so that you can easily call addEventHelper on an object
* @param event the event you want to add to the queue
* @Return void, adds the event to its correct position in queue
*/
void EventQueue::addEvent(Event *event){
addEventHelper(this, event);
}