-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharraysortedbag.py
More file actions
44 lines (39 loc) · 1.47 KB
/
arraysortedbag.py
File metadata and controls
44 lines (39 loc) · 1.47 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
from arraybag import ArrayBag
class ArraySortedBag(ArrayBag):
"""An array-based sorted bag implementation."""
# Constructor
def __init__(self, sourceCollection=None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
ArrayBag.__init__(self, sourceCollection)
# Accessor methods
def __contain__(self, item):
"""Returns True if item is in self, or False otherwise."""
left = 0
right = len(self) - 1
while left <= right:
midPoint = (left + right) // 2
if self._items[midPoint] == item:
return True
else if self._items[midPoint] < item:
left = midPoint + 1
else:
right = midPoint - 1
return False
# Mutator methods
def add(self, item):
"""Add item to self."""
# Empty or last item, call ArrayBag.add
if self.isEmpty() or item > self._items[len(self) - 1]:
ArrayBag.add(self, item)
else:
# Resize the array if it is full here
# Search for first item >= new item
targetIndex = 0
while item > self._items[targetIndex]:
targetIndex += 1
# Open a hole for net item
for i in range(len(self), targetIndex, -1):
self._items[i] = self._items[i - 1]
self._items[targetIndex] = item
self._size += 1