-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderQueue.py
59 lines (46 loc) · 1.64 KB
/
OrderQueue.py
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
from CustomPizza import *
from SpecialtyPizza import *
from Pizza import *
from PizzaOrder import *
class OrderQueue:
def __init__(self):
self.heapList = [0]
self.currentSize = 0
def percUp(self, i):
while i // 2 > 0:
if self.heapList[i].getTime() < self.heapList[i // 2].getTime():
tmp = self.heapList[i // 2]
self.heapList[i // 2] = self.heapList[i]
self.heapList[i] = tmp
i = i // 2
def addOrder(self, pizzaOrder):
self.heapList.append(pizzaOrder)
self.currentSize = self.currentSize + 1
self.percUp(self.currentSize)
def percDown(self, i):
while (i * 2) <= self.currentSize:
mc = self.minChild(i)
if self.heapList[i].getTime() > self.heapList[mc].getTime():
tmp = self.heapList[i]
self.heapList[i] = self.heapList[mc]
self.heapList[mc] = tmp
i = mc
def minChild(self, i):
if (i * 2 + 1) > self.currentSize:
return i * 2
else:
if self.heapList[i * 2].getTime() < self.heapList[i*2+1].getTime():
return i*2
else:
return i*2+1
def processNextOrder(self):
if self.heapList == [0]:
raise QueueEmptyException()
retval = self.heapList[1].getOrderDescription() #retval
self.heapList[1] = self.heapList[self.currentSize]
self.currentSize = self.currentSize - 1
self.heapList.pop()
self.percDown(1)
return retval
class QueueEmptyException(Exception):
pass