- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
Serializable,Iterable<Map.Entry<K,,V>> ImmutableMap<K,,V> ImmutableSequencedMap<K,,V> ReadOnlyMap<K,,V> ReadOnlySequencedMap<K,V>
ImmutableSequencedMap interface using a Compressed
Hash-Array Mapped Prefix-tree (CHAMP) and a bit-mapped trie (Vector).
Features:
- supports up to 230 entries
- allows null keys and null values
- is immutable
- is thread-safe
- iterates in the order, in which keys were inserted
Performance characteristics:
- put, putFirst, putLast: O(log₃₂ N) in an amortized sense, because we sometimes have to renumber the elements.
- remove: O(log₃₂ N) in an amortized sense, because we sometimes have to renumber the elements.
- containsKey: O(log₃₂ N)
- toMutable: O(1) + O(log₃₂ N) distributed across subsequent updates in the mutable copy
- clone: O(1)
- iterator creation: O(log₃₂ N)
- iterator.next: O(1)
- getFirst, getLast: O(log₃₂ N)
Implementation details:
This map performs read and write operations of single elements in O(log N) time, and in O(log N) space, where N is the number of elements in the set.
The CHAMP trie contains nodes that may be shared with other maps.
If a write operation is performed on a node, then this set creates a copy of the node and of all parent nodes up to the root (copy-path-on-write). Since the CHAMP trie has a fixed maximal height, the cost is O(1).
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.
Insertion Order:
This map uses a counter to keep track of the insertion order. It stores the current value of the counter in the sequence number field of each data entry. If the counter wraps around, it must renumber all sequence numbers.
The renumbering is why the add and remove methods are O(1)
only in an amortized sense.
To support iteration, we use a Vector. The Vector has the same contents as the CHAMP trie. However, its elements are stored in insertion order.
If an element is removed from the CHAMP trie that is not the first or the last element of the Vector, we replace its corresponding element in the Vector by a tombstone. If the element is at the start or end of the Vector, we remove the element and all its neighboring tombstones from the Vector.
A tombstone can store the number of neighboring tombstones in ascending and in descending direction. We use these numbers to skip tombstones when we iterate over the vector. Since we only allow iteration in ascending or descending order from one of the ends of the vector, we do not need to keep the number of neighbors in all tombstones up to date. It is sufficient, if we update the neighbor with the lowest index and the one with the highest index.
If the number of tombstones exceeds half of the size of the collection, we renumber all sequence numbers, and we create a new Vector.
The immutable version of this set 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:
For a similar design, see 'SimpleImmutableSequencedMap.scala'. Note, that this code is not a derivative of that code.
- The Scala library. SimpleImmutableSequencedMap.scala. Copyright EPFL and Lightbend, Inc. Apache License 2.0.
- github.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedChampVectorMap(@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.static <K,V> @NonNull ChampVectorMap <K, V> Returns an immutable copy of the provided map.static <K,V> @NonNull ChampVectorMap <K, V> Returns an immutable copy of the provided map.booleanCompares the specified object with this map for equality.Gets the first entry in this map ornullif this map is empty.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.Gets the last entry in this map ornullif this map is empty.intmaxSize()Returns the maximal number of entries that this map type can holdprotected @NonNull ChampVectorMap<K, V> newInstance(@NonNull PrivateData privateData) Creates a new instance with the provided privateData object as its internal data structure.static <K,V> @NonNull ChampVectorMap <K, V> 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.Creates an entry for the specified key and value and adds it to the front of the map if an entry for the specified key is not already present.Creates an entry for the specified key and value and adds it to the end of the map if an entry for the specified key is not already present.Returns a reversed-order view of 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
retainAllMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableSequencedMap
putKeyValues, removeFirst, removeLast, reverseMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyMap
containsEntry, containsValue, getOrDefaultMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedMap
readOnlyEntrySet, readOnlyKeySet, readOnlyValues
-
Constructor Details
-
ChampVectorMap
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
-
copyOf
public static <K,V> @NonNull ChampVectorMap<K,V> copyOf(@NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> map) 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
-
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> - Specified by:
clearin interfaceImmutableSequencedMap<K,V> - Returns:
- this set instance if it is already empty, or a different set instance that is empty.
-
containsKey
Description copied from interface:ReadOnlyMapReturnstrueif 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. -
firstEntry
Description copied from interface:ReadOnlySequencedMapGets the first entry in this map ornullif this map is empty.- Specified by:
firstEntryin interfaceReadOnlySequencedMap<K,V> - Returns:
- the first entry or
null
-
lastEntry
Description copied from interface:ReadOnlySequencedMapGets the last entry in this map ornullif this map is empty.- Specified by:
lastEntryin interfaceReadOnlySequencedMap<K,V> - Returns:
- the last entry or
null
-
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> - Specified by:
putin interfaceImmutableSequencedMap<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 ChampVectorMap<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> - Specified by:
putAllin interfaceImmutableSequencedMap<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
-
putFirst
Description copied from interface:ImmutableSequencedMapCreates an entry for the specified key and value and adds it to the front of the map if an entry for the specified key is not already present. If this map already contains an entry for the specified key, replaces the value and moves the entry to the front.- Specified by:
putFirstin interfaceImmutableSequencedMap<K,V> - Parameters:
key- the keyvalue- the value- Returns:
- this map instance if no changes are needed, or a different map instance with the applied changes.
-
putLast
Description copied from interface:ImmutableSequencedMapCreates an entry for the specified key and value and adds it to the end of the map if an entry for the specified key is not already present. If this map already contains an entry for the specified key, replaces the value and moves the entry to the end.- Specified by:
putLastin interfaceImmutableSequencedMap<K,V> - Parameters:
key- the keyvalue- the value- Returns:
- this map instance if no changes are needed, or a different map instance with the applied changes.
-
readOnlyReversed
Description copied from interface:ReadOnlySequencedMapReturns a reversed-order view of this map.Changes to the underlying map are visible in the reversed view.
- Specified by:
readOnlyReversedin interfaceReadOnlySequencedMap<K,V> - Returns:
- a reversed-order view of this map
-
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> - Specified by:
removein interfaceImmutableSequencedMap<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> - Specified by:
removeAllin interfaceImmutableSequencedMap<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> - Specified by:
retainAllin interfaceImmutableSequencedMap<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
-
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> - Specified by:
toMutablein interfaceImmutableSequencedMap<K,V> - Returns:
- a mutable sequenced CHAMP map
-
asMap
Description copied from interface:ReadOnlyMapWraps this map in theMapinterface - without copying.- Specified by:
asMapin interfaceReadOnlyMap<K,V> - Specified by:
asMapin interfaceReadOnlySequencedMap<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(). -
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> - Specified by:
characteristicsin interfaceReadOnlySequencedMap<K,V> - Returns:
- characteristics.
-