From: Tim Vieira Date: Thu, 1 Aug 2013 20:31:36 +0000 (-0400) Subject: replace cons comparison operator with something much faster. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=a5d6fc7f0759384b4bfd5608c047492f4756282c;p=dyna2 replace cons comparison operator with something much faster. --- 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([])