Skip to content

Commit

Permalink
{beam_state}: replace Object.hash with _SystemHash.hash2
Browse files Browse the repository at this point in the history
  • Loading branch information
slovnicki committed May 16, 2023
1 parent aec739f commit 97643b4
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion package/lib/src/beam_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ class BeamState with RouteInformationSerializable<BeamState> {
);

@override
int get hashCode => Object.hash(uri, json.encode(routeState));
int get hashCode =>
_SystemHash.hash2(uri.hashCode, routeState.hashCode, _hashSeed);

@override
bool operator ==(Object other) {
Expand All @@ -205,3 +206,28 @@ class BeamState with RouteInformationSerializable<BeamState> {
json.encode(other.routeState) == json.encode(routeState);
}
}

final int _hashSeed = identityHashCode(Object);

// Copied from dart._internal because Object.hash is unavailable in Dart <2.14
// Used for BeamState.hashCode
class _SystemHash {
static int combine(int hash, int value) {
hash = 0x1fffffff & (hash + value);
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}

static int finish(int hash) {
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}

static int hash2(int v1, int v2, [int seed = 0]) {
int hash = seed;
hash = combine(hash, v1);
hash = combine(hash, v2);
return finish(hash);
}
}

0 comments on commit 97643b4

Please sign in to comment.