]> hydra-www.ietfng.org Git - dyna2/commitdiff
replace cons comparison operator with something much faster.
authorTim Vieira <tim.f.vieira@gmail.com>
Thu, 1 Aug 2013 20:31:36 +0000 (16:31 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Thu, 1 Aug 2013 20:31:36 +0000 (16:31 -0400)
src/Dyna/Backend/Python/term.py

index b89ca7e3f975d4de57a4764162d1b78a80bd27a0..6166dd5703ebd970152925569e4a61606b695f10 100644 (file)
@@ -17,6 +17,8 @@ class Term(object):
         return self is other
 
     def __cmp__(self, other):
+        if self is other:
+            return 0
         try:
             return cmp((self.fn, self.args), (other.fn, other.args))
         except AttributeError:
@@ -52,6 +54,15 @@ class Cons(NoIntern, Term):
         self.aggregator = NoAggregator
         self.aslist = [self.head] + self.tail.aslist
 
+    def __cmp__(self, other):
+        try:
+            if other.fn == 'cons/2':
+                return cmp(self.aslist, other.aslist)   # faster
+            else:
+                return cmp(self.fn, other.fn)
+        except AttributeError:
+            return 1
+
     def __repr__(self):
         return '[%s]' % (', '.join(map(_repr, self.aslist)))
 
@@ -81,9 +92,6 @@ class _Nil(Term):
     def __repr__(self):
         return '[]'
 
-#    def __contains__(self, x):
-#        return false
-
     def like_chart(self):
         return iter([])