-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharraylistiteration.py
94 lines (84 loc) · 3.77 KB
/
arraylistiteration.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
class ArrayListIteration(object):
"""Represents the list iteration for an array list."""
def __init__(self, backingStore):
"""Set the initial state of the list iterator."""
self._backingStore = backingStore
self._modCount = backingStore.getModCount()
self.first()
# Accessors
def first(self):
"""Resets the cursor to the begining of the backing store."""
self._cursor = 0
self._lastItemPos = -1
def last(self):
"""Moves the cursor to the end of the backing store."""
self._cursor = len(self._backingStore)
self._lastItemPos = -1
def hasNext(self):
"""Returns True if the iterator has a next item
or False otherwise."""
return self._cursor < len(self._backingStore)
def next(self):
"""Preconditions: hasNext returns True.
The list has not been modified except by this
iterator's mutators.
Returns the current item and and advance the cursor
to the next item."""
if not self.hasNext():
raise ValueError("No next item in list iterator")
if self._modCount != self._backingStore.getModCount():
raise AttributeError("Illegal modification of backing store")
self._lastItemPos = self._cursor
self._cursor += 1
return sefl._backingStore[self._lastItemPos]
def hasPrevious(self):
"""Returns True if the iterator has a previous item
or False otherwise."""
return self._cursor > 0
def previous(self):
"""Precondition: hasPrevious returns True.
The list has not been modified except by this iterator's mutators.
Returns the current item and moves the cursor to the previous item."""
if not self.hasPrevious():
raise ValueError("No previous item in list iterator"
if self._modCount != self._backingStore.getModCount():
raise AttributeError("Illegal modification of backing store")
self._lastItemPos = self._cursor
self._cursor -= 1
return sefl._backingStore[self._lastItemPos]
# Mutators
def replace(self, item):
"""Precondition: the current position is defined.
The list has not been modified except by this iterator's mutators."""
if self._lastItemPos == -1
raise AttributeError("The current position is undefined.")
if self._modCount != self._backingStore.getModCount():
raise AttributeError("Illegal modification of backing store")
self._backingStore[self._lastItemPos] = item
self._lastItemPos = -1
def insert(self, item):
"""Precondition: The list has not been modified except by this
iterator's mutators."""
if self._modCount != self._backingStore.getModCount():
raise AttributeError("Illegal modification of backing store")
if self._lastItemPos = -1:
# Cursor not defined, so add item to end of list
self._backingStore.add(item)
else:
self._backingStore.insert(self._lastItemPos, item)
self._lastItemPos = -1
self._modCount += 1
def remove(self, item):
"""Precondition: the current position is defined.
The list has not been modified except by this iterator's mutators."""
if self._lastItemPos == -1
raise AttributeError("The current position is undefined.")
if self._modCount != self._backingStore.getModCount():
raise AttributeError("Illegal modification of backing store")
item = self._backingStore.pop(self._lastItemPos)
# if the item removed was obtained via next,
# move cursor back
if self._lastItemPos < self._cursor:
self._cursor -= 1
self._modCount += 1
self._lastItemPos = -1