Author: Lukas Diekmann <lukas.diekm...@uni-duesseldorf.de>
Branch: set-strategies
Changeset: r50760:01dbcc06249a
Date: 2011-12-20 16:19 +0100
http://bitbucket.org/pypy/pypy/changeset/01dbcc06249a/

Log:    better approach for merging sets with 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
@@ -193,6 +193,11 @@
         """ Return the items in the list as unwrapped strings. If the list does
         not use the list strategy, return None. """
         return self.strategy.getitems_str(self)
+
+    def getitems_int(self):
+        """ Return the items in the list as unwrapped strings. If the list does
+        not use the list strategy, return None. """
+        return self.strategy.getitems_int(self)
     # ___________________________________________________
 
 
@@ -292,6 +297,9 @@
     def getitems_str(self, w_list):
         return None
 
+    def getitems_int(self, w_list):
+        return None
+
     def getstorage_copy(self, w_list):
         raise NotImplementedError
 
@@ -502,17 +510,15 @@
             raise IndexError
         return start + i * step
 
+    def getitems_int(self, w_list):
+        return self._getitems_range(w_list, False)
+
     def getitem(self, w_list, i):
         return self.wrap(self._getitem_unwrapped(w_list, i))
 
     def getitems_copy(self, w_list):
         return self._getitems_range(w_list, True)
 
-    getitems_wrapped = getitems_copy
-
-    def getitems_unwrapped(self, w_list):
-        return self._getitems_range(w_list, False)
-
     def getstorage_copy(self, w_list):
         # tuple is unmutable
         return w_list.lstorage
@@ -703,11 +709,6 @@
     def getitems_copy(self, w_list):
         return [self.wrap(item) for item in self.unerase(w_list.lstorage)]
 
-    getitems_wrapped = getitems_copy
-
-    def getitems_unwrapped(self, w_list):
-        return self.unerase(w_list.lstorage)
-
     @jit.unroll_safe
     def getitems_unroll(self, w_list):
         return [self.wrap(item) for item in self.unerase(w_list.lstorage)]
@@ -936,8 +937,6 @@
     def getitems(self, w_list):
         return self.unerase(w_list.lstorage)
 
-    getitems_wrapped = getitems
-
 class IntegerListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = 0
     _applevel_repr = "int"
@@ -965,6 +964,9 @@
         if reverse:
             l.reverse()
 
+    def getitems_int(self, w_list):
+        return self.unerase(w_list.lstorage)
+
 class FloatListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = 0.0
     _applevel_repr = "float"
@@ -1022,7 +1024,6 @@
     def getitems_str(self, w_list):
         return self.unerase(w_list.lstorage)
 
-
 # _______________________________________________________
 
 init_signature = Signature(['sequence'], None, None)
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -440,6 +440,11 @@
             return w_obj.getitems_str()
         return None
 
+    def listview_int(self, w_obj):
+        if isinstance(w_obj, W_ListObject):
+            return w_obj.getitems_int()
+        return None
+
     def sliceindices(self, w_slice, w_length):
         if isinstance(w_slice, W_SliceObject):
             a, b, c = w_slice.indices3(self, self.int_w(w_length))
diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -13,8 +13,6 @@
 from pypy.objspace.std.listobject import W_ListObject
 from pypy.objspace.std.intobject import W_IntObject
 from pypy.objspace.std.stringobject import W_StringObject
-from pypy.objspace.std.listobject import IntegerListStrategy, 
StringListStrategy,\
-        EmptyListStrategy, RangeListStrategy, ObjectListStrategy, 
FloatListStrategy
 
 class W_BaseSetObject(W_Object):
     typedef = None
@@ -282,9 +280,6 @@
     def get_empty_storage(self):
         return self.erase(None)
 
-    def get_storage_from_w_list(self, w_list):
-        return self.get_empty_storage()
-
     def is_correct_type(self, w_key):
         return False
 
@@ -389,9 +384,7 @@
             setdata[self.unwrap(w_item)] = None
         return self.erase(setdata)
 
-    def get_storage_from_w_list(self, w_list):
-        items = w_list.strategy.getitems_unwrapped(w_list)
-
+    def get_storage_from_unwrapped_list(self, items):
         setdata = self.get_empty_dict()
         for item in items:
             setdata[item] = None
@@ -759,14 +752,6 @@
     def get_empty_storage(self):
         return self.erase(self.get_empty_dict())
 
-    def get_storage_from_w_list(self, w_list):
-        items = w_list.strategy.getitems_wrapped(w_list)
-
-        setdata = self.get_empty_dict()
-        for item in items:
-            setdata[item] = None
-        return self.erase(setdata)
-
     def get_empty_dict(self):
         return newset(self.space)
 
@@ -904,22 +889,6 @@
 def newset(space):
     return r_dict(space.eq_w, space.hash_w, force_non_null=True)
 
-_strategy_map = {
-    EmptyListStrategy: EmptySetStrategy,
-    IntegerListStrategy: IntegerSetStrategy,
-    RangeListStrategy: IntegerSetStrategy,
-    StringListStrategy: StringSetStrategy,
-    FloatListStrategy: ObjectSetStrategy,
-    ObjectListStrategy: ObjectSetStrategy
-}
-
-def set_strategy_and_setdata_from_listobject(space, w_set, w_list):
-    strategy_class = _strategy_map[w_list.strategy.__class__]
-    strategy = space.fromcache(strategy_class)
-
-    w_set.sstorage = strategy.get_storage_from_w_list(w_list)
-    w_set.strategy = strategy
-
 def set_strategy_and_setdata(space, w_set, w_iterable):
     from pypy.objspace.std.intobject import W_IntObject
     if w_iterable is None :
@@ -932,8 +901,18 @@
         w_set.sstorage = w_iterable.get_storage_copy()
         return
 
-    if isinstance(w_iterable, W_ListObject):
-        set_strategy_and_setdata_from_listobject(space, w_set, w_iterable)
+    stringlist = space.listview_str(w_iterable)
+    if stringlist != None:
+        strategy = space.fromcache(StringSetStrategy)
+        w_set.strategy = strategy
+        w_set.sstorage = strategy.get_storage_from_unwrapped_list(stringlist)
+        return
+
+    intlist = space.listview_int(w_iterable)
+    if intlist != None:
+        strategy = space.fromcache(IntegerSetStrategy)
+        w_set.strategy = strategy
+        w_set.sstorage = strategy.get_storage_from_unwrapped_list(intlist)
         return
 
     iterable_w = space.listview(w_iterable)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to