Top Description Inners Fields Constructors Methods
org.graalvm.collections

pack-priv final Class EconomicMapImpl<K, V>

extends Object
implements EconomicMap<K, V>, EconomicSet<K>
Class Inheritance
All Implemented Interfaces
org.graalvm.collections.EconomicSet, org.graalvm.collections.UnmodifiableEconomicSet, java.lang.Iterable, org.graalvm.collections.EconomicMap, org.graalvm.collections.UnmodifiableEconomicMap
Imports
java.util.Iterator, java.util.function.BiFunction

Implementation of a map with a memory-efficient structure that always preserves insertion order when iterating over keys. Particularly efficient when number of entries is 0 or smaller equal INITIAL_CAPACITY or smaller 256. The key/value pairs are kept in an expanding flat object array with keys at even indices and values at odd indices. If the map has smaller or equal to HASH_THRESHOLD entries, there is no additional hash data structure and comparisons are done via linear checking of the key/value pairs. For the case where the equality check is particularly cheap (e.g., just an object identity comparison), this limit below which the map is without an actual hash table is higher and configured at HASH_THRESHOLD_IDENTITY_COMPARE. When the hash table needs to be constructed, the field hashArray becomes a new hash array where an entry of 0 means no hit and otherwise denotes the entry number in the entries array. The hash array is interpreted as an actual byte array if the indices fit within 8 bit, or as an array of short values if the indices fit within 16 bit, or as an array of integer values in other cases. Hash collisions are handled by chaining a linked list of CollisionLink objects that take the place of the values in the entries array. Removing entries will put null into the entries array. If the occupation of the map falls below a specific threshold, the map will be compressed via the maybeCompress(int) method.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
EconomicMapImpl.CollisionLink

Links the collisions.

private abstract class

Field Summary

Modifier and TypeField and Description
private static final int
COMPRESS_IMMEDIATE_CAPACITY

Maximum number of entries that are moved linearly forward if a key is removed.

private int
deletedEntries

Number of deleted entries.

private Object[]
entries

Entries array with even indices storing keys and odd indices storing values.

private static final int
HASH_THRESHOLD

Number of entries above which a hash table is created.

private static final int
HASH_THRESHOLD_IDENTITY_COMPARE

Number of entries above which a hash table is created when equality can be checked with object identity.

private byte[]
hashArray

Hash array that is interpreted either as byte or short or int array depending on number of map entries.

private static final int
INITIAL_CAPACITY

Initial number of key/value pair entries that is allocated in the first entries array.

private final boolean
private static final int
LARGE_HASH_THRESHOLD

Number of entries above which more than 1 byte is necessary for the hash index.

private static final int
MAX_ELEMENT_COUNT

Maximum number of entries allowed in the map.

private static final int
MIN_CAPACITY_INCREASE

Minimum number of key/value pair entries added when the entries array is increased in size.

private final Equivalence
strategy

The strategy used for comparing keys or null for denoting special strategy Equivalence#IDENTITY.

private int
totalEntries

Total number of entries (actual entries plus deleted entries).

private static final int
VERY_LARGE_HASH_THRESHOLD

Number of entries above which more than 2 bytes are are necessary for the hash index.

Constructor Summary

AccessConstructor and Description
private
EconomicMapImpl(Equivalence strategy, boolean isSet)

private
EconomicMapImpl(Equivalence strategy, int initialCapacity, boolean isSet)

private
EconomicMapImpl(Equivalence strategy, UnmodifiableEconomicMap<K, V> other, boolean isSet)

private
EconomicMapImpl(Equivalence strategy, UnmodifiableEconomicSet<K> other, boolean isSet)

Method Summary

Modifier and TypeMethod and Description
public boolean
add(K element)

Implements org.graalvm.collections.EconomicSet.add.

Adds element to this set if it is not already present.

private void
public void
clear()

Implements org.graalvm.collections.EconomicMap.clear, org.graalvm.collections.EconomicSet.clear.

Removes all of the mappings from this map.

private boolean
compareKeys(Object key, Object entryKey)

private int
compressLarge(int nextIndex)

Compresses the graph and returns the translated new next index.

public boolean
contains(K element)

Implements org.graalvm.collections.UnmodifiableEconomicSet.contains.

Returns true if this set contains a mapping for the element.

public boolean
containsKey(K key)

Implements org.graalvm.collections.UnmodifiableEconomicMap.containsKey.

