From 4eabec44af2e635408d9cba8672e947145cc6971 Mon Sep 17 00:00:00 2001 From: Vivien Maisonneuve Date: Wed, 2 Jul 2014 06:32:12 +0200 Subject: [PATCH 1/1] Improve hash functions in linexprs --- pypol/linexprs.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pypol/linexprs.py b/pypol/linexprs.py index d8b020d..3aef337 100644 --- a/pypol/linexprs.py +++ b/pypol/linexprs.py @@ -36,6 +36,7 @@ class Expression: '_constant', '_symbols', '_dimension', + '_hash', ) def __new__(cls, coefficients=None, constant=0): @@ -77,6 +78,7 @@ class Expression: self._constant = constant self._symbols = tuple(self._coefficients) self._dimension = len(self._symbols) + self._hash = hash((tuple(self._coefficients.items()), self._constant)) return self def coefficient(self, symbol): @@ -106,6 +108,9 @@ class Expression: def dimension(self): return self._dimension + def __hash__(self): + return self._hash + def isconstant(self): return False @@ -220,9 +225,6 @@ class Expression: from .polyhedra import Gt return Gt(self, other) - def __hash__(self): - return hash((tuple(self.coefficients()), self._constant)) - def _toint(self): lcm = functools.reduce(lambda a, b: a*b // gcd(a, b), [value.denominator for value in self.values()]) @@ -348,11 +350,12 @@ class Symbol(Expression): raise TypeError('name must be a string or a Symbol instance') name = name.strip() self = object().__new__(cls) - self._coefficients = {name: 1} + self._coefficients = OrderedDict([(name, 1)]) self._constant = 0 self._symbols = tuple(name) self._name = name self._dimension = 1 + self._hash = hash(self._name) return self @property @@ -398,9 +401,10 @@ class Constant(Expression): self._constant = numerator.constant else: self._constant = Fraction(numerator, denominator) - self._coefficients = {} + self._coefficients = OrderedDict() self._symbols = () self._dimension = 0 + self._hash = hash(self._constant) return self def isconstant(self): -- 2.20.1