1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 ///////////////////////////////////////////////////////////////////////////////
21 //////////////////////////////////////////////////
22 // THIS IS A GENERATED CLASS. DO NOT HAND EDIT! //
23 //////////////////////////////////////////////////
27 * An open addressed hashing implementation for int primitives.
29 * Created: Sun Nov 4 08:56:06 2001
31 * @author Eric D. Friedman
32 * @version $Id: PHash.template,v 1.2 2007/06/29 22:39:46 robeden Exp $
35 abstract public class TIntHash extends TPrimitiveHash implements TIntHashingStrategy {
37 /** the set of ints */
38 protected transient int[] _set;
40 /** strategy used to hash values in this collection */
41 protected TIntHashingStrategy _hashingStrategy;
44 * Creates a new <code>TIntHash</code> instance with the default
45 * capacity and load factor.
49 this._hashingStrategy = this;
53 * Creates a new <code>TIntHash</code> instance whose capacity
54 * is the next highest prime above <tt>initialCapacity + 1</tt>
55 * unless that value is already prime.
57 * @param initialCapacity an <code>int</code> value
59 public TIntHash(int initialCapacity) {
60 super(initialCapacity);
61 this._hashingStrategy = this;
65 * Creates a new <code>TIntHash</code> instance with a prime
66 * value at or near the specified capacity and load factor.
68 * @param initialCapacity used to find a prime capacity for the table.
69 * @param loadFactor used to calculate the threshold over which
70 * rehashing takes place.
72 public TIntHash(int initialCapacity, float loadFactor) {
73 super(initialCapacity, loadFactor);
74 this._hashingStrategy = this;
78 * Creates a new <code>TIntHash</code> instance with the default
79 * capacity and load factor.
80 * @param strategy used to compute hash codes and to compare keys.
82 public TIntHash(TIntHashingStrategy strategy) {
84 this._hashingStrategy = strategy;
88 * Creates a new <code>TIntHash</code> instance whose capacity
89 * is the next highest prime above <tt>initialCapacity + 1</tt>
90 * unless that value is already prime.
92 * @param initialCapacity an <code>int</code> value
93 * @param strategy used to compute hash codes and to compare keys.
95 public TIntHash(int initialCapacity, TIntHashingStrategy strategy) {
96 super(initialCapacity);
97 this._hashingStrategy = strategy;
101 * Creates a new <code>TIntHash</code> instance with a prime
102 * value at or near the specified capacity and load factor.
104 * @param initialCapacity used to find a prime capacity for the table.
105 * @param loadFactor used to calculate the threshold over which
106 * rehashing takes place.
107 * @param strategy used to compute hash codes and to compare keys.
109 public TIntHash(int initialCapacity, float loadFactor, TIntHashingStrategy strategy) {
110 super(initialCapacity, loadFactor);
111 this._hashingStrategy = strategy;
115 * @return a deep clone of this collection
117 public Object clone() {
118 TIntHash h = (TIntHash)super.clone();
119 h._set = (int[])this._set.clone();
124 * initializes the hashtable to a prime capacity which is at least
125 * <tt>initialCapacity + 1</tt>.
127 * @param initialCapacity an <code>int</code> value
128 * @return the actual capacity chosen
130 protected int setUp(int initialCapacity) {
133 capacity = super.setUp(initialCapacity);
134 _set = new int[capacity];
139 * Searches the set for <tt>val</tt>
141 * @param val an <code>int</code> value
142 * @return a <code>boolean</code> value
144 public boolean contains(int val) {
145 return index(val) >= 0;
149 * Executes <tt>procedure</tt> for each element in the set.
151 * @param procedure a <code>TObjectProcedure</code> value
152 * @return false if the loop over the set terminated because
153 * the procedure returned false for some value.
155 public boolean forEach(TIntProcedure procedure) {
156 byte[] states = _states;
158 for (int i = set.length; i-- > 0;) {
159 if (states[i] == FULL && ! procedure.execute(set[i])) {
167 * Releases the element currently stored at <tt>index</tt>.
169 * @param index an <code>int</code> value
171 protected void removeAt(int index) {
172 _set[index] = (int)0;
173 super.removeAt(index);
177 * Locates the index of <tt>val</tt>.
179 * @param val an <code>int</code> value
180 * @return the index of <tt>val</tt> or -1 if it isn't in the set.
182 protected int index(int val) {
183 int hash, probe, index, length;
185 final byte[] states = _states;
186 final int[] set = _set;
187 length = states.length;
188 hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff;
189 index = hash % length;
191 if (states[index] != FREE &&
192 (states[index] == REMOVED || set[index] != val)) {
194 probe = 1 + (hash % (length - 2));
201 } while (states[index] != FREE &&
202 (states[index] == REMOVED || set[index] != val));
205 return states[index] == FREE ? -1 : index;
209 * Locates the index at which <tt>val</tt> can be inserted. if
210 * there is already a value equal()ing <tt>val</tt> in the set,
211 * returns that value as a negative integer.
213 * @param val an <code>int</code> value
214 * @return an <code>int</code> value
216 protected int insertionIndex(int val) {
217 int hash, probe, index, length;
219 final byte[] states = _states;
220 final int[] set = _set;
221 length = states.length;
222 hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff;
223 index = hash % length;
225 if (states[index] == FREE) {
226 return index; // empty, all done
227 } else if (states[index] == FULL && set[index] == val) {
228 return -index -1; // already stored
229 } else { // already FULL or REMOVED, must probe
230 // compute the double hash
231 probe = 1 + (hash % (length - 2));
233 // if the slot we landed on is FULL (but not removed), probe
234 // until we find an empty slot, a REMOVED slot, or an element
235 // equal to the one we are trying to insert.
236 // finding an empty slot means that the value is not present
237 // and that we should use that slot as the insertion point;
238 // finding a REMOVED slot means that we need to keep searching,
239 // however we want to remember the offset of that REMOVED slot
240 // so we can reuse it in case a "new" insertion (i.e. not an update)
242 // finding a matching value means that we've found that our desired
243 // key is already in the table
245 if (states[index] != REMOVED) {
246 // starting at the natural offset, probe until we find an
247 // offset that isn't full.
253 } while (states[index] == FULL && set[index] != val);
256 // if the index we found was removed: continue probing until we
257 // locate a free location or an element which equal()s the
259 if (states[index] == REMOVED) {
260 int firstRemoved = index;
261 while (states[index] != FREE &&
262 (states[index] == REMOVED || set[index] != val)) {
268 return states[index] == FULL ? -index -1 : firstRemoved;
270 // if it's full, the key is already stored
271 return states[index] == FULL ? -index -1 : index;
276 * Default implementation of TIntHashingStrategy:
277 * delegates hashing to HashFunctions.hash(int).
279 * @param val the value to hash
280 * @return the hashcode.
282 public final int computeHashCode(int val) {
283 return HashFunctions.hash(val);