Package com.biglybt.core.util
Class LightHashMap<S,T>
- java.lang.Object
-
- java.util.AbstractMap<S,T>
-
- com.biglybt.core.util.LightHashMap<S,T>
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.util.Map<S,T>
- Direct Known Subclasses:
JSONObject
,LightHashMapEx
public class LightHashMap<S,T> extends java.util.AbstractMap<S,T> implements java.lang.Cloneable
A lighter (on memory) hash map
Advantages over HashMap:- Lower memory footprint
- Everything is stored in a single array, this might improve cache performance (not verified)
- Read-only operations on Key and Value iterators should be concurrency-safe (Entry iterators are not) but they might return null values unexpectedly under concurrent modification (not verified)
- removal is implemented with thombstone-keys, this can significantly increase the lookup time if many values are removed. Use compactify() for scrubbing
- entry set iterators and thus transfers to other maps are slower than compareable implementations
- the map does not store hashcodes and relies on either the key-objects themselves caching them (such as strings) or a fast computation of hashcodes
- concurrent modification detection is not as fail-fast as HashMap as no modification counter is used and only structural differences are noted
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
LightHashMap.EntrySet
private class
LightHashMap.HashIterator
private class
LightHashMap.KeySet
private class
LightHashMap.Values
-
Field Summary
Fields Modifier and Type Field Description (package private) java.lang.Object[]
data
private static int
DEFAULT_CAPACITY
private static float
DEFAULT_LOAD_FACTOR
(package private) float
loadFactor
private static java.lang.Object
NULLKEY
(package private) int
size
private static java.lang.Object
THOMBSTONE
-
Constructor Summary
Constructors Constructor Description LightHashMap()
LightHashMap(int initialCapacity)
LightHashMap(int initialCapacity, float loadFactor)
LightHashMap(java.util.Map m)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.Object
add(java.lang.Object key, java.lang.Object value, boolean bulkAdd)
private void
adjustCapacity(int newSize)
int
capacity()
private void
checkCapacity(int n)
void
clear()
java.lang.Object
clone()
void
compactify(float compactingLoadFactor)
will shrink the internal storage size to the least possible amount, should be used after removing many entries for exampleboolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
java.util.Set
entrySet()
private int
findIndex(java.lang.Object keyToFind)
T
get(java.lang.Object key)
private boolean
keysEqual(java.lang.Object o1, java.lang.Object o2)
java.util.Set<S>
keySet()
static void
main(java.lang.String[] args)
private int
nonModifyingFindIndex(java.lang.Object keyToFind)
T
put(java.lang.Object key, java.lang.Object value)
void
putAll(java.util.Map m)
T
remove(java.lang.Object key)
private java.lang.Object
removeForIndex(int idx)
(package private) static void
test()
java.util.Collection<T>
values()
-
-
-
Field Detail
-
THOMBSTONE
private static final java.lang.Object THOMBSTONE
-
NULLKEY
private static final java.lang.Object NULLKEY
-
DEFAULT_LOAD_FACTOR
private static final float DEFAULT_LOAD_FACTOR
- See Also:
- Constant Field Values
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
loadFactor
final float loadFactor
-
size
int size
-
data
java.lang.Object[] data
-
-
Method Detail
-
entrySet
public java.util.Set entrySet()
-
put
public T put(java.lang.Object key, java.lang.Object value)
-
putAll
public void putAll(java.util.Map m)
-
keySet
public java.util.Set<S> keySet()
-
values
public java.util.Collection<T> values()
-
capacity
public int capacity()
-
get
public T get(java.lang.Object key)
-
add
private java.lang.Object add(java.lang.Object key, java.lang.Object value, boolean bulkAdd)
-
remove
public T remove(java.lang.Object key)
-
removeForIndex
private java.lang.Object removeForIndex(int idx)
-
clear
public void clear()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
keysEqual
private boolean keysEqual(java.lang.Object o1, java.lang.Object o2)
-
findIndex
private int findIndex(java.lang.Object keyToFind)
-
nonModifyingFindIndex
private int nonModifyingFindIndex(java.lang.Object keyToFind)
-
checkCapacity
private void checkCapacity(int n)
-
compactify
public void compactify(float compactingLoadFactor)
will shrink the internal storage size to the least possible amount, should be used after removing many entries for example- Parameters:
compactingLoadFactor
- load factor for the compacting operation. Use 0f to compact with the load factor specified during instantiation. Use negative values of the desired load factors to compact only when it would reduce the storage size.
-
adjustCapacity
private void adjustCapacity(int newSize)
-
test
static void test()
-
main
public static void main(java.lang.String[] args)
-
-