[pypy-svn] r78816 - in pypy/branch/fast-forward/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sun Nov 7 16:01:11 CET 2010


Author: arigo
Date: Sun Nov  7 16:01:10 2010
New Revision: 78816

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py
   pypy/branch/fast-forward/pypy/objspace/std/setobject.py
   pypy/branch/fast-forward/pypy/objspace/std/settype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py
Log:
set.intersection(more_than_one_argument).


Modified: pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py	Sun Nov  7 16:01:10 2010
@@ -11,10 +11,9 @@
                                           ' as a new set.\n\n(i.e. all'
                                           ' elements that are in this set but'
                                           ' not the other.)')
-frozenset_intersection          = SMM('intersection', 2,
-                                      doc='Return the intersection of two sets'
-                                          ' as a new set.\n\n(i.e. all'
-                                          ' elements that are in both sets.)')
+frozenset_intersection          = SMM('intersection', 1, varargs_w=True,
+                                      doc='Return a new set with elements common'
+                                          ' to the set and all others.')
 frozenset_issubset              = SMM('issubset', 2,
                                       doc='Report whether another set contains'
                                           ' this set.')

Modified: pypy/branch/fast-forward/pypy/objspace/std/setobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/setobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/setobject.py	Sun Nov  7 16:01:10 2010
@@ -471,30 +471,32 @@
     del w_left.setdata[w_key]
     return w_key
 
-def set_intersection__Set_Set(space, w_left, w_other):
-    # optimization only (the general case works too)
+def and__Set_Set(space, w_left, w_other):
     ld, rd = w_left.setdata, w_other.setdata
     new_ld = _intersection_dict(space, ld, rd)
     return w_left._newobj(space, new_ld)
 
-set_intersection__Set_Frozenset = set_intersection__Set_Set
-set_intersection__Frozenset_Frozenset = set_intersection__Set_Set
-set_intersection__Frozenset_Set = set_intersection__Set_Set
-
-def set_intersection__Set_ANY(space, w_left, w_other):
-    result = newset(space)
-    ld = w_left.setdata
-    for w_key in space.listview(w_other):
-        if w_key in ld:
-            result[w_key] = None
+and__Set_Frozenset = and__Set_Set
+and__Frozenset_Set = and__Set_Set
+and__Frozenset_Frozenset = and__Set_Set
+
+def set_intersection__Set(space, w_left, others_w):
+    result = w_left.setdata
+    if len(others_w) == 0:
+        result = result.copy()
+    for w_other in others_w:
+        if isinstance(w_other, W_BaseSetObject):
+            # optimization only
+            result = _intersection_dict(space, result, w_other.setdata)
+        else:
+            result2 = newset(space)
+            for w_key in space.listview(w_other):
+                if w_key in result:
+                    result2[w_key] = None
+            result = result2
     return w_left._newobj(space, result)
 
-frozenset_intersection__Frozenset_ANY = set_intersection__Set_ANY
-
-and__Set_Set = set_intersection__Set_Set
-and__Set_Frozenset = set_intersection__Set_Set
-and__Frozenset_Set = set_intersection__Set_Set
-and__Frozenset_Frozenset = set_intersection__Set_Set
+frozenset_intersection__Frozenset = set_intersection__Set
 
 def set_intersection_update__Set_Set(space, w_left, w_other):
     # optimization only (the general case works too)

Modified: pypy/branch/fast-forward/pypy/objspace/std/settype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/settype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/settype.py	Sun Nov  7 16:01:10 2010
@@ -23,10 +23,9 @@
                                       doc='Remove an element from a set if it'
                                           ' is a member.\n\nIf the element is'
                                           ' not a member, do nothing.')
-set_intersection                = SMM('intersection', 2,
-                                      doc='Return the intersection of two sets'
-                                          ' as a new set.\n\n(i.e. all'
-                                          ' elements that are in both sets.)')
+set_intersection                = SMM('intersection', 1, varargs_w=True,
+                                      doc='Return a new set with elements common'
+                                          ' to the set and all others.')
 set_intersection_update         = SMM('intersection_update', 2,
                                       doc='Update a set with the intersection'
                                           ' of itself and another.')

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py	Sun Nov  7 16:01:10 2010
@@ -11,8 +11,8 @@
 from pypy.objspace.std.setobject import W_SetObject, W_FrozensetObject
 from pypy.objspace.std.setobject import _initialize_set
 from pypy.objspace.std.setobject import newset, make_setdata_from_w_iterable
-from pypy.objspace.std.setobject import set_intersection__Set_Set
-from pypy.objspace.std.setobject import set_intersection__Set_ANY
+from pypy.objspace.std.setobject import and__Set_Set
+from pypy.objspace.std.setobject import set_intersection__Set
 from pypy.objspace.std.setobject import eq__Set_Set
 
 letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
@@ -35,10 +35,10 @@
         _initialize_set(self.space, t0, self.otherword)
         t1 = W_FrozensetObject(self.space,
                 make_setdata_from_w_iterable(self.space, self.otherword))
-        r0 = set_intersection__Set_Set(self.space, s, t0)
-        r1 = set_intersection__Set_Set(self.space, s, t1)
+        r0 = and__Set_Set(self.space, s, t0)
+        r1 = and__Set_Set(self.space, s, t1)
         assert eq__Set_Set(self.space, r0, r1) == self.true
-        sr = set_intersection__Set_ANY(self.space, s, self.otherword)
+        sr = set_intersection__Set(self.space, s, [self.otherword])
         assert eq__Set_Set(self.space, r0, sr) == self.true
 
     def test_compare(self):
@@ -284,3 +284,19 @@
         assert not set([1,2,5]).isdisjoint(frozenset([4,5,6]))
         assert not set([1,2,5]).isdisjoint([4,5,6])
         assert not set([1,2,5]).isdisjoint((4,5,6))
+
+    def test_intersection(self):
+        assert set([1,2,3]).intersection(set([2,3,4])) == set([2,3])
+        assert set([1,2,3]).intersection(frozenset([2,3,4])) == set([2,3])
+        assert set([1,2,3]).intersection([2,3,4]) == set([2,3])
+        assert set([1,2,3]).intersection((2,3,4)) == set([2,3])
+        assert frozenset([1,2,3]).intersection(set([2,3,4])) == frozenset([2,3])
+        assert frozenset([1,2,3]).intersection(frozenset([2,3,4]))== frozenset([2,3])
+        assert frozenset([1,2,3]).intersection([2,3,4]) == frozenset([2,3])
+        assert frozenset([1,2,3]).intersection((2,3,4)) == frozenset([2,3])
+        assert set([1,2,3,4]).intersection([2,3,4,5], set((1,2,3))) == set([2,3])
+        assert frozenset([1,2,3,4]).intersection((2,3,4,5), [1,2,3]) == \
+                   frozenset([2,3])
+        s = set([1,2,3])
+        assert s.intersection() == s
+        assert s.intersection() is not s



More information about the Pypy-commit mailing list