Author: Lukas Diekmann <[email protected]>
Branch: list-strategies
Changeset: r47454:ed567fc8e950
Date: 2011-03-09 10:30 +0100
http://bitbucket.org/pypy/pypy/changeset/ed567fc8e950/
Log: Try to keep rangelist on append and pop
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -305,8 +305,15 @@
return make_range_list(self.space, new_start, new_step, length)
def append(self, w_list, w_item):
- #XXX maybe check later if w_item fits in range to keep
RangeListStrategy
if is_W_IntObject(w_item):
+ l = self.cast_from_void_star(w_list.storage)
+ step = l[1]
+ last_in_range = self.getitem(w_list, -1)
+ if self.unwrap(w_item) - step == self.unwrap(last_in_range):
+ new = cast_to_void_star((l[0],l[1],l[2]+1), "integer")
+ w_list.storage = new
+ return
+
self.switch_to_integer_strategy(w_list)
else:
w_list.switch_to_object_strategy()
@@ -325,6 +332,23 @@
w_list.deleteslice(start, step, slicelength)
def pop(self, w_list, index):
+ #XXX move this to list_pop_List_ANY
+ if index < 0:
+ index += self.length(w_list)
+
+ l = self.cast_from_void_star(w_list.storage)
+ if index == 0:
+ r = self.getitem(w_list, 0)
+ new = cast_to_void_star((l[0]+l[1],l[1],l[2]-1), "integer")
+ w_list.storage = new
+ return r
+
+ if index == self.length(w_list)-1:
+ r = self.getitem(w_list, -1)
+ new = cast_to_void_star((l[0],l[1],l[2]-1), "integer")
+ w_list.storage = new
+ return r
+
self.switch_to_integer_strategy(w_list)
return w_list.pop(index)
diff --git a/pypy/objspace/std/test/test_liststrategies.py
b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -148,8 +148,8 @@
def test_rangelist(self):
l = make_range_list(self.space, 1,3,7)
assert isinstance(l.strategy, RangeListStrategy)
- v = l.pop(6)
- assert self.space.eq_w(v, self.space.wrap(19))
+ v = l.pop(5)
+ assert self.space.eq_w(v, self.space.wrap(16))
assert isinstance(l.strategy, IntegerListStrategy)
l = make_range_list(self.space, 1,3,7)
@@ -157,3 +157,25 @@
l.append(self.space.wrap("string"))
assert isinstance(l.strategy, ObjectListStrategy)
+ l = make_range_list(self.space, 1,1,5)
+ assert isinstance(l.strategy, RangeListStrategy)
+ l.append(self.space.wrap(19))
+ assert isinstance(l.strategy, IntegerListStrategy)
+
+ def test_keep_range(self):
+ # simple list
+ l = make_range_list(self.space, 1,1,5)
+ assert isinstance(l.strategy, RangeListStrategy)
+ x = l.pop(0)
+ assert self.space.eq_w(x, self.space.wrap(1))
+ assert isinstance(l.strategy, RangeListStrategy)
+ l.pop(-1)
+ assert isinstance(l.strategy, RangeListStrategy)
+ l.append(self.space.wrap(5))
+ assert isinstance(l.strategy, RangeListStrategy)
+
+ # complex list
+ l = make_range_list(self.space, 1,3,5)
+ assert isinstance(l.strategy, RangeListStrategy)
+ l.append(self.space.wrap(16))
+ assert isinstance(l.strategy, RangeListStrategy)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit