[pypy-commit] pypy SpecialisedTuples: (mwp) add code for ordering of specialised 2-tuples

mwp noreply at buildbot.pypy.org
Thu Nov 10 10:47:48 CET 2011


Author: Mark Pearse <mark.pearse at skynet.be>
Branch: SpecialisedTuples
Changeset: r49101:59211f8aac41
Date: 2011-11-06 17:48 +0100
http://bitbucket.org/pypy/pypy/changeset/59211f8aac41/

Log:	(mwp) add code for ordering of specialised 2-tuples

diff --git a/pypy/objspace/std/specialisedtupleobject.py b/pypy/objspace/std/specialisedtupleobject.py
--- a/pypy/objspace/std/specialisedtupleobject.py
+++ b/pypy/objspace/std/specialisedtupleobject.py
@@ -11,7 +11,6 @@
 from pypy.rlib.rarithmetic import intmask
 from pypy.rlib.objectmodel import compute_hash
 
-
 class NotSpecialised(Exception):
     pass         
             
@@ -96,6 +95,59 @@
             else:
                 return space.w_False
     
+        def ne(self, space, w_other):
+            if w_other.length() != 2:
+                return space.w_True
+            if self.val0 != w_other.val0:
+                return space.w_True
+            if self.val1 != w_other.val1:
+                return space.w_True
+            return space.w_False
+    
+        def lt(self, space, w_other):
+            assert self.length() <= 2
+            ncmp = min(self.length(), w_other.length())
+            if ncmp >= 1:
+                if not self.val0 == w_other.val0:
+                    return space.newbool(self.val0 < w_other.val0)
+            if ncmp >= 2:
+                if not self.val1 == w_other.val1:
+                    return space.newbool(self.val1 < w_other.val1)
+            return space.newbool(self.length() < w_other.length())
+    
+        def le(self, space, w_other):
+            assert self.length() <= 2
+            ncmp = min(self.length(), w_other.length())
+            if ncmp >= 1:
+                if not self.val0 == w_other.val0:
+                    return space.newbool(self.val0 <= w_other.val0)
+            if ncmp >= 2:
+                if not self.val1 == w_other.val1:
+                    return space.newbool(self.val1 <= w_other.val1)
+            return space.newbool(self.length() <= w_other.length())
+    
+        def ge(self, space, w_other):
+            assert self.length() <= 2
+            ncmp = min(self.length(), w_other.length())
+            if ncmp >= 1:
+                if not self.val0 == w_other.val0:
+                    return space.newbool(self.val0 >= w_other.val0)
+            if ncmp >= 2:
+                if not self.val1 == w_other.val1:
+                    return space.newbool(self.val1 >= w_other.val1)
+            return space.newbool(self.length() >= w_other.length())
+    
+        def gt(self, space, w_other):
+            assert self.length() <= 2
+            ncmp = min(self.length(), w_other.length())
+            if ncmp >= 1:
+                if not self.val0 == w_other.val0:
+                    return space.newbool(self.val0 > w_other.val0)
+            if ncmp >= 2:
+                if not self.val1 == w_other.val1:
+                    return space.newbool(self.val1 > w_other.val1)
+            return space.newbool(self.length() > w_other.length())
+    
         def getitem(self, index):
             if index == 0:
                 return self.space.wrap(self.val0)
@@ -131,6 +183,21 @@
 def eq__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
     return w_tuple1.eq(space, w_tuple2)
 
+def ne__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
+    return w_tuple1.ne(space, w_tuple2)
+
+def lt__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
+    return w_tuple1.lt(space, w_tuple2)
+
+def le__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
+    return w_tuple1.le(space, w_tuple2)
+
+def ge__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
+    return w_tuple1.ge(space, w_tuple2)
+
+def gt__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
+    return w_tuple1.gt(space, w_tuple2)
+
 def hash__SpecialisedTuple(space, w_tuple):
     return w_tuple.hash(space)
 
diff --git a/pypy/objspace/std/test/test_specialisedtupleobject.py b/pypy/objspace/std/test/test_specialisedtupleobject.py
--- a/pypy/objspace/std/test/test_specialisedtupleobject.py
+++ b/pypy/objspace/std/test/test_specialisedtupleobject.py
@@ -117,29 +117,30 @@
 
     def test_neq(self):
         a = self.forbid_delegation((1,2))
-        b = (1,2)
+        b = (1,)
+        b = b+(2,)
         assert not a != b
         
-        c = (2,1)
+        c = (1,3)
         assert a != c
         
-        d = (1.0,2.0)
-        assert a != d
-        
-        e = ('r','s')
-        assert a != e
-        
-    def test_ordering (self):
+    def test_ordering(self):
         a = self.forbid_delegation((1,2))
         assert a <  (2,2)    
-        assert a <= (1,2)    
+        assert a <  (1,3)    
+        assert not a <  (1,2) 
+           
+        assert a <=  (2,2)    
+        assert a <=  (1,2) 
+        assert not a <=  (1,1) 
+           
+        assert a >= (0,2)    
         assert a >= (1,2)    
-        assert a >  (0,2)    
+        assert not a >= (1,3)    
         
-        assert a <  (1,3)    
-        assert a <= (1,2)    
-        assert a >= (1,2)    
-        assert a >  (1,1) 
+        assert a > (0,2)    
+        assert a > (1,1)    
+        assert not a > (1,3)    
         
     def test_hash(self):
         a = (1,2)


More information about the pypy-commit mailing list