KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > HashFunctions


1 // Copyright (c) 1999 CERN - European Organization for Nuclear Research.
2

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.
10

11 package gnu.trove;
12
13 /**
14  * Provides various hash functions.
15  *
16  * @author wolfgang.hoschek@cern.ch
17  * @version 1.0, 09/24/99
18  */

19 public final class HashFunctions {
20     /**
21      * Returns a hashcode for the specified value.
22      *
23      * @return a hash code value for the specified value.
24      */

25     public static final int hash(double value) {
26         long bits = Double.doubleToLongBits(value);
27         return (int)(bits ^ (bits >>> 32));
28         //return (int) Double.doubleToLongBits(value*663608941.737);
29
//this avoids excessive hashCollisions in the case values are
30
//of the form (1.0, 2.0, 3.0, ...)
31
}
32
33     /**
34      * Returns a hashcode for the specified value.
35      *
36      * @return a hash code value for the specified value.
37      */

38     public static final int hash(float value) {
39         return Float.floatToIntBits(value*663608941.737f);
40         // this avoids excessive hashCollisions in the case values are
41
// of the form (1.0, 2.0, 3.0, ...)
42
}
43
44     /**
45      * Returns a hashcode for the specified value. The hashcode is computed as
46      * <blockquote><pre>
47      * 31^5*(d[0]*31^(n-1) + d[1]*31^(n-2) + ... + d[n-1])
48      * </pre></blockquote>
49      * using <code>int</code> arithmetic, where <code>d[i]</code> is
50      * the <i>i</i>th digit of the value, counting from the right,
51      * <code>n</code> is the number of decimal digits of the specified
52      * value, and <code>^</code> indicates exponentiation. (The hash
53      * value of the value zero is zero.)
54      *
55      * @return a hash code value for the specified value.
56      */

57     public static final int hash(int value) {
58         //return value * 0x278DDE6D; // see cern.jet.random.engine.DRand
59

60         return value;
61     
62         /*
63           value &= 0x7FFFFFFF; // make it >=0
64           int hashCode = 0;
65           do hashCode = 31*hashCode + value%10;
66           while ((value /= 10) > 0);
67
68           return 28629151*hashCode; // spread even further; h*31^5
69         */

70     }
71
72     /**
73      * Returns a hashcode for the specified value.
74      *
75      * @return a hash code value for the specified value.
76      */

77     public static final int hash(long value) {
78         return (int)(value ^ (value >> 32));
79
80         /*
81          * The hashcode is computed as
82          * <blockquote><pre>
83          * 31^5*(d[0]*31^(n-1) + d[1]*31^(n-2) + ... + d[n-1])
84          * </pre></blockquote>
85          * using <code>int</code> arithmetic, where <code>d[i]</code> is the
86          * <i>i</i>th digit of the value, counting from the right, <code>n</code> is the number of decimal digits of the specified value,
87          * and <code>^</code> indicates exponentiation.
88          * (The hash value of the value zero is zero.)
89  
90          value &= 0x7FFFFFFFFFFFFFFFL; // make it >=0 (0x7FFFFFFFFFFFFFFFL==Long.MAX_VALUE)
91          int hashCode = 0;
92          do hashCode = 31*hashCode + (int) (value%10);
93          while ((value /= 10) > 0);
94
95          return 28629151*hashCode; // spread even further; h*31^5
96         */

97     }
98
99     /**
100      * Returns a hashcode for the specified object.
101      *
102      * @return a hash code value for the specified object.
103      */

104     public static final int hash(Object JavaDoc object) {
105         return object==null ? 0 : object.hashCode();
106     }
107 }
108
Popular Tags