Vector3f hashCode()

I notice that the Vector3f class implements equals but not hashCode (which violates the equals-hashCode contract).



I’m computing shadow volumes, and it would be handy if Vector3f had a compliant hashCode method, so I could use a Set to reduce potentially duplicate TriMesh vertices to a unique set, which makes figuring out triangle adjacency (to define potentially shadowing edges) faster (as I can then use == instead of .equals()).

You’re right, and this is my fault. I’ll get that in right away. Sorry about that.

There’s a handy hash code utility that I get a lot of use from. Using this utility, Vector3f’s hashCode method would be:


public int hashCode()
{
   int hash = HashCodeUtil.SEED;
   hash = HashCodeUtil.hash( hash, x );
   hash = HashCodeUtil.hash( hash, y );
   hash = HashCodeUtil.hash( hash, z );
   return hash;
}

Interesting util, I’ll have to look into that…



for now I just used the basic:



int hash = 17;

hash += 37 * x;

hash += 37 * y;

hash += 37 * z;



I’m always willing to learn better ways though.

Thanks for adding that in!



HashCodeUtil isn’t better per se, but it makes hashCode methods easy to implement well - a bad hashing algorithm can really hurt collection performance.

good point. the three classes that do hashcode (Vector2f and 3f as well as ColorRGBA) have been updated.

://