Package org.jibx.util
Class ReferenceCountMap
java.lang.Object
org.jibx.util.ReferenceCountMap
Hash map for counting references to
Object
keys. The map implementation is not very efficient when
resizing, but works well when the size of the map is known in advance or when accesses are substantially more common
than adds.- Author:
- Dennis M. Sosnoski
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double
Default fill fraction allowed before growing table.private int
Size of array used for keys.private int
Number of entries present in table.private int
Entries allowed before growing table.private int
Offset added (modulo table size) to slot number on collision.private Object[]
Array of key table slots.private int[]
Array of value table slots.private static final int
Minimum size used for hash table. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.ReferenceCountMap
(int count) Constructor with count.Copy (clone) constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate int
assignSlot
(Object key, int value) Assign slot for entry.void
clear()
Clear all keys and counts.clone()
Construct a copy of the table.private final int
freeSlot
(int slot) Find free slot number for entry.final int
Find an entry in the table.int
incrementCount
(Object key) Increment a use count in the table.private void
internalRemove
(int slot) Internal remove pair from the table.iterator()
Get iterator for keys in map.Object[]
keyArray()
Get array of keys in map.private boolean
reinsert
(int slot) Reinsert an entry into the hash map.private void
restructure
(Object[] keys, int[] values) Restructure the table.int
size()
Get number of entries in map.private int
standardFind
(Object key) Standard find key in table.private final int
standardSlot
(Object key) Standard base slot computation for a key.private final int
stepSlot
(int slot) Step the slot number for an entry.
-
Field Details
-
DEFAULT_FILL
private static final double DEFAULT_FILLDefault fill fraction allowed before growing table.- See Also:
-
MINIMUM_SIZE
private static final int MINIMUM_SIZEMinimum size used for hash table.- See Also:
-
m_entryCount
private int m_entryCountNumber of entries present in table. -
m_entryLimit
private int m_entryLimitEntries allowed before growing table. -
m_arraySize
private int m_arraySizeSize of array used for keys. -
m_hitOffset
private int m_hitOffsetOffset added (modulo table size) to slot number on collision. -
m_keyTable
Array of key table slots. -
m_valueTable
private int[] m_valueTableArray of value table slots.
-
-
Constructor Details
-
ReferenceCountMap
public ReferenceCountMap(int count) Constructor with count.- Parameters:
count
- number of values to assume in initial sizing of table
-
ReferenceCountMap
public ReferenceCountMap()Default constructor. -
ReferenceCountMap
Copy (clone) constructor.- Parameters:
base
- instance being copied
-
-
Method Details
-
stepSlot
private final int stepSlot(int slot) Step the slot number for an entry. Adds the collision offset (modulo the table size) to the slot number.- Parameters:
slot
- slot number to be stepped- Returns:
- stepped slot number
-
freeSlot
private final int freeSlot(int slot) Find free slot number for entry. Starts at the slot based directly on the hashed key value. If this slot is already occupied, it adds the collision offset (modulo the table size) to the slot number and checks that slot, repeating until an unused slot is found.- Parameters:
slot
- initial slot computed from key- Returns:
- slot at which entry was added
-
standardSlot
Standard base slot computation for a key.- Parameters:
key
- key value to be computed- Returns:
- base slot for key
-
standardFind
Standard find key in table. This method may be used directly for key lookup using either thehashCode()
method defined for the key objects or theSystem.identityHashCode()
method, and either theequals()
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.- Parameters:
key
- to be found in table- Returns:
- index of matching key, or
-index-1
of slot to be used for inserting key in table if not already present (always negative)
-
reinsert
private boolean reinsert(int slot) Reinsert an entry into the hash map. This is used when the table is being directly modified, and does not adjust the count present or check the table capacity.- Parameters:
slot
- position of entry to be reinserted into hash map- Returns:
true
if the slot number used by the entry has has changed,false
if not
-
internalRemove
private void internalRemove(int slot) Internal remove pair from the table. Removes the pair from the table by setting the key entry tonull
and adjusting the count present, then chains through the table to reinsert any other pairs which may have collided with the removed pair. If the associated value is an object reference, it should be set tonull
before this method is called.- Parameters:
slot
- index number of pair to be removed
-
restructure
Restructure the table. This is used when the table is increasing or decreasing in size, and works directly with the old table representation arrays. It inserts pairs from the old arrays directly into the table without adjusting the count present or checking the table size.- Parameters:
keys
- array of keysvalues
- array of values
-
assignSlot
Assign slot for entry. Starts at the slot found by the hashed key value. If this slot is already occupied, it steps the slot number and checks the resulting slot, repeating until an unused slot is found. This method does not check for duplicate keys, so it should only be used for internal reordering of the tables.- Parameters:
key
- to be added to tablevalue
- associated value for key- Returns:
- slot at which entry was added
-
incrementCount
Increment a use count in the table. If the key object is already present in the table this adds one to the reference count; if not present, this adds the key with an initial reference count of one.- Parameters:
key
- referenced object (non-null
)- Returns:
- incremented use count
-
getCount
Find an entry in the table.- Parameters:
key
- key for entry to be returned- Returns:
- value for key, or zero if key not found
-
size
public int size()Get number of entries in map.- Returns:
- entry count
-
iterator
Get iterator for keys in map. The returned iterator is not safe, so the iterator behavior is undefined if the map is modified.- Returns:
- iterator
-
keyArray
Get array of keys in map.- Returns:
- key array
-
clone
Construct a copy of the table. -
clear
public void clear()Clear all keys and counts.
-