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

arigo at codespeak.net arigo at codespeak.net
Sun Nov 7 15:32:59 CET 2010


Author: arigo
Date: Sun Nov  7 15:32:57 2010
New Revision: 78814

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's isdisjoint() method.


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 15:32:57 2010
@@ -33,6 +33,10 @@
 frozenset_reduce                = SMM('__reduce__',1,
                                       doc='Return state information for'
                                           ' pickling.')
+# 2.6 methods
+frozenset_isdisjoint            = SMM('isdisjoint', 2,
+                                      doc='Return True if two sets have a'
+                                          ' null intersection.')
 
 register_all(vars(), globals())
 

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 15:32:57 2010
@@ -167,6 +167,14 @@
             result[w_key] = None
     return result
 
+def _isdisjoint_dict(ld, rd):
+    if len(ld) > len(rd):
+        ld, rd = rd, ld     # loop over the smaller dict
+    for w_key in ld:
+        if w_key in rd:
+            return False
+    return True
+
 def _symmetric_difference_dict(space, ld, rd):
     result = newset(space)
     for w_key in ld:
@@ -510,6 +518,25 @@
 
 inplace_and__Set_Frozenset = inplace_and__Set_Set
 
+def set_isdisjoint__Set_Set(space, w_left, w_other):
+    # optimization only (the general case works too)
+    ld, rd = w_left.setdata, w_other.setdata
+    disjoint = _isdisjoint_dict(ld, rd)
+    return space.newbool(disjoint)
+
+set_isdisjoint__Set_Frozenset = set_isdisjoint__Set_Set
+set_isdisjoint__Frozenset_Frozenset = set_isdisjoint__Set_Set
+set_isdisjoint__Frozenset_Set = set_isdisjoint__Set_Set
+
+def set_isdisjoint__Set_ANY(space, w_left, w_other):
+    ld = w_left.setdata
+    for w_key in space.listview(w_other):
+        if w_key in ld:
+            return space.w_False
+    return space.w_True
+
+frozenset_isdisjoint__Frozenset_ANY = set_isdisjoint__Set_ANY
+
 def set_symmetric_difference__Set_Set(space, w_left, w_other):
     # optimization only (the general case works too)
     ld, rd = w_left.setdata, w_other.setdata

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 15:32:57 2010
@@ -62,6 +62,10 @@
 set_reduce                      = SMM('__reduce__',1,
                                       doc='Return state information for'
                                           ' pickling.')
+# 2.6 methods
+set_isdisjoint                  = SMM('isdisjoint', 2,
+                                      doc='Return True if two sets have a'
+                                          ' null intersection.')
 
 register_all(vars(), globals())
 

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 15:32:57 2010
@@ -270,3 +270,13 @@
         efs = [f, Frozenset(f)]
         # All empty frozenset subclass instances should have different ids
         assert len(set(map(id, efs))) == len(efs)
+
+    def test_isdisjoint(self):
+        assert set([1,2,3]).isdisjoint(set([4,5,6]))
+        assert set([1,2,3]).isdisjoint(frozenset([4,5,6]))
+        assert set([1,2,3]).isdisjoint([4,5,6])
+        assert set([1,2,3]).isdisjoint((4,5,6))
+        assert not set([1,2,5]).isdisjoint(set([4,5,6]))
+        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))



More information about the Pypy-commit mailing list