|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||
java.lang.Object | +--com.sosnoski.util.PrimitiveHashBase
Base class for type-specific hash map and hash set classes using keys of primitive types. This base class implementation is based on a design using parallel arrays for "slot in use" flags and the actual key values, with type-specific subclasses implementing the key value hashing and comparisons. Subclasses may use additional parallel arrays beyond these two, 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.
ObjectHashBase| Field Summary | |
protected static double |
DEFAULT_FILL
Default fill fraction allowed before growing table. |
protected static int |
KEY_MULTIPLIER
Hash value multiplier to scramble bits before calculating slot. |
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 table. |
protected boolean[] |
m_flagTable
Array of slot occupied flags. |
protected int |
m_hitOffset
Offset added (modulo table size) to slot number on collision. |
protected static int |
MINIMUM_SIZE
Minimum size used for table. |
| Constructor Summary | |
PrimitiveHashBase(int count,
double fill,
java.lang.Class ktype)
Constructor with full specification. |
|
PrimitiveHashBase(PrimitiveHashBase 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 keys. |
protected int |
freeSlot(int slot)
Find free slot number for entry. |
protected abstract java.lang.Object |
getKeyArray()
Get the backing array of keys. |
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 keys. |
int |
size()
Get number of items in table. |
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 |
protected static final double DEFAULT_FILL
protected static final int MINIMUM_SIZE
protected static final int KEY_MULTIPLIER
protected double m_fillFraction
protected int m_entryCount
protected int m_entryLimit
protected int m_hitOffset
protected boolean[] m_flagTable
| Constructor Detail |
public PrimitiveHashBase(int count,
double fill,
java.lang.Class ktype)
count - number of values to assume in initial sizing of tablefill - fraction full allowed for table before growingktype - type of primitives used for keyspublic PrimitiveHashBase(PrimitiveHashBase 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 key 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 key
|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||