|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||
java.lang.Object | +--com.sosnoski.util.ObjectHashBase
Base class for type-specific hash map and hash set classes using
object keys. This class uses a single array for keys with null
values in unused slots. Subclasses may use additional arrays in parallel to
this key array, and the internal operation is designed to allow for this
case.
Hash classes based on this class are unsynchronized in order to provide the best possible performance for typical usage scenarios, so explicit synchronization must be implemented by the subclass or the application in cases where they are to be modified in a multithreaded environment.
This implementation uses the standard naive hash technique of adding a fixed offset to the computed position in the table when a collision occurs, so that several checks may potentially be necessary in order to determine if a particular key value is present in the table (since if the slot calculated by the hash function is occupied by a different entry it's still necessary to check the offset slot for a match, and then the offset slot from that, until an empty slot is found).
Each time the number of entries present in the table grows above the specified fraction full the table is expanded to twice its prior size plus one (to ensure the size is always an odd number). The collision offset is set to half the table size rounded down, ensuring that it will cycle through all slots of the table before returning to the original slot.
Subclasses need to implement the abstract methods defined by this class for working with the key array, as well as the actual data access methods.
| Field Summary | |
protected static double |
DEFAULT_FILL
Default fill fraction allowed before growing table. |
static java.lang.String |
IDENTITY_COMP
Standard hash with object identity comparison technique specifier. |
static java.lang.String |
IDENTITY_HASH
Identity hash with object identity comparison technique specifier. |
protected int |
m_arraySize
Size of array used for keys. |
protected int |
m_entryCount
Number of entries present in table. |
protected int |
m_entryLimit
Entries allowed before growing table. |
protected double |
m_fillFraction
Fill fraction allowed for this hash table. |
protected int |
m_hitOffset
Offset added (modulo table size) to slot number on collision. |
protected boolean |
m_identCompare
Use identity comparison flag. |
protected boolean |
m_identHash
Use identity hash flag. |
protected static int |
MINIMUM_SIZE
Minimum size used for hash table. |
static java.lang.String |
STANDARD_HASH
Standard hashing technique specifier. |
| Constructor Summary | |
ObjectHashBase(int count,
double fill,
java.lang.Class type,
java.lang.Object tech)
Constructor with full specification. |
|
ObjectHashBase(ObjectHashBase base)
Copy (clone) constructor. |
|
| Method Summary | |
void |
clear()
Set the table to the empty state. |
void |
ensureCapacity(int min)
Ensure that the table has the capacity for at least the specified number of items. |
protected int |
freeSlot(int slot)
Find free slot number for entry. |
protected abstract java.lang.Object[] |
getKeyArray()
Get the backing array of items. |
protected void |
growCapacity(int min)
Increase table capacity. |
protected abstract void |
reallocate(int size)
Resize the base arrays after a size change. |
protected abstract void |
setKeyArray(java.lang.Object array)
Set the backing array of items. |
int |
size()
Get number of items in table. |
protected int |
standardFind(java.lang.Object key)
Standard find key in table. |
protected int |
standardSlot(java.lang.Object key)
Standard base slot computation for a key. |
protected int |
stepSlot(int slot)
Step the slot number for an entry. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final java.lang.String STANDARD_HASH
public static final java.lang.String IDENTITY_COMP
public static final java.lang.String IDENTITY_HASH
protected static final double DEFAULT_FILL
protected static final int MINIMUM_SIZE
protected final boolean m_identHash
protected final boolean m_identCompare
protected final double m_fillFraction
protected int m_entryCount
protected int m_entryLimit
protected int m_arraySize
protected int m_hitOffset
| Constructor Detail |
public ObjectHashBase(int count,
double fill,
java.lang.Class type,
java.lang.Object tech)
count - number of values to assume in initial sizing of tablefill - fraction full allowed for table before growingtype - type of keys contained in tabletech - hash technique specifier (one of STANDARD_HASH,
IDENTITY_COMP, or IDENTITY_HASH)public ObjectHashBase(ObjectHashBase base)
base - instance being copied| Method Detail |
public final int size()
protected abstract java.lang.Object[] getKeyArray()
protected abstract void setKeyArray(java.lang.Object array)
array - backing item array objectprotected abstract void reallocate(int size)
size - new size for base arraysprotected void growCapacity(int min)
min - minimum new table capacitypublic final void ensureCapacity(int min)
min - minimum capacity to be guaranteedpublic void clear()
protected final int stepSlot(int slot)
slot - slot number to be steppedprotected final int freeSlot(int slot)
slot - initial slot computed from keyprotected final int standardSlot(java.lang.Object key)
hashCode() method
defined for the key objects or the System.identityHashCode()
method, as selected by the hash technique constructor parameter. To
implement a hash class based on some other methods of hashing and/or
equality testing, define a separate method in the subclass with a
different name and use that method instead. This avoids the overhead
caused by overrides of a very heavily used method.key - key value to be computedprotected int standardFind(java.lang.Object key)
hashCode() method defined for the
key objects or the System.identityHashCode() method, and
either the equals() method defined for the key objects or
the == operator, as selected by the hash technique
constructor parameter. To implement a hash class based on some other
methods of hashing and/or equality testing, define a separate method in
the subclass with a different name and use that method instead. This
avoids the overhead caused by overrides of a very heavily used method.key - to be found in table-index-1 of slot to be
used for inserting key in table if not already present (always negative)
|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||