Reputation: 4795
I need to hash tuples of integers (order is important) into integers with low probability of collision. I'm using the approach of converting integers to string, concatenate with ',' and obtain the string hash but it's being too slow.
Is there a way for getting fast hashes from tuples of integers?
Upvotes: 6
Views: 2351
Reputation: 262534
Here is what Java's Arrays.hashCode(int[]) does:
2938 public static int hashCode(int a[]) {
2939 if (a == null)
2940 return 0;
2941
2942 int result = 1;
2943 for (int element : a)
2944 result = 31 * result + element;
2945
2946 return result;
2947 }
This calculation is specified in the List interface. I don't know if it is collision-resistant enough for you, but it seems like a good place to start. It does take order into consideration (i.e. different order of the same numbers will yield a different hash value).
Upvotes: 4