jifalops / dart-latlong

Other
47 stars 21 forks source link

improved LatLng.hashCode #15

Open ristiisa opened 2 months ago

ristiisa commented 2 months ago

Description:

This PR updates the hashCode implementation for the LatLng class to use the XOR strategy. This approach aims to reduce hash code collisions by providing a better distribution of hash codes compared to simple addition.

Changes:

Updated the hashCode method in the LatLng class to use XOR (latitude.hashCode ^ longitude.hashCode).

Benefits:

Reduced hash code collisions, leading to more efficient use of hash maps and sets. Improved overall performance in collections that rely on hash codes.

Collision Example:

With the previous implementation using addition, the following pairs of coordinates produce the same hash code:

(latitude: 1.0, longitude: 2.0) (latitude: 2.0, longitude: 1.0) Both pairs will produce the same hash code since 1.hashCode + 2.hashCode == 2.hashCode + 1.hashCode.

By using the XOR strategy, these pairs will generate different hash codes, reducing the likelihood of collisions.