Returns true if this map contains a mapping for key.

public static <K, V> EconomicMapImpl<K, V>
create(Equivalence strategy, boolean isSet)

public static <K, V> EconomicMapImpl<K, V>
create(Equivalence strategy, int initialCapacity, boolean isSet)

public static <K, V> EconomicMapImpl<K, V>
create(Equivalence strategy, UnmodifiableEconomicMap<K, V> other, boolean isSet)

public static <K, V> EconomicMapImpl<K, V>
create(Equivalence strategy, UnmodifiableEconomicSet<K> other, boolean isSet)

private void
private int
find(K key)

private int
private int
findHash(K key)

private int
findLinear(K key)

public V
get(K key)

Implements org.graalvm.collections.UnmodifiableEconomicMap.get.

Returns the value to which key is mapped, or null if this map contains no mapping for key.

public MapCursor<K, V>
getEntries()

Implements org.graalvm.collections.EconomicMap.getEntries.

Returns a MapCursor view of the mappings contained in this map.

public Equivalence
getEquivalenceStrategy()

Overrides default org.graalvm.collections.UnmodifiableEconomicMap.getEquivalenceStrategy.

Returns the strategy used to compare keys.

private int
getHashArray(int index)

private int
private int
private int
getHashThreshold()

Number of entries above which a hash table should be constructed.

private Object
getKey(int index)

public Iterable<K>
getKeys()

Implements org.graalvm.collections.UnmodifiableEconomicMap.getKeys.

Returns a Iterable view of the keys contained in this map.

private Object
getRawValue(int index)

private Object
getValue(int index)

public Iterable<V>
getValues()

Implements org.graalvm.collections.UnmodifiableEconomicMap.getValues.

Returns a Iterable view of the values contained in this map.

private void
grow()

private boolean
private void
init(int size)

private boolean
private static <K, V> EconomicMapImpl<K, V>
intercept(EconomicMapImpl<K, V> map)

Intercept method for debugging purposes.

public boolean
public Iterator<K>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

private int
maybeCompress(int nextIndex)

Compresses the graph if there is a large number of deleted entries and returns the translated new next index.

public V
put(K key, V value)

Implements org.graalvm.collections.EconomicMap.put.

Associates value with key in this map.

private void
putHashEntry(Object key, int entryIndex, boolean rehashOnCollision)

private int
remove(int indexToRemove)

Implements org.graalvm.collections.EconomicSet.remove.

Removes the element at the specific index and returns the index of the next element.

public void
remove(K element)

Implements org.graalvm.collections.EconomicSet.remove.

Removes element from this set if it is present.

public V
removeKey(K key)

Implements org.graalvm.collections.EconomicMap.removeKey.

Removes the mapping for key from this map if it is present.

public void
replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

Implements org.graalvm.collections.EconomicMap.replaceAll.

Replaces each entry's value with the result of invoking function on that entry until all entries have been processed or the function throws an exception.

private void
setHashArray(int index, int value)

private void
setKey(int index, Object newValue)

private void
setRawValue(int index, Object newValue)

private void
setValue(int index, Object newValue)

public int
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

pack-priv static <K, V> String
toString(boolean isSet, int size, MapCursor<K, V> cursor)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

COMPRESS_IMMEDIATE_CAPACITYback to summary
private static final int COMPRESS_IMMEDIATE_CAPACITY

Maximum number of entries that are moved linearly forward if a key is removed.

deletedEntriesback to summary
private int deletedEntries

Number of deleted entries.

entriesback to summary
private Object[] entries

Entries array with even indices storing keys and odd indices storing values.

HASH_THRESHOLDback to summary
private static final int HASH_THRESHOLD

Number of entries above which a hash table is created.

HASH_THRESHOLD_IDENTITY_COMPAREback to summary
private static final int HASH_THRESHOLD_IDENTITY_COMPARE

Number of entries above which a hash table is created when equality can be checked with object identity.

hashArrayback to summary
private byte[] hashArray

Hash array that is interpreted either as byte or short or int array depending on number of map entries.

INITIAL_CAPACITYback to summary
private static final int INITIAL_CAPACITY

Initial number of key/value pair entries that is allocated in the first entries array.

isSetback to summary
private final boolean isSet
LARGE_HASH_THRESHOLDback to summary
private static final int LARGE_HASH_THRESHOLD

Number of entries above which more than 1 byte is necessary for the hash index.

