Skip to content

Commit

Permalink
Small refactorigns for better understandability. Added bound check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bystroushaak committed Sep 22, 2019
1 parent 4434338 commit 41a436a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/tinySelf/shared/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TwoPointerArray(object):
def __init__(self, length):
self._length = length
self._allocated_length = length
self._array = [None for _ in xrange(length)]
self._left_pointer = 0
self._right_pointer = 0
Expand All @@ -13,6 +13,9 @@ def __len__(self):
return self._right_pointer - self._left_pointer

def __getitem__(self, index):
if index >= self._right_pointer:
raise IndexError()

return self._array[self._left_pointer + index]

def __setitem__(self, key, value):
Expand All @@ -37,10 +40,10 @@ def pop_last(self):
return rval

def append(self, item):
if self._right_pointer >= self._length:
if self._right_pointer >= self._allocated_length:
self._array.append(item)
self._right_pointer += 1
self._length += 1
self._allocated_length += 1
return

self._array[self._right_pointer] = item
Expand Down

0 comments on commit 41a436a

Please sign in to comment.