-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcms.py
98 lines (61 loc) · 2.56 KB
/
gcms.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
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
from bin import Bin
from avl import AVLTree
from object import Object, Color
from exceptions import NoBinFoundException
from node import Node
class GCMS:
def __init__(self):
# Maintain all the Bins and Objects in GCMS
#avl tree of bins
self.binsystem=AVLTree()
self.bin__id=AVLTree()
self.objectsystem=AVLTree()
def add_bin(self, bin_id, capacity):
b=Bin(bin_id,capacity)
self.binsystem.add_node(capacity,b)
self.bin__id.add_node(bin_id,b)
def add_object(self, object_id, size, color):
obj = Object(object_id, size, color)
if color == Color.BLUE:
t = self.binsystem.find_ceil_leastid(size)
elif color == Color.YELLOW:
t = self.binsystem.find_ceil_maxid(size)
elif color == Color.RED:
t = self.binsystem.find_largest_minid(size)
elif color==Color.GREEN:
t = self.binsystem.find_largest_maxid(size)
if t is None:
raise NoBinFoundException
new = Node(t.key - size, t.data)
self.binsystem._delete_node(t)
self.binsystem.add_node(new.key, new.data)
new.data.capacity-=size
new.data.add_object(obj)
self.objectsystem.add_node(object_id, (size, new.data))
def delete_object(self, object_id):
x = self.objectsystem.search_node(object_id)
if x is None:
raise NoBinFoundException
size, bin_obj = x.data
bin_obj.remove_object(object_id)
self.objectsystem._delete_key(object_id)
y=self.binsystem.search_node(bin_obj.capacity,bin_obj)
self.binsystem._delete_node(y)
self.binsystem.add_node(bin_obj.capacity+size,bin_obj)
bin_obj.capacity+=size
def bin_info(self, bin_id):
# returns a tuple with current capacity of the bin and the list of objects in the bin (int, list[int])
g=self.bin__id.search_node(bin_id)
c=g.data.capacity
l=[]
for i in g.data.objects:
# c=c-i.sizeobj
l.append(i.object_id)
res=(c,l)
return res
def object_info(self, object_id):
# returns the bin_id in which the object is stored
x=self.objectsystem.search_node(object_id)
if(x==None):
return None
return(((x.data)[1]).bin_id)