-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork_queue.py
More file actions
executable file
·101 lines (75 loc) · 1.97 KB
/
Copy pathwork_queue.py
File metadata and controls
executable file
·101 lines (75 loc) · 1.97 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
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
class WorkQueueException(Exception): pass
class WorkQueue:
def __init__(self):
self.wq = [] # Main work queue.
self.cur_time = 0 # Current simulation time
self.sorted = True # Keep track of if queue is sorted
def get_queue(self):
self.sort()
return self.wq
def empty(self):
return (self.wq == [])
def sort(self):
# Sort the queue if necessary.
# The sort is done right before a dequeue so we don't run it very often.
if not self.sorted:
self.wq.sort()
self.sorted = True
def dequeue(self):
# Make sure the queue is not empty.
if self.empty():
# Throw exception
raise WorkQueueException('WorkQueue.empty() called with empty queue')
self.sort()
# Get the event and set the current time.
next_event = self.wq.pop(0)
self.cur_time = next_event[0]
return next_event
def enqueue(self, event):
# Check time
if event[0] < self.cur_time:
raise WorkQueueException('WorkQueue.enqueue() called with past time')
# Insert event
self.wq.append(event)
self.sorted = False
def remove_event(self, event):
self.wq.remove(event)
def test():
# Create the main event queue.
wq = WorkQueue()
print 'Empty (should be True):',wq.empty()
print
print 'Dequeue on empty test...'
try:
event = wq.dequeue()
print 'No exception (incorrect)'
print event
except WorkQueueException, e:
print 'Exception (correct)', e
print
print 'Enqueue test...'
try:
wq.enqueue([1,4])
print 'No exception (correct)'
except WorkQueueException, e:
print 'Exception (incorrect)', e
print
print 'Empty (should be False):',wq.empty()
print
print 'Dequeue a value test...'
try:
event = wq.dequeue()
print 'No exception (correct)'
print event
except WorkQueueException, e:
print 'Exception (incorrect)', e
print 'Enqueue old time test...'
try:
wq.enqueue([0,5])
print 'No exception (incorrect)'
except WorkQueueException, e:
print 'Exception (correct)', e
print
print 'Testing done.'
if __name__ == '__main__':
test()