Skip to content

Commit

Permalink
Make hash code more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed Jun 7, 2024
1 parent 7fb1c34 commit 3d6c15d
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/src/vector/float64x2_vector.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
final SimdHelper<Float64x2> _simdHelper;
late ByteBuffer _buffer;
late int _numOfBuckets;
int _hash = 0;

@override
Iterator<double> get iterator => _getTypedList().iterator;
Expand Down Expand Up @@ -157,18 +158,24 @@ class Float64x2Vector with IterableMixin<double> 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) {
Expand Down

0 comments on commit 3d6c15d

Please sign in to comment.