MAX_ELEMENT_COUNTback to summary
private static final int MAX_ELEMENT_COUNT

Maximum number of entries allowed in the map.

MIN_CAPACITY_INCREASEback to summary
private static final int MIN_CAPACITY_INCREASE

Minimum number of key/value pair entries added when the entries array is increased in size.

strategyback to summary
private final Equivalence strategy

The strategy used for comparing keys or null for denoting special strategy Equivalence#IDENTITY.

totalEntriesback to summary
private int totalEntries

Total number of entries (actual entries plus deleted entries).

VERY_LARGE_HASH_THRESHOLDback to summary
private static final int VERY_LARGE_HASH_THRESHOLD

Number of entries above which more than 2 bytes are are necessary for the hash index.

Constructor Detail

EconomicMapImplback to summary
private EconomicMapImpl(Equivalence strategy, boolean isSet)
EconomicMapImplback to summary
private EconomicMapImpl(Equivalence strategy, int initialCapacity, boolean isSet)
EconomicMapImplback to summary
private EconomicMapImpl(Equivalence strategy, UnmodifiableEconomicMap<K, V> other, boolean isSet)
EconomicMapImplback to summary
private EconomicMapImpl(Equivalence strategy, UnmodifiableEconomicSet<K> other, boolean isSet)

Method Detail

addback to summary
public boolean add(K element)

Implements org.graalvm.collections.EconomicSet.add.

Doc from org.graalvm.collections.EconomicSet.add.

Adds element to this set if it is not already present.

Returns:boolean

true if this set did not already contain element.

Annotations
@SuppressWarnings:unchecked
@Override
checkKeyNonNullback to summary
private void checkKeyNonNull(K key)
clearback to summary
public void clear()

Implements org.graalvm.collections.EconomicMap.clear, org.graalvm.collections.EconomicSet.clear.

Doc from org.graalvm.collections.EconomicMap.clear.

Removes all of the mappings from this map. The map will be empty after this call returns.

Annotations
@Override
compareKeysback to summary
private boolean compareKeys(Object key, Object entryKey)
compressLargeback to summary
private int compressLarge(int nextIndex)

Compresses the graph and returns the translated new next index.

containsback to summary
public boolean contains(K element)

Implements org.graalvm.collections.UnmodifiableEconomicSet.contains.

Doc from org.graalvm.collections.UnmodifiableEconomicSet.contains.

Returns true if this set contains a mapping for the element.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(K key)

Implements org.graalvm.collections.UnmodifiableEconomicMap.containsKey.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.containsKey.

Returns true if this map contains a mapping for key. Always returns false if the key is null.

Annotations
@Override
createback to summary
public static <K, V> EconomicMapImpl<K, V> create(Equivalence strategy, boolean isSet)
createback to summary
public static <K, V> EconomicMapImpl<K, V> create(Equivalence strategy, int initialCapacity, boolean isSet)
createback to summary
public static <K, V> EconomicMapImpl<K, V> create(Equivalence strategy, UnmodifiableEconomicMap<K, V> other, boolean isSet)
createback to summary
public static <K, V> EconomicMapImpl<K, V> create(Equivalence strategy, UnmodifiableEconomicSet<K> other, boolean isSet)
createHashback to summary
private void createHash()
findback to summary
private int find(K key)
findAndRemoveHashback to summary
private int findAndRemoveHash(Object key)
findHashback to summary
private int findHash(K key)
findLinearback to summary
private int findLinear(K key)
getback to summary
public V get(K key)

Implements org.graalvm.collections.UnmodifiableEconomicMap.get.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.get.

Returns the value to which key is mapped, or null if this map contains no mapping for key. The key must not be null.

Annotations
@SuppressWarnings:unchecked
@Override
getEntriesback to summary
public MapCursor<K, V> getEntries()

Implements org.graalvm.collections.EconomicMap.getEntries.

Doc from org.graalvm.collections.EconomicMap.getEntries.

Returns a MapCursor view of the mappings contained in this map.

Annotations
@Override
getEquivalenceStrategyback to summary
public Equivalence getEquivalenceStrategy()

Overrides default org.graalvm.collections.UnmodifiableEconomicMap.getEquivalenceStrategy.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.getEquivalenceStrategy.

Returns the strategy used to compare keys.

Annotations
@Override
getHashArrayback to summary
private int getHashArray(int index)
getHashIndexback to summary
private int getHashIndex(Object key)
getHashTableSizeback to summary
private int getHashTableSize()
getHashThresholdback to summary
private int getHashThreshold()

