1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import gnu.trove.impl.hash.THash;
16 import java.lang.reflect.Array;
20 * An open addressed hashing implementation for Object types.
22 * Created: Sun Nov 4 08:56:06 2001
24 * @author Eric D. Friedman
25 * @version $Id: UnaryQueryHash.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
27 abstract public class DoubleKeyQueryHash<Procedure> extends THash {
28 static final long serialVersionUID = -3461112548087185871L;
30 /** the set of Objects */
31 protected transient DoubleKeyValueMap<Procedure>[] _set;
33 protected final DoubleKeyValueMap<Procedure> REMOVED = new DoubleKeyValueMap<Procedure>(-1);
36 * Creates a new <code>TObjectHash</code> instance with the
37 * default capacity and load factor.
39 public DoubleKeyQueryHash() {
40 super(DEFAULT_CAPACITY, 0.75f);
43 public int capacity() {
47 protected void removeAt(int index) {
48 _set[index] = REMOVED;
49 super.removeAt(index);
53 * initializes the Object set of this hash table.
55 * @param initialCapacity an <code>int</code> value
56 * @return an <code>int</code> value
58 @SuppressWarnings("unchecked")
59 protected int setUp(int initialCapacity) {
62 capacity = super.setUp(initialCapacity);
63 _set = (DoubleKeyValueMap[])Array.newInstance(DoubleKeyValueMap.class, capacity);
68 protected int index(final int id) {
70 final DoubleKeyValueMap<Procedure>[] set = _set;
71 final int length = set.length;
72 final int hash = (31 * id) & 0x7fffffff;
73 int index = hash % length;
74 DoubleKeyValueMap<Procedure> cur = set[index];
76 if ( cur == null ) return -1;
78 // NOTE: here it has to be REMOVED or FULL (some user-given value)
79 if ( cur == REMOVED || !(id == cur.id)) {
81 final int probe = 1 + (hash % (length - 2));
90 && (cur == REMOVED || !(id == cur.id)));
93 return cur == null ? -1 : index;
97 final protected DoubleKeyValueMap<Procedure> index2(final int id) {
99 final DoubleKeyValueMap<Procedure>[] set = _set;
100 final int length = set.length;
101 final int hash = (31 * id) & 0x7fffffff;
102 int index = hash % length;
103 DoubleKeyValueMap<Procedure> cur = set[index];
105 if ( cur == null ) return null;
107 // NOTE: here it has to be REMOVED or FULL (some user-given value)
108 if ( cur == REMOVED || (id != cur.id)) {
110 final int probe = 1 + (hash % (length - 2));
119 && (cur == REMOVED || (id != cur.id)));
128 * Locates the index at which <tt>obj</tt> can be inserted. if
129 * there is already a value equal()ing <tt>obj</tt> in the set,
130 * returns that value's index as <tt>-index - 1</tt>.
132 * @param obj an <code>Object</code> value
133 * @return the index of a FREE slot at which obj can be inserted
134 * or, if obj is already stored in the hash, the negative value of
135 * that index, minus 1: -index -1.
137 protected int insertionIndex(final int id) {
139 final DoubleKeyValueMap<Procedure>[] set = _set;
140 final int length = set.length;
141 final int hash = (31 * id) & 0x7fffffff;
142 int index = hash % length;
143 DoubleKeyValueMap<Procedure> cur = set[index];
146 return index; // empty, all done
147 } else if (cur != REMOVED && (id == cur.id)) {
148 return -index -1; // already stored
149 } else { // already FULL or REMOVED, must probe
150 // compute the double hash
151 final int probe = 1 + (hash % (length - 2));
153 // if the slot we landed on is FULL (but not removed), probe
154 // until we find an empty slot, a REMOVED slot, or an element
155 // equal to the one we are trying to insert.
156 // finding an empty slot means that the value is not present
157 // and that we should use that slot as the insertion point;
158 // finding a REMOVED slot means that we need to keep searching,
159 // however we want to remember the offset of that REMOVED slot
160 // so we can reuse it in case a "new" insertion (i.e. not an update)
162 // finding a matching value means that we've found that our desired
163 // key is already in the table
164 if (cur != REMOVED) {
165 // starting at the natural offset, probe until we find an
166 // offset that isn't full.
175 && ! (id == cur.id));
178 // if the index we found was removed: continue probing until we
179 // locate a free location or an element which equal()s the
181 if (cur == REMOVED) {
182 int firstRemoved = index;
184 && (cur == REMOVED || ! (id == cur.id))) {
191 // NOTE: cur cannot == REMOVED in this block
192 return (cur != null) ? -index -1 : firstRemoved;
194 // if it's full, the key is already stored
195 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
196 return (cur != null) ? -index -1 : index;
200 protected int insertionIndex2(final int id, final DoubleKeyValueMap<Procedure>[] set) {
202 final int length = set.length;
203 final int hash = (31 * id) & 0x7fffffff;
204 int index = hash % length;
205 DoubleKeyValueMap<Procedure> cur = set[index];
208 return index; // empty, all done
209 } else if (cur != REMOVED && (id == cur.id)) {
210 return -index -1; // already stored
211 } else { // already FULL or REMOVED, must probe
212 // compute the double hash
213 final int probe = 1 + (hash % (length - 2));
215 // if the slot we landed on is FULL (but not removed), probe
216 // until we find an empty slot, a REMOVED slot, or an element
217 // equal to the one we are trying to insert.
218 // finding an empty slot means that the value is not present
219 // and that we should use that slot as the insertion point;
220 // finding a REMOVED slot means that we need to keep searching,
221 // however we want to remember the offset of that REMOVED slot
222 // so we can reuse it in case a "new" insertion (i.e. not an update)
224 // finding a matching value means that we've found that our desired
225 // key is already in the table
226 if (cur != REMOVED) {
227 // starting at the natural offset, probe until we find an
228 // offset that isn't full.
237 && ! (id == cur.id));
240 // if the index we found was removed: continue probing until we
241 // locate a free location or an element which equal()s the
243 if (cur == REMOVED) {
244 int firstRemoved = index;
246 && (cur == REMOVED || ! (id == cur.id))) {
253 // NOTE: cur cannot == REMOVED in this block
254 return (cur != null) ? -index -1 : firstRemoved;
256 // if it's full, the key is already stored
257 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
258 return (cur != null) ? -index -1 : index;
263 * Convenience methods for subclasses to use in throwing exceptions about
264 * badly behaved user objects employed as keys. We have to throw an
265 * IllegalArgumentException with a rather verbose message telling the
266 * user that they need to fix their object implementation to conform
267 * to the general contract for java.lang.Object.
269 * @param o1 the first of the equal elements with unequal hash codes.
270 * @param o2 the second of the equal elements with unequal hash codes.
271 * @exception IllegalArgumentException the whole point of this method.
273 protected final void throwObjectContractViolation(Object o1, Object o2)
274 throws IllegalArgumentException {
275 throw new IllegalArgumentException("Equal objects must have equal hashcodes. "
276 + "During rehashing, Trove discovered that "
277 + "the following two objects claim to be "
278 + "equal (as in java.lang.Object.equals()) "
279 + "but their hashCodes (or those calculated by "
280 + "your TObjectHashingStrategy) are not equal."
281 + "This violates the general contract of "
282 + "java.lang.Object.hashCode(). See bullet point two "
283 + "in that method's documentation. "
285 + "; object #2 =" + o2);