Package com.polytechnik.utils
Class LongHashMap<V>
java.lang.Object
com.polytechnik.utils.LongHashMap<V>
- Direct Known Subclasses:
LongTreeValueMapTest
This is a copy of java.util.HashMap which uses longs as keys
instead of Objects. It turns out that using this in the PageCache
implementation speeds up heap traversals by a factor of three.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final boolean
(package private) final float
The load factor for the hash table.(package private) int
The number of times this HashMap has been structurally modified Structural modifications are those that change the number of mappings in the HashMap or otherwise modify its internal structure (e.g., rehash).(package private) int
The total number of mappings in the hash table.private static final char[]
(package private) LongHashMap.Entry<V>[]
The hash table data.(package private) int
The table is rehashed when its size exceeds this threshold. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, empty map with a default capacity and load factor, which is0.75
.LongHashMap
(int initialCapacity) Constructs a new, empty map with the specified initial capacity and default load factor, which is0.75
.LongHashMap
(int initialCapacity, float loadFactor) Constructs a new, empty map with the specified initial capacity and the specified load factor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all mappings from this map.private static int
final boolean
containsKey
(long key) Returnstrue
if this map contains a mapping for the specified key.int
copyKeys
(long[] keylist) int
copyKeysAndValues
(long[] keylist, V[] valuesarr) int
copyValues
(V[] valuesarr) boolean
forEachEntry
(LongHashMap.TLongObjectProcedure<V> procedure) Executes procedure for each key/value entry in the map.boolean
forEachEntry
(Predicate<V> procedure) final V
get
(long key) Returns the value to which this map maps the specified key.(package private) final LongHashMap.Entry
<V> getEntry
(long key) Returns the entry associated with the specified key in the HashMap.final boolean
isEmpty()
Returnstrue
if this map contains no key-value mappings.static void
Associates the specified value with the specified key in this map.private static byte[]
randombytes
(int n) private static Long
randomLongk
(int i) private static String
randomstring
(int n) private static String
randomStringk
(int i) (package private) void
rehash()
Rehashes the contents of this map into a newHashMap
instance with a larger capacity.remove
(long key) Removes the mapping for this key from this map if present.(package private) final LongHashMap.Entry
<V> removeEntryForKey
(long key) Removes and returns the entry associated with the specified key in the HashMap.final int
size()
Returns the number of key-value mappings in this map.private static int
test_the_same
(String comment, LongHashMap<String> m, HashMap<Long, String> hm)
-
Field Details
-
table
The hash table data. -
size
transient int sizeThe total number of mappings in the hash table. -
threshold
int thresholdThe table is rehashed when its size exceeds this threshold. (The value of this field is (int)(capacity * loadFactor).) -
loadFactor
final float loadFactorThe load factor for the hash table. -
modCount
transient int modCountThe number of times this HashMap has been structurally modified Structural modifications are those that change the number of mappings in the HashMap or otherwise modify its internal structure (e.g., rehash). This field is used to make iterators on Collection-views of the HashMap fail-fast. (See ConcurrentModificationException). -
SYMBOLS64
private static final char[] SYMBOLS64 -
DEBUG_TEST
private static final boolean DEBUG_TEST- See Also:
-
-
Constructor Details
-
LongHashMap
public LongHashMap(int initialCapacity, float loadFactor) Constructs a new, empty map with the specified initial capacity and the specified load factor.- Parameters:
initialCapacity
- the initial capacity of the HashMap.loadFactor
- the load factor of the HashMap- Throws:
IllegalArgumentException
- if the initial capacity is less than zero, or if the load factor is nonpositive.
-
LongHashMap
public LongHashMap(int initialCapacity) Constructs a new, empty map with the specified initial capacity and default load factor, which is0.75
.- Parameters:
initialCapacity
- the initial capacity of the HashMap.- Throws:
IllegalArgumentException
- if the initial capacity is less than zero.
-
LongHashMap
public LongHashMap()Constructs a new, empty map with a default capacity and load factor, which is0.75
.
-
-
Method Details
-
size
public final int size()Returns the number of key-value mappings in this map.- Returns:
- the number of key-value mappings in this map.
-
isEmpty
public final boolean isEmpty()Returnstrue
if this map contains no key-value mappings.- Returns:
true
if this map contains no key-value mappings.
-
get
Returns the value to which this map maps the specified key. Returnsnull
if the map contains no mapping for this key. A return value ofnull
does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull
. ThecontainsKey
operation may be used to distinguish these two cases.- Parameters:
key
- key whose associated value is to be returned.- Returns:
- the value to which this map maps the specified key.
-
containsKey
public final boolean containsKey(long key) Returnstrue
if this map contains a mapping for the specified key.- Parameters:
key
- key whose presence in this Map is to be tested.- Returns:
true
if this map contains a mapping for the specified key.
-
getEntry
Returns the entry associated with the specified key in the HashMap. Returns null if the HashMap contains no mapping for this key. -
put
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.- Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.- Returns:
- previous value associated with specified key, or
null
if there was no mapping for key. Anull
return can also indicate that the HashMap previously associatednull
with the specified key.
-
remove
Removes the mapping for this key from this map if present.- Parameters:
key
- key whose mapping is to be removed from the map.- Returns:
- previous value associated with specified key, or
null
if there was no mapping for key. Anull
return can also indicate that the map previously associatednull
with the specified key.
-
removeEntryForKey
Removes and returns the entry associated with the specified key in the HashMap. Returns null if the HashMap contains no mapping for this key. -
clear
public void clear()Removes all mappings from this map. -
rehash
void rehash()Rehashes the contents of this map into a newHashMap
instance with a larger capacity. This method is called automatically when the number of keys in this map exceeds its capacity and load factor. -
copyValues
-
copyKeys
public int copyKeys(long[] keylist) -
copyKeysAndValues
-
forEachEntry
Executes procedure for each key/value entry in the map.- Parameters:
procedure
- - a LongObjectProcedure value.- Returns:
- false if the loop over the entries terminated because the procedure returned false for some entry.
-
forEachEntry
-
randombytes
private static byte[] randombytes(int n) -
randomstring
-
randomLongk
-
randomStringk
-
comparisontest_hashmap
private static int comparisontest_hashmap() -
test_the_same
-
main
-