-
Notifications
You must be signed in to change notification settings - Fork 17
/
object_array_representation.py
110 lines (87 loc) · 3.06 KB
/
object_array_representation.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
99
100
101
102
103
104
105
106
107
108
109
110
from linked_list import DoubleLinkedListNode, DoubleLinkedList
class OutOfSpaceMultipleArrayListRepresentationError(BaseException):
pass
class MultipleArrayListRepresentationNode(list):
def __init__(self, *args, **kwargs):
super(MultipleArrayListRepresentationNode, self).__init__(*args, **kwargs)
self.append(None)
self.append(None)
self.append(None)
@property
def key(self):
return self[0]
@key.setter
def key(self, value):
self[0] = value
@property
def next(self):
return self[1]
@next.setter
def next(self, value):
self[1] = value
@property
def prev(self):
return self[2]
@prev.setter
def prev(self, value):
self[2] = value
class MultipleArrayListRepresentation():
def __init__(self, length):
# Initialize container
self.container = []
for pos in range(length):
self.container.append(MultipleArrayListRepresentationNode())
self.get(pos).next = pos + 1 if pos < (length - 1) else None
# Set nil position
self.nil_position = 0
self.nil.next = self.nil_position
self.nil.prev = self.nil_position
# Set free_position to the next element
self.free_position = 1
def get(self, position):
return self.container[position]
@property
def nil(self):
return self.container[self.nil_position]
@property
def free(self):
return self.container[self.free_position]
def allocate_object(self):
if self.free_position is not None:
position = self.free_position
self.free_position = self.free.next
return position
else:
raise OutOfSpaceMultipleArrayListRepresentationError
def free_object(self, position):
self.container[self.free_position].next = position
self.container[position].next = None
self.free_position = position
def insert(self, value):
# allocate
position = self.allocate_object()
node = self.get(position)
node.key = value
# insert
nil_next = self.get(self.nil.next)
nil_next.prev = position
node.next = self.nil.next
self.nil.next = position
node.prev = self.nil_position
def delete(self, value):
pass
def __repr__(self):
return str(self.container)
multiple_array_list_representation = MultipleArrayListRepresentation(7)
multiple_array_list_representation.insert(978)
multiple_array_list_representation.insert(123)
multiple_array_list_representation.insert(123)
multiple_array_list_representation.insert(123)
multiple_array_list_representation.insert(123)
multiple_array_list_representation.insert(123)
try:
multiple_array_list_representation.insert(123)
raise Exception("We can't go there, out of space must be raised")
except OutOfSpaceMultipleArrayListRepresentationError:
pass
#assert multiple_array_list_representation == [[None, 6, 1], [978, 0, 2], [123, 1, 3], [123, 2, 4], [123, 3, 5], [123, 4, 6], [123, 5, 0]]