From 328d83a08a5ae6759c6f26ede48aaca06c672f53 Mon Sep 17 00:00:00 2001 From: adnanmuttaleb Date: Tue, 29 Oct 2019 10:27:09 +0200 Subject: [PATCH] modified Vector v4 equality check --- 10-seq-hacking/vector_v4.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/10-seq-hacking/vector_v4.py b/10-seq-hacking/vector_v4.py index cb6d1e6..03e32c4 100644 --- a/10-seq-hacking/vector_v4.py +++ b/10-seq-hacking/vector_v4.py @@ -160,6 +160,7 @@ class Vector: def __init__(self, components): self._components = array(self.typecode, components) + self._hash = None def __iter__(self): return iter(self._components) @@ -177,12 +178,17 @@ def __bytes__(self): bytes(self._components)) def __eq__(self, other): - return (len(self) == len(other) and + return (hash(self) == hash(other) and len(self) == len(other) and all(a == b for a, b in zip(self, other))) def __hash__(self): + if self._hash: + return self._hash + hashes = (hash(x) for x in self) - return functools.reduce(operator.xor, hashes, 0) + self._hash = functools.reduce(operator.xor, hashes, 0) + + return self._hash def __abs__(self): return math.sqrt(sum(x * x for x in self))