From 41a436ae8cadae269fcaa77ff12cff31db1d0dd0 Mon Sep 17 00:00:00 2001 From: Bystroushaak Date: Mon, 23 Sep 2019 01:01:23 +0200 Subject: [PATCH] Small refactorigns for better understandability. Added bound check. --- src/tinySelf/shared/arrays.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tinySelf/shared/arrays.py b/src/tinySelf/shared/arrays.py index 52e90fe..7b5c76a 100644 --- a/src/tinySelf/shared/arrays.py +++ b/src/tinySelf/shared/arrays.py @@ -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 @@ -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): @@ -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