Lukas Diekmann avatar Lukas Diekmann committed 01dbcc0

better approach for merging sets with lists

Comments (0)

Files changed (3)

pypy/objspace/std/listobject.py

         """ 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)
     # ___________________________________________________
 
 
     def getitems_str(self, w_list):
         return None
 
+    def getitems_int(self, w_list):
+        return None
+
     def getstorage_copy(self, w_list):
         raise NotImplementedError
 
             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
     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)]
     def getitems(self, w_list):
         return self.unerase(w_list.lstorage)
 
-    getitems_wrapped = getitems
-
 class IntegerListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = 0
     _applevel_repr = "int"
         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"
     def getitems_str(self, w_list):
         return self.unerase(w_list.lstorage)
 
-
 # _______________________________________________________
 
 init_signature = Signature(['sequence'], None, None)

pypy/objspace/std/objspace.py

             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))

pypy/objspace/std/setobject.py

 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
     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
 
             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
     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)
 
 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 :
         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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.