01: package it.unimi.dsi.fastutil;
02:
03: /*
04: * fastutil: Fast & compact type-specific collections for Java
05: *
06: * Copyright (C) 2002-2008 Sebastiano Vigna
07: *
08: * This library is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU Lesser General Public
10: * License as published by the Free Software Foundation; either
11: * version 2.1 of the License, or (at your option) any later version.
12: *
13: * This library is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: * Lesser General Public License for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public
19: * License along with this library; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: *
22: */
23:
24: /** Common code for all hash-based classes.
25: *
26: * All hashing in <code>fastutil</code> is performed starting from a 32-bit integer
27: * associated to a key or value. For all integer types smaller than <code>long</code>, we
28: * just cast. In all other cases, we do some conversion using static code in this
29: * class. Note that we follow the conventions established by the various classes
30: * associated to primitive types ({@link Boolean}, {@link Double}, etc.).
31: */
32:
33: public class HashCommon {
34:
35: /** This reference is used to fill keys and values of removed entries (if
36: they are objects). <code>null</code> cannot be used as it would confuse the
37: search algorithm in the presence of an actual <code>null</code> key. */
38: public static final Object REMOVED = new Object();
39:
40: private HashCommon() {
41: };
42:
43: /** Returns the hash code that would be returned by {@link Float#hashCode()}.
44: *
45: * @return the same code as {@link Float#hashCode() new Float(f).hashCode()}.
46: */
47:
48: final public static int float2int(final float f) {
49: return Float.floatToRawIntBits(f);
50: }
51:
52: /** Returns the hash code that would be returned by {@link Double#hashCode()}.
53: *
54: * @return the same code as {@link Double#hashCode() new Double(f).hashCode()}.
55: */
56:
57: final public static int double2int(final double d) {
58: final long l = Double.doubleToRawLongBits(d);
59: return (int) (l ^ (l >>> 32));
60: }
61:
62: /** Returns the hash code that would be returned by {@link Long#hashCode()}.
63: *
64: * @return the same code as {@link Long#hashCode() new Long(f).hashCode()}.
65: */
66: final public static int long2int(final long l) {
67: return (int) (l ^ (l >>> 32));
68: }
69: }
|