From 9430c4f5bfac2aa6f1968e17ad8a9607a9233127 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Sun, 26 Jul 2015 02:17:35 -0700 Subject: [PATCH] tweak rotated array stuff and tests. --- dotfiles/lib/python/rotated_array.py | 25 +++++++------------ .../lib/python/rotated_array_proxy_test.py | 13 +++++++++- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/dotfiles/lib/python/rotated_array.py b/dotfiles/lib/python/rotated_array.py index 21af5a9e..1c5a44c7 100644 --- a/dotfiles/lib/python/rotated_array.py +++ b/dotfiles/lib/python/rotated_array.py @@ -37,22 +37,6 @@ class RotatedArrayProxy(object): else: self._handle_slice(item) - def _handle_slice(self, item): - start = self._actual_index(item.start) - stop = self._actual_index(item.stop) - if start > stop: - sliced = self.incoming[start::item.stride] - # Ensure that the stride is preserved as it passes through - # the rotation. - start_index = (len(self.incoming) - start) % (item.stride or 1) - if start_index <= stop: - sliced.extend( - self.incoming[start_index:stop:item.stride] - ) - return sliced - else: - return self.incoming[start:stop:item.stride] - def _actual_index(self, index): if index is None: return index @@ -89,3 +73,12 @@ class RotatedArrayProxy(object): def unrotated(self): return rotate_array(self.incoming, self.rotation_index) + + def insert(self, x): + insertion_index = self.actual_insertion_index(x) + if insertion_index < self.rotation_index or ( + insertion_index == self.rotation_index and + (not self.incoming or x < self.incoming[0]) + ): + self._rotation_index += 1 + self.incoming.insert(self.actual_insertion_index(x), x) diff --git a/dotfiles/lib/python/rotated_array_proxy_test.py b/dotfiles/lib/python/rotated_array_proxy_test.py index dc86d379..ac513086 100644 --- a/dotfiles/lib/python/rotated_array_proxy_test.py +++ b/dotfiles/lib/python/rotated_array_proxy_test.py @@ -66,4 +66,15 @@ def test_rotation_index_and_unrotate(): def test_insert(): - + arr = [3]*117 + [1] + [2]*16 + rap = rotated_array.RotatedArrayProxy(arr) + rap.insert(3) + rap.insert(3) + rap.insert(2) + rap.insert(2) + rap.insert(5) + rap.insert(24) + rap.insert(5) + rap.insert(4) + rap.insert(4) + assert rap.unrotated() == sorted(rap.incoming)