From a5d6fc7f0759384b4bfd5608c047492f4756282c Mon Sep 17 00:00:00 2001 From: Tim Vieira Date: Thu, 1 Aug 2013 16:31:36 -0400 Subject: [PATCH] replace cons comparison operator with something much faster. --- src/Dyna/Backend/Python/term.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Dyna/Backend/Python/term.py b/src/Dyna/Backend/Python/term.py index b89ca7e..6166dd5 100644 --- a/src/Dyna/Backend/Python/term.py +++ b/src/Dyna/Backend/Python/term.py @@ -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([]) -- 2.50.1