Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hash code collision for Vector2D and Vector3D #227

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Commits on Jan 16, 2024

  1. Fix hash code collision for Vector2D and Vector3D

    This commit modifies the `hashCode()` method for `Vector2D` and the
    `Vector3D` classes in order to ensure unique values are generated for
    non-identical instances of vectors.
    
    The proposed approach for calculating the hash code value is the same
    that is used in JTS and HE_Mesh projects. And as quoted in JTS project,
    this approach was initially 'borrowed' from the Joshua Bloch book
    'Effective Java' (1st ed, 2008).
    
    In the "Item 9: Always override hashCode when you override equals" he
    describes the recipe as follows:
    
    1. Store some constant nonzero value, say 17, in an int variable called
       `result`.
    2. For each significant field `f` in your object, do the following:
       a. Compute an int hash code `c` for the field:
          i   ...
          ii  ...
          iii If the field is a long, compute (int)(f^(f>>>32))
          iv  ...
          v   If the field is a double, compute Double.doublToLongBits(f),
              and then hash the resulting long as in step 2.a.iii
          vi  ...
          vii ...
       b. Combine the hash code `c` computed in step 2.a into `result` as
          follows:
            result = 31 * result + c
    ivanshuba committed Jan 16, 2024
    Configuration menu
    Copy the full SHA
    93a01fa View commit details
    Browse the repository at this point in the history

Commits on Jan 17, 2024

  1. Configuration menu
    Copy the full SHA
    d313086 View commit details
    Browse the repository at this point in the history
  2. Make the Vector2D and Vector3D hashCode method more readable

    This commit uses calls to Double.hashCode() method in order to make the
    code more readable while keeping the overall approach for calculating
    the hash code value.
    ivanshuba committed Jan 17, 2024
    Configuration menu
    Copy the full SHA
    ddb75f0 View commit details
    Browse the repository at this point in the history

Commits on Jan 18, 2024

  1. Modify GreatCircle hashCode calculation

    This commit uses the same approach we've used in Vector2D and Vector3D
    to calculate hash code value for the GreatCircle class instances.
    
    Previously, GreatCircle.hashCode was using the Objects.hash(Object ...)
    wrapper method. In its turn, it has called the Arrays.hashCode(Object[])
    method.
    Unfortunately, this does not prevent hash collisions calculated for the
    GreatCircle instances. Not investigated the cause yet. As a guess, this
    might be related to that the Precision.DoubleEquivalence's hashCode()
    method is not overridden.
    ivanshuba committed Jan 18, 2024
    Configuration menu
    Copy the full SHA
    448c2d3 View commit details
    Browse the repository at this point in the history

Commits on Jan 30, 2024

  1. Revert GreatCircle hash code calculation

    Modifid GreatCircleTest instead to allow all the test successfully pass.
    
    See this comment thread for details:
    apache#227 (review)
    ivanshuba committed Jan 30, 2024
    Configuration menu
    Copy the full SHA
    0341398 View commit details
    Browse the repository at this point in the history

Commits on Jan 31, 2024

  1. Add changes requested in PR review

    - in Vector3D.hashCode():
      - use Double.hashCode() instead of the Double.doubleToLongBits()
      - revert Javadoc formatting
    
    - in Vector2D.hashCode(), revert Javadoc formatting
    ivanshuba committed Jan 31, 2024
    Configuration menu
    Copy the full SHA
    39c7ef0 View commit details
    Browse the repository at this point in the history