[pypy-commit] pypy list-strategies: implemented sort for range and empty lists

l.diekmann noreply at buildbot.pypy.org
Fri Sep 23 13:15:37 CEST 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47554:2114011231cc
Date: 2011-09-16 12:13 +0200
http://bitbucket.org/pypy/pypy/changeset/2114011231cc/

Log:	implemented sort for range and empty lists

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
@@ -316,6 +316,9 @@
         w_list.strategy = strategy
         w_list.lstorage = storage
 
+    def sort(self, w_list, reverse):
+        return
+
     def insert(self, w_list, index, w_item):
         assert index == 0
         self.append(w_list, w_item)
@@ -480,6 +483,15 @@
         self.switch_to_integer_strategy(w_list)
         w_list.setslice(start, step, slicelength, sequence_w)
 
+    def sort(self, w_list, reverse):
+        start, step, length = self.unerase(w_list.lstorage)
+        if step > 0 and reverse or step < 0 and not reverse:
+            start = start + step * (length - 1)
+            step = step * (-1)
+        else:
+            return
+        w_list.lstorage = self.erase([start, step, length])
+
     def insert(self, w_list, index, w_item):
         self.switch_to_integer_strategy(w_list)
         w_list.insert(index, w_item)
@@ -1287,17 +1299,11 @@
         if has_key:
             sorterclass = CustomKeySort
         else:
-            # XXX this is nonsense. just do something special for the object
-            # strategy and call sort immediately otherwise. implement sort on
-            # the empty list (and the range list, if you want)
-            if w_list.strategy is space.fromcache(IntegerListStrategy):
+            if w_list.strategy is space.fromcache(ObjectListStrategy):
+                sorterclass = SimpleSort
+            else:
                 w_list.sort(has_reverse)
                 return space.w_None
-            elif w_list.strategy is space.fromcache(StringListStrategy):
-                w_list.sort(has_reverse)
-                return space.w_None
-            else:
-                sorterclass = SimpleSort
 
     sorter = sorterclass(w_list.getitems(), w_list.length())
     sorter.space = space
diff --git a/pypy/objspace/std/test/test_listobject.py b/pypy/objspace/std/test/test_listobject.py
--- a/pypy/objspace/std/test/test_listobject.py
+++ b/pypy/objspace/std/test/test_listobject.py
@@ -943,6 +943,17 @@
         assert x[10:3:-2] == [9,7,5]
         assert x[1:5:-1] == []
 
+    def test_sort_range(self):
+        l = range(3,10,3)
+        l.sort()
+        assert l == [3, 6, 9]
+        l.sort(reverse = True)
+        assert l == [9, 6, 3]
+        l.sort(reverse = True)
+        assert l == [9, 6, 3]
+        l.sort()
+        assert l == [3, 6, 9]
+
 class AppTestListFastSubscr:
 
     def setup_class(cls):


More information about the pypy-commit mailing list