MutableScatterMap
-
Cmn
class MutableScatterMap<K : Any?, V : Any?> : ScatterMap
MutableScatterMap is a container with a Map-like interface based on a flat hash table implementation (the key/value mappings are not stored by nodes but directly into arrays). The underlying implementation is designed to avoid all allocations on insertion, removal, retrieval, and iteration. Allocations may still happen on insertion when the underlying storage needs to grow to accommodate newly added entries to the table. In addition, this implementation minimizes memory usage by avoiding the use of separate objects to hold key/value pairs.
This implementation makes no guarantee as to the order of the keys and values stored, nor does it make guarantees that the order remains constant over time.
This implementation is not thread-safe: if multiple threads access this container concurrently, and one or more threads modify the structure of the map (insertion or removal for instance), the calling code must provide the appropriate synchronization. Multiple threads are safe to read from this map concurrently if no write is happening.
Note: when a Map is absolutely necessary, you can use the method asMap to create a thin wrapper around a MutableScatterMap. Please refer to asMap for more details and caveats.
Note: when a MutableMap is absolutely necessary, you can use the method asMutableMap to create a thin wrapper around a MutableScatterMap. Please refer to asMutableMap for more details and caveats.
MutableScatterMap and SimpleArrayMap: like SimpleArrayMap, MutableScatterMap is designed to avoid the allocation of extra objects when inserting new entries in the map. However, the implementation of MutableScatterMap offers better performance characteristics compared to SimpleArrayMap and is thus generally preferable. If memory usage is a concern, SimpleArrayMap automatically shrinks its storage to avoid using more memory than necessary. You can also control memory usage with MutableScatterMap by manually calling MutableScatterMap.trim.
Note: For the JS target the implementation delegates to a regular map.
| See also | |
|---|---|
Map |
Summary
Public constructors |
|
|---|---|
<K : Any?, V : Any?> MutableScatterMap(initialCapacity: Int)Creates a new |
Cmn
android
N
JS
|
Public functions |
||
|---|---|---|
MutableMap<K, V> |
Wraps this |
Cmn
android
N
JS
|
Unit |
clear()Removes all mappings from this map. |
Cmn
android
N
JS
|
inline V |
compute(key: K, computeBlock: (key, value?) -> V)Retrieves a value for |
Cmn
android
N
JS
|
inline V |
getOrPut(key: K, defaultValue: () -> V)Returns the value to which the specified |
Cmn
android
N
JS
|
inline operator Unit |
minusAssign(key: K)Removes the specified |
Cmn
android
N
JS
|
inline operator Unit |
minusAssign(keys: Array<K>)Removes the specified |
Cmn
android
N
JS
|
inline operator Unit |
minusAssign(keys: Iterable<K>)Removes the specified |
Cmn
android
N
JS
|
inline operator Unit |
minusAssign(keys: ObjectList<K>)Removes the specified |
Cmn
android
N
JS
|
inline operator Unit |
minusAssign(keys: ScatterSet<K>)Removes the specified |
Cmn
android
N
JS
|
inline operator Unit |
minusAssign(keys: Sequence<K>)Removes the specified |
Cmn
android
N
JS
|
inline operator Unit |
plusAssign(from: Map<K, V>)Puts all the key/value mappings in the |
Cmn
android
N
JS
|
inline operator Unit |
plusAssign(from: ScatterMap<K, V>)Puts all the key/value mappings in the |
Cmn
android
N
JS
|
inline operator Unit |
plusAssign(pair: Pair<K, V>)Puts the key/value mapping from the |
Cmn
android
N
JS
|
inline operator Unit |
plusAssign(pairs: Array<Pair<K, V>>)Puts all the |
Cmn
android
N
JS
|
inline operator Unit |
plusAssign(pairs: Iterable<Pair<K, V>>)Puts all the |
Cmn
android
N
JS
|
inline operator Unit |
plusAssign(pairs: Sequence<Pair<K, V>>)Puts all the |
Cmn
android
N
JS
|
V? |
put(key: K, value: V) |
Cmn
android
N
JS
|
Unit |
Puts all the key/value mappings in the |
Cmn
android
N
JS
|
Unit |
putAll(from: ScatterMap<K, V>)Puts all the key/value mappings in the |
Cmn
android
N
JS
|
Unit |
Puts all the |
Cmn
android
N
JS
|
Unit |
Puts all the |
Cmn
android
N
JS
|
Unit |
Puts all the |
Cmn
android
N
JS
|
V? |
remove(key: K)Removes the specified |
Cmn
android
N
JS
|
Boolean |
remove(key: K, value: V)Removes the specified |
Cmn
android
N
JS
|
inline Unit |
Removes any mapping for which the specified |
Cmn
android
N
JS
|
operator Unit |
set(key: K, value: V) |
Cmn
android
N
JS
|
Int |
trim()Trims this |
Cmn
android
N
JS
|
Inherited functions |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited properties |
|---|
Public constructors
MutableScatterMap
<K : Any?, V : Any?> MutableScatterMap(
initialCapacity: Int = DefaultScatterCapacity
)
Creates a new MutableScatterMap
| Parameters | |
|---|---|
initialCapacity: Int = DefaultScatterCapacity |
The initial desired capacity for this container. The container will honor this value by guaranteeing its internal structures can hold that many entries without requiring any allocations. The initial capacity can be set to 0. |
Public functions
asMutableMap
fun asMutableMap(): MutableMap<K, V>
Wraps this ScatterMap with a MutableMap interface. The MutableMap is backed by the ScatterMap, so changes to the ScatterMap are reflected in the MutableMap and vice-versa. If the ScatterMap is modified while an iteration over the MutableMap is in progress (and vice- versa), the results of the iteration are undefined.
Note: while this method is useful to use this MutableScatterMap with APIs accepting MutableMap interfaces, it is less efficient to do so than to use MutableScatterMap's APIs directly. While the MutableMap implementation returned by this method tries to be as efficient as possible, the semantics of MutableMap may require the allocation of temporary objects for access and iteration.
compute
inline fun compute(key: K, computeBlock: (key, value?) -> V): V
Retrieves a value for key and computes a new value based on the existing value (or null if the key is not in the map). The computed value is then stored in the map for the given key.
| Returns | |
|---|---|
V |
value computed by |
getOrPut
inline fun getOrPut(key: K, defaultValue: () -> V): V
Returns the value to which the specified key is mapped, if the value is present in the map and not null. Otherwise, calls defaultValue() and puts the result in the map associated with key.
minusAssign
inline operator fun minusAssign(key: K): Unit
Removes the specified key and its associated value from the map.
minusAssign
inline operator fun minusAssign(keys: Array<K>): Unit
Removes the specified keys and their associated value from the map.
minusAssign
inline operator fun minusAssign(keys: Iterable<K>): Unit
Removes the specified keys and their associated value from the map.
minusAssign
inline operator fun minusAssign(keys: ObjectList<K>): Unit
Removes the specified keys and their associated value from the map.
minusAssign
inline operator fun minusAssign(keys: ScatterSet<K>): Unit
Removes the specified keys and their associated value from the map.
minusAssign
inline operator fun minusAssign(keys: Sequence<K>): Unit
Removes the specified keys and their associated value from the map.
plusAssign
inline operator fun plusAssign(from: Map<K, V>): Unit
Puts all the key/value mappings in the from map into this map.
plusAssign
inline operator fun plusAssign(from: ScatterMap<K, V>): Unit
Puts all the key/value mappings in the from map into this map.
plusAssign
inline operator fun plusAssign(pair: Pair<K, V>): Unit
Puts the key/value mapping from the pair in this map, using the first element as the key, and the second element as the value.
plusAssign
inline operator fun plusAssign(pairs: Array<Pair<K, V>>): Unit
Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.
plusAssign
inline operator fun plusAssign(pairs: Iterable<Pair<K, V>>): Unit
Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.
plusAssign
inline operator fun plusAssign(pairs: Sequence<Pair<K, V>>): Unit
Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.
put
fun put(key: K, value: V): V?
Creates a new mapping from key to value in this map. If key is already present in the map, the association is modified and the previously associated value is replaced with value. If key is not present, a new entry is added to the map, which may require to grow the underlying storage and cause allocations. Return the previous value associated with the key, or null if the key was not present in the map.
putAll
fun putAll(from: Map<K, V>): Unit
Puts all the key/value mappings in the from map into this map.
putAll
fun putAll(from: ScatterMap<K, V>): Unit
Puts all the key/value mappings in the from map into this map.
putAll
fun putAll(pairs: Array<Pair<K, V>>): Unit
Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.
putAll
fun putAll(pairs: Iterable<Pair<K, V>>): Unit
Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.
putAll
fun putAll(pairs: Sequence<Pair<K, V>>): Unit
Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.
remove
fun remove(key: K): V?
Removes the specified key and its associated value from the map. If the key was present in the map, this function returns the value that was present before removal.
remove
fun remove(key: K, value: V): Boolean
Removes the specified key and its associated value from the map if the associated value equals value. Returns whether the removal happened.
removeIf
inline fun removeIf(predicate: (K, V) -> Boolean): Unit
Removes any mapping for which the specified predicate returns true.
set
operator fun set(key: K, value: V): Unit
Creates a new mapping from key to value in this map. If key is already present in the map, the association is modified and the previously associated value is replaced with value. If key is not present, a new entry is added to the map, which may require to grow the underlying storage and cause allocations.
trim
fun trim(): Int
Trims this MutableScatterMap's storage so it is sized appropriately to hold the current mappings.
Returns the number of empty entries removed from this map's storage. Returns be 0 if no trimming is necessary or possible.