- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
Serializable,Iterable<Map.Entry<K,,V>> ImmutableMap<K,,V> ReadOnlyMap<K,V>
ImmutableMap interface using a Compressed Hash-Array
Mapped Prefix-tree (CHAMP).
Features:
- supports up to 231 - 1 entries
- allows null keys and null values
- is immutable
- is thread-safe
- does not guarantee a specific iteration order
Performance characteristics:
- put: O(log₃₂ N)
- remove: O(log₃₂ N)
- containsKey: O(log₃₂ N)
- toMutable: O(1) + O(log₃₂ N) distributed across subsequent updates in the mutable copy
- clone: O(1)
- iterator.next(): O(1)
Implementation details:
This map performs read and write operations of single elements in O(log₃₂ N) time, and in O(log₃₂ N) space.
The CHAMP trie contains nodes that may be shared with other maps.
If a write operation is performed on a node, then this map creates a copy of the node and of all parent nodes up to the root (copy-path-on-write).
This map can create a mutable copy of itself in O(1) time and O(1) space
using method toMutable(). The mutable copy shares its nodes
with this map, until it has gradually replaced the nodes with exclusively
owned nodes.
All operations on this map can be performed concurrently, without a need for synchronisation.
The immutable version of this map extends from the non-public class
ChampBitmapIndexNode. This design safes 16 bytes for every instance,
and reduces the number of redirections for finding an element in the
collection by 1.
References:
Portions of the code in this class has been derived from 'The Capsule Hash Trie Collections Library'.
- Michael J. Steindorfer (2017). Efficient Immutable Collections.
- michael.steindorfer.name
- The Capsule Hash Trie Collections Library.
Copyright (c) Michael Steindorfer. BSD-2-Clause License - github.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedChampMap(@NonNull PrivateData privateData) Creates a new instance with the provided privateData data object. -
Method Summary
Modifier and TypeMethodDescriptionasMap()Wraps this map in theMapinterface - without copying.intReturns the spliterator characteristics of the key set.clear()Returns a copy of this map that is empty.booleanReturnstrueif this map contains a entry for the specified key.Returns an immutable copy of the provided map.Returns an immutable copy of the provided map.booleanCompares the specified object with this map for equality.Returns the value to which the key is mapped, ornullif this map contains no entry for the key.inthashCode()Returns the hash code value for this map.booleanisEmpty()Returnstrueif this map contains no entries.iterator()Returns an iterator over the entries contained in this map.intmaxSize()Returns the maximal number of entries that this map type can holdnewInstance(@NonNull PrivateData privateData) Creates a new instance with the provided privateData object as its internal data structure.of()Returns an empty immutable map.Returns a copy of this map that contains all entries of this map with the specified entry added or updated.Returns a copy of this map that contains all entries of this map with entries from the specified map added or updated.Returns a copy of this map that contains all entries of this map with entries from the specified map added or updated.Returns aReadOnlySetview to the keys contained in this map.Returns a copy of this map that contains all entries of this map with the specified entry removed.Returns a copy of this map that contains all entries of this map except the entries of the specified collection.Returns a copy of this map that contains only entries that are in this map and in the specified collection.intsize()Returns the number of entries contained in this map..Returns a spliterator over the entries contained in this map.Creates a mutable copy of this map.toString()Returns a string representation of this map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableMap
putKeyValues, retainAllMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyMap
containsEntry, containsValue, getOrDefault, readOnlyEntrySet, readOnlyValues
-
Constructor Details
-
ChampMap
Creates a new instance with the provided privateData data object.This constructor is intended to be called from a constructor of the subclass, that is called from method
newInstance(PrivateData).- Parameters:
privateData- an privateData data object
-
-
Method Details
-
newInstance
Creates a new instance with the provided privateData object as its internal data structure.Subclasses must override this method, and return a new instance of their subclass!
- Parameters:
privateData- the internal data structure needed by this class for creating the instance.- Returns:
- a new instance of the subclass
-
characteristics
public int characteristics()Description copied from interface:ReadOnlyMapReturns the spliterator characteristics of the key set. This implementation returnsSpliterator.SIZED|Spliterator.DISTINCT.- Specified by:
characteristicsin interfaceReadOnlyMap<K,V> - Returns:
- characteristics.
-
copyOf
public static <K,V> @NonNull ChampMap<K,V> copyOf(@NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> c) Returns an immutable copy of the provided map.- Type Parameters:
K- the key typeV- the value type- Parameters:
c- a map- Returns:
- an immutable copy
-
copyOf
Returns an immutable copy of the provided map.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- a map- Returns:
- an immutable copy
-
of
Returns an empty immutable map.- Type Parameters:
K- the key typeV- the value type- Returns:
- an empty immutable map
-
clear
Returns a copy of this map that is empty.- Specified by:
clearin interfaceImmutableMap<K,V> - Returns:
- this set instance if it is already empty, or a different set instance that is empty.
-
containsKey
Returnstrueif this map contains a entry for the specified key.- Specified by:
containsKeyin interfaceReadOnlyMap<K,V> - Parameters:
o- a key- Returns:
trueif this map contains a entry for the specified key
-
equals
Description copied from interface:ReadOnlyMapCompares the specified object with this map for equality.Returns
trueif the given object is also a read-only map and the two maps represent the same entries, ignorig the sequence of the map entries. -
get
Description copied from interface:ReadOnlyMapReturns the value to which the key is mapped, ornullif this map contains no entry for the key.- Specified by:
getin interfaceReadOnlyMap<K,V> - Parameters:
o- a key- Returns:
- the mapped value or
null
-
hashCode
public int hashCode()Description copied from interface:ReadOnlyMapReturns the hash code value for this map. The hash code is the sum of the hash code of its entries. -
isEmpty
public boolean isEmpty()Description copied from interface:ReadOnlyMapReturnstrueif this map contains no entries.- Specified by:
isEmptyin interfaceReadOnlyMap<K,V> - Returns:
trueif empty
-
iterator
Description copied from interface:ReadOnlyMapReturns an iterator over the entries contained in this map. -
maxSize
public int maxSize()Description copied from interface:ImmutableMapReturns the maximal number of entries that this map type can hold- Specified by:
maxSizein interfaceImmutableMap<K,V> - Returns:
- the maximal size
-
put
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with the specified entry added or updated.- Specified by:
putin interfaceImmutableMap<K,V> - Parameters:
key- the key of the entryvalue- the value of the entry- Returns:
- this map instance if it already contains the same entry, or a different map instance with the entry added or updated
-
putAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with entries from the specified map added or updated.- Specified by:
putAllin interfaceImmutableMap<K,V> - Parameters:
m- another map- Returns:
- this map instance if it already contains the same entries, or a different map instance with the entries added or updated
-
putAll
public @NonNull ChampMap<K,V> putAll(@NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> c) Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with entries from the specified map added or updated.- Specified by:
putAllin interfaceImmutableMap<K,V> - Parameters:
c- another map- Returns:
- this map instance if it already contains the same entries, or a different map instance with the entries added or updated
-
remove
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with the specified entry removed.- Specified by:
removein interfaceImmutableMap<K,V> - Parameters:
key- the key of the entry- Returns:
- this map instance if it already does not contain the entry, or a different map instance with the entry removed
-
removeAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map except the entries of the specified collection.- Specified by:
removeAllin interfaceImmutableMap<K,V> - Parameters:
c- a collection with keys of entries to be removed from this map- Returns:
- this map instance if it already does not contain the entries, or a different map instance with the entries removed
-
retainAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains only entries that are in this map and in the specified collection.- Specified by:
retainAllin interfaceImmutableMap<K,V> - Parameters:
c- a collection with keys of entries to be retained in this map- Returns:
- this map instance if it has not changed, or a different map instance with entries removed
-
readOnlyKeySet
Description copied from interface:ReadOnlyMapReturns aReadOnlySetview to the keys contained in this map.- Specified by:
readOnlyKeySetin interfaceReadOnlyMap<K,V> - Returns:
- a read-only view
-
size
public int size()Description copied from interface:ReadOnlyMapReturns the number of entries contained in this map..- Specified by:
sizein interfaceReadOnlyMap<K,V> - Returns:
- the number of entries
-
spliterator
Description copied from interface:ReadOnlyMapReturns a spliterator over the entries contained in this map.- Specified by:
spliteratorin interfaceIterable<K>- Specified by:
spliteratorin interfaceReadOnlyMap<K,V> - Returns:
- a spliterator
-
toMutable
Creates a mutable copy of this map.- Specified by:
toMutablein interfaceImmutableMap<K,V> - Returns:
- a mutable CHAMP map
-
asMap
Description copied from interface:ReadOnlyMapWraps this map in theMapinterface - without copying.- Specified by:
asMapin interfaceReadOnlyMap<K,V> - Returns:
- the wrapped map
-
toString
Returns a string representation of this map.The string representation is consistent with the one produced by
AbstractMap.toString().
-