Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

modified Vector v4 equality check #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions 10-seq-hacking/vector_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down