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 ///////////////////////////////////////////////////////////////////////////////
22 * The base class for hashtables of primitive values. Since there is
23 * no notion of object equality for primitives, it isn't possible to
24 * use a `REMOVED' object to track deletions in an open-addressed table.
25 * So, we have to resort to using a parallel `bookkeeping' array of bytes,
26 * in which flags can be set to indicate that a particular slot in the
27 * hash table is FREE, FULL, or REMOVED.
29 * Created: Fri Jan 11 18:55:16 2002
31 * @author Eric D. Friedman
32 * @version $Id: TPrimitiveHash.java,v 1.5 2008/10/08 16:39:10 robeden Exp $
35 abstract public class TPrimitiveHash extends THash {
36 /** flags indicating whether each position in the hash is
37 * FREE, FULL, or REMOVED */
38 protected transient byte[] _states;
40 /* constants used for state flags */
42 /** flag indicating that a slot in the hashtable is available */
43 protected static final byte FREE = 0;
45 /** flag indicating that a slot in the hashtable is occupied */
46 protected static final byte FULL = 1;
48 /** flag indicating that the value of a slot in the hashtable
50 protected static final byte REMOVED = 2;
53 * Creates a new <code>THash</code> instance with the default
54 * capacity and load factor.
56 public TPrimitiveHash() {
61 * Creates a new <code>TPrimitiveHash</code> instance with a prime
62 * capacity at or near the specified capacity and with the default
65 * @param initialCapacity an <code>int</code> value
67 public TPrimitiveHash(int initialCapacity) {
68 this(initialCapacity, DEFAULT_LOAD_FACTOR);
72 * Creates a new <code>TPrimitiveHash</code> instance with a prime
73 * capacity at or near the minimum needed to hold
74 * <tt>initialCapacity<tt> elements with load factor
75 * <tt>loadFactor</tt> without triggering a rehash.
77 * @param initialCapacity an <code>int</code> value
78 * @param loadFactor a <code>float</code> value
80 public TPrimitiveHash(int initialCapacity, float loadFactor) {
82 _loadFactor = loadFactor;
83 setUp(HashFunctions.fastCeil(initialCapacity / loadFactor));
86 public Object clone() {
87 TPrimitiveHash h = (TPrimitiveHash)super.clone();
88 h._states = (byte[])this._states.clone();
93 * Returns the capacity of the hash table. This is the true
94 * physical capacity, without adjusting for the load factor.
96 * @return the physical capacity of the hash table.
98 protected int capacity() {
99 return _states.length;
103 * Delete the record at <tt>index</tt>.
105 * @param index an <code>int</code> value
107 protected void removeAt(int index) {
108 _states[index] = REMOVED;
109 super.removeAt(index);
113 * initializes the hashtable to a prime capacity which is at least
114 * <tt>initialCapacity + 1</tt>.
116 * @param initialCapacity an <code>int</code> value
117 * @return the actual capacity chosen
119 protected int setUp(int initialCapacity) {
122 capacity = super.setUp(initialCapacity);
123 _states = new byte[capacity];