From 3d6c15d16b1c66bfd1b4d6fadfa8ced308fef909 Mon Sep 17 00:00:00 2001 From: Ilya Gyrdymov Date: Sat, 8 Jun 2024 01:14:39 +0300 Subject: [PATCH] Make hash code more reliable --- lib/src/vector/float64x2_vector.g.dart | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/src/vector/float64x2_vector.g.dart b/lib/src/vector/float64x2_vector.g.dart index 0a75320..aa072da 100644 --- a/lib/src/vector/float64x2_vector.g.dart +++ b/lib/src/vector/float64x2_vector.g.dart @@ -121,6 +121,7 @@ class Float64x2Vector with IterableMixin implements Vector { final SimdHelper _simdHelper; late ByteBuffer _buffer; late int _numOfBuckets; + int _hash = 0; @override Iterator get iterator => _getTypedList().iterator; @@ -157,18 +158,24 @@ class Float64x2Vector with IterableMixin implements Vector { } @override - int get hashCode => _cache.get(vectorHashKey, () { - if (isEmpty) { - return 0; - } + int get hashCode { + if (isEmpty) { + return 0; + } + + if (_hash != 0) { + return _hash; + } - var i = 0; + _hash = 1; + final typesList = _getTypedList(); - final summed = _getSimdList() - .reduce((sum, element) => sum + element.scale((31 * (i++)) * 1.0)); + for (var i = 0; i < length; i++) { + _hash = _hash * 31 + typesList[i].hashCode; + } - return length + _simdHelper.sumLanesForHash(summed).hashCode; - }, skipCaching: false); + return _hash; + } @override Vector operator +(Object value) {