Number of entries above which a hash table should be constructed.

getKeyback to summary
private Object getKey(int index)
getKeysback to summary
public Iterable<K> getKeys()

Implements org.graalvm.collections.UnmodifiableEconomicMap.getKeys.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.getKeys.

Returns a Iterable view of the keys contained in this map.

Annotations
@Override
getRawValueback to summary
private Object getRawValue(int index)
getValueback to summary
private Object getValue(int index)
getValuesback to summary
public Iterable<V> getValues()

Implements org.graalvm.collections.UnmodifiableEconomicMap.getValues.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.getValues.

Returns a Iterable view of the values contained in this map.

Annotations
@Override
growback to summary
private void grow()
hasHashArrayback to summary
private boolean hasHashArray()
initback to summary
private void init(int size)
initFromback to summary
private boolean initFrom(Object o)
Annotations
@SuppressWarnings:unchecked
interceptback to summary
private static <K, V> EconomicMapImpl<K, V> intercept(EconomicMapImpl<K, V> map)

Intercept method for debugging purposes.

isEmptyback to summary
public boolean isEmpty()

Implements org.graalvm.collections.UnmodifiableEconomicMap.isEmpty, org.graalvm.collections.UnmodifiableEconomicSet.isEmpty.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.isEmpty.

Returns true if this map contains no key-value mappings.

Annotations
@Override
iteratorback to summary
public Iterator<K> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<K>

an Iterator.

Annotations
@Override
maybeCompressback to summary
private int maybeCompress(int nextIndex)

Compresses the graph if there is a large number of deleted entries and returns the translated new next index.

putback to summary
public V put(K key, V value)

Implements org.graalvm.collections.EconomicMap.put.

Doc from org.graalvm.collections.EconomicMap.put.

Associates value with key in this map. If the map previously contained a mapping for key, the old value is replaced by value. While the value may be null, the key must not be {code null}.

Returns:V

the previous value associated with key, or null if there was no mapping for key.

Annotations
@SuppressWarnings:unchecked
@Override
putHashEntryback to summary
private void putHashEntry(Object key, int entryIndex, boolean rehashOnCollision)
removeback to summary
private int remove(int indexToRemove)

Implements org.graalvm.collections.EconomicSet.remove.

Removes the element at the specific index and returns the index of the next element. This can be a different value if graph compression was triggered.

removeback to summary
public void remove(K element)

Implements org.graalvm.collections.EconomicSet.remove.

Doc from org.graalvm.collections.EconomicSet.remove.

Removes element from this set if it is present. This set will not contain element once the call returns.

Annotations
@Override
removeKeyback to summary
public V removeKey(K key)

Implements org.graalvm.collections.EconomicMap.removeKey.

Doc from org.graalvm.collections.EconomicMap.removeKey.

Removes the mapping for key from this map if it is present. The map will not contain a mapping for key once the call returns. The key must not be null.

Returns:V

the previous value associated with key, or null if there was no mapping for key.

Annotations
@SuppressWarnings:unchecked
@Override
replaceAllback to summary
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

Implements org.graalvm.collections.EconomicMap.replaceAll.

Doc from org.graalvm.collections.EconomicMap.replaceAll.

Replaces each entry's value with the result of invoking function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.

Annotations
@SuppressWarnings:unchecked
@Override
setHashArrayback to summary
private void setHashArray(int index, int value)
setKeyback to summary
private void setKey(int index, Object newValue)
setRawValueback to summary
private void setRawValue(int index, Object newValue)
setValueback to summary
private void setValue(int index, Object newValue)
sizeback to summary
public int size()

Implements org.graalvm.collections.UnmodifiableEconomicMap.size, org.graalvm.collections.UnmodifiableEconomicSet.size.

Doc from org.graalvm.collections.UnmodifiableEconomicMap.size.

Returns the number of key-value mappings in this map.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
toStringback to summary
pack-priv static <K, V> String toString(boolean isSet, int size, MapCursor<K, V> cursor)
org.graalvm.collections back to summary

private abstract Class EconomicMapImpl.SparseMapIterator<E>

extends Object
implements Iterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Field Summary

Modifier and TypeField and Description
protected int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentback to summary
protected int current

Constructor Detail

SparseMapIteratorback to summary
private SparseMapIterator()

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override