1 // Copyright (c) 1999 CERN - European Organization for Nuclear Research.
3 // Permission to use, copy, modify, distribute and sell this software and
4 // its documentation for any purpose is hereby granted without fee,
5 // provided that the above copyright notice appear in all copies and that
6 // both that copyright notice and this permission notice appear in
7 // supporting documentation. CERN makes no representations about the
8 // suitability of this software for any purpose. It is provided "as is"
9 // without expressed or implied warranty.
14 * Provides various hash functions.
16 * @author wolfgang.hoschek@cern.ch
17 * @version 1.0, 09/24/99
19 public final class HashFunctions {
21 * Returns a hashcode for the specified value.
23 * @return a hash code value for the specified value.
25 public static int hash(double value) {
26 assert !Double.isNaN(value) : "Values of NaN are not supported.";
28 long bits = Double.doubleToLongBits(value);
29 return (int)(bits ^ (bits >>> 32));
30 //return (int) Double.doubleToLongBits(value*663608941.737);
31 //this avoids excessive hashCollisions in the case values are
32 //of the form (1.0, 2.0, 3.0, ...)
36 * Returns a hashcode for the specified value.
38 * @return a hash code value for the specified value.
40 public static int hash(float value) {
41 assert !Float.isNaN(value) : "Values of NaN are not supported.";
43 return Float.floatToIntBits(value*663608941.737f);
44 // this avoids excessive hashCollisions in the case values are
45 // of the form (1.0, 2.0, 3.0, ...)
49 * Returns a hashcode for the specified value.
51 * @return a hash code value for the specified value.
53 public static int hash(int value) {
54 // Multiply by prime to make sure hash can't be negative (see Knuth v3, p. 515-516)
59 * Returns a hashcode for the specified value.
61 * @return a hash code value for the specified value.
63 public static int hash(long value) {
64 // Multiply by prime to make sure hash can't be negative (see Knuth v3, p. 515-516)
65 return ((int)(value ^ (value >>> 32))) * 31;
69 * Returns a hashcode for the specified object.
71 * @return a hash code value for the specified object.
73 public static int hash(Object object) {
74 return object==null ? 0 : object.hashCode();
79 * In profiling, it has been found to be faster to have our own local implementation
80 * of "ceil" rather than to call to {@link Math#ceil(double)}.
82 static int fastCeil( float v ) {
83 int possible_result = ( int ) v;
84 if ( v - possible_result > 0 ) possible_result++;
85 return possible_result;