Class TrieNodeMap<V>
- java.lang.Object
-
- org.predict4all.nlp.ngram.trie.map.TrieNodeMap<V>
-
public class TrieNodeMap<V> extends Object
Custom implementation copied fromTIntObjectHashMap
but with less attribute to reduce the heap size in Trie.
Source is copied from class hierarchy (with manually merging methods):THash
TPrimitiveHash
TIntHash
TIntObjectHashMap
-
-
Field Summary
Fields Modifier and Type Field Description protected int
_free
the current number of free slots in the hash.protected int
_maxSize
The maximum number of elements allowed without allocating more space.int[]
_set
the set of intsprotected int
_size
the current number of occupied slots in the hash.byte[]
_states
flags indicating whether each position in the hash is FREE, FULL, or REMOVEDprotected V[]
_values
the values of the mapprotected boolean
consumeFreeSlot
static byte
FREE
flag indicating that a slot in the hashtable is availablestatic byte
FULL
flag indicating that a slot in the hashtable is occupiedstatic int
oneInstanceCount
static byte
REMOVED
flag indicating that the value of a slot in the hashtable was deleted
-
Constructor Summary
Constructors Constructor Description TrieNodeMap()
Creates a newTHash
instance with a prime capacity at or near the minimum needed to holdinitialCapacity
elements with load factorloadFactor
without triggering a rehash.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
calculateGrownCapacity()
int
capacity()
Returns the capacity of the hash table.void
clear()
Empties the collection.void
compact()
Compresses the hashtable to the minimum prime size (as defined by PrimeFinder) that will hold all of the elements currently in the table.protected void
computeMaxSize(int capacity)
Computes the values of maxSize.boolean
contains(int val)
Searches the set forval
boolean
containsKey(int key)
boolean
containsValue(Object val)
boolean
forEach(TIntProcedure procedure)
Executesprocedure
for each element in the set.boolean
forEachEntry(TIntObjectProcedure<? super V> procedure)
boolean
forEachKey(TIntProcedure procedure)
boolean
forEachValue(TObjectProcedure<? super V> procedure)
boolean
forEachValue(Consumer<? super V> procedure)
V
get(int key)
protected int
index(int val)
Locates the index ofval
.protected int
insertKey(int val)
Locates the index at whichval
can be inserted. if there is already a value equal()ingval
in the set, returns that value as a negative integer.boolean
isEmpty()
Tells whether this set is currently holding any elements.int[]
keys()
int[]
keys(int[] dest)
protected void
postInsertHook(boolean usedFreeSlot)
After an insert, this hook is called to adjust the size/free values of the set and to perform rehashing if necessary.V
put(int key, V value)
void
putAll(Map<? extends Integer,? extends V> map)
V
putIfAbsent(int key, V value)
protected void
rehash(int newCapacity)
V
remove(int key)
protected void
removeAt(int index)
Delete the record atindex
.boolean
retainEntries(TIntObjectProcedure<? super V> procedure)
protected int
setUp(int initialCapacity)
initializes the hashtable to a prime capacity which is at leastinitialCapacity + 1
.int
size()
Returns the number of distinct elements in this collection.void
testAll()
String
toString()
void
transformValues(TObjectFunction<V,V> function)
void
trimToSize()
This simply callscompact
.Object[]
values()
V[]
values(V[] dest)
-
-
-
Field Detail
-
FREE
public static final byte FREE
flag indicating that a slot in the hashtable is available- See Also:
- Constant Field Values
-
FULL
public static final byte FULL
flag indicating that a slot in the hashtable is occupied- See Also:
- Constant Field Values
-
REMOVED
public static final byte REMOVED
flag indicating that the value of a slot in the hashtable was deleted- See Also:
- Constant Field Values
-
_size
protected transient int _size
the current number of occupied slots in the hash.
-
_free
protected transient int _free
the current number of free slots in the hash.
-
_set
public transient int[] _set
the set of ints
-
consumeFreeSlot
protected boolean consumeFreeSlot
-
_maxSize
protected int _maxSize
The maximum number of elements allowed without allocating more space.
-
_states
public transient byte[] _states
flags indicating whether each position in the hash is FREE, FULL, or REMOVED
-
_values
protected transient V[] _values
the values of the map
-
oneInstanceCount
public static int oneInstanceCount
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Tells whether this set is currently holding any elements.- Returns:
- a
boolean
value
-
size
public int size()
Returns the number of distinct elements in this collection.- Returns:
- an
int
value
-
compact
public void compact()
Compresses the hashtable to the minimum prime size (as defined by PrimeFinder) that will hold all of the elements currently in the table. If you have done a lot ofremove
operations and plan to do a lot of queries or insertions or iteration, it is a good idea to invoke this method. Doing so will accomplish two things:
- You'll free memory allocated to the table but no longer needed because of the remove()s.
- You'll get better query/insert/iterator performance
because there won't be any
REMOVED
slots to skip over when probing for indices in the table.
-
trimToSize
public final void trimToSize()
This simply callscompact
. It is included for symmetry with other collection classes. Note that the name of this method is somewhat misleading (which is why we prefercompact
) as the load factor may require capacity above and beyond the size of this collection.- See Also:
compact()
-
removeAt
protected void removeAt(int index)
Delete the record atindex
. Reduces the size of the collection by one.- Parameters:
index
- anint
value
-
clear
public void clear()
Empties the collection.
-
setUp
protected int setUp(int initialCapacity)
initializes the hashtable to a prime capacity which is at leastinitialCapacity + 1
.- Parameters:
initialCapacity
- anint
value- Returns:
- the actual capacity chosen
-
computeMaxSize
protected void computeMaxSize(int capacity)
Computes the values of maxSize. There will always be at least one free slot required.- Parameters:
capacity
- anint
value
-
postInsertHook
protected final void postInsertHook(boolean usedFreeSlot)
After an insert, this hook is called to adjust the size/free values of the set and to perform rehashing if necessary.- Parameters:
usedFreeSlot
- the slot
-
calculateGrownCapacity
protected int calculateGrownCapacity()
-
capacity
public int capacity()
Returns the capacity of the hash table. This is the true physical capacity, without adjusting for the load factor.- Returns:
- the physical capacity of the hash table.
-
contains
public boolean contains(int val)
Searches the set forval
- Parameters:
val
- anint
value- Returns:
- a
boolean
value
-
forEach
public boolean forEach(TIntProcedure procedure)
Executesprocedure
for each element in the set.- Parameters:
procedure
- aTObjectProcedure
value- Returns:
- false if the loop over the set terminated because the procedure returned false for some value.
-
index
protected int index(int val)
Locates the index ofval
.- Parameters:
val
- anint
value- Returns:
- the index of
val
or -1 if it isn't in the set.
-
insertKey
protected int insertKey(int val)
Locates the index at whichval
can be inserted. if there is already a value equal()ingval
in the set, returns that value as a negative integer.- Parameters:
val
- anint
value- Returns:
- an
int
value
-
rehash
protected void rehash(int newCapacity)
-
containsKey
public boolean containsKey(int key)
-
containsValue
public boolean containsValue(Object val)
-
get
public V get(int key)
-
remove
public V remove(int key)
-
keys
public int[] keys()
-
keys
public int[] keys(int[] dest)
-
values
public Object[] values()
-
forEachKey
public boolean forEachKey(TIntProcedure procedure)
-
forEachValue
public boolean forEachValue(TObjectProcedure<? super V> procedure)
-
forEachEntry
public boolean forEachEntry(TIntObjectProcedure<? super V> procedure)
-
retainEntries
public boolean retainEntries(TIntObjectProcedure<? super V> procedure)
-
transformValues
public void transformValues(TObjectFunction<V,V> function)
-
testAll
public void testAll()
-
-