MutableScatterMap

class MutableScatterMap<K, V>(initialCapacity: Int = DefaultScatterCapacity) : ScatterMap<K, V>

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.

Parameters

initialCapacity

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.

See also

Constructors

Link copied to clipboard
constructor(initialCapacity: Int = DefaultScatterCapacity)

Creates a new MutableScatterMap

Properties

Link copied to clipboard

Returns the number of key-value pairs that can be stored in this map without requiring internal storage reallocation.

Link copied to clipboard
val size: Int

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

Functions

Link copied to clipboard
inline fun all(predicate: (K, V) -> Boolean): Boolean

Returns true if all entries match the given predicate.

Link copied to clipboard
fun any(): Boolean

Returns true if this map has at least one entry.

inline fun any(predicate: (K, V) -> Boolean): Boolean

Returns true if at least one entry matches the given predicate.

Link copied to clipboard
fun asMap(): Map<K, V>

Wraps this ScatterMap with a Map interface. The Map is backed by the ScatterMap, so changes to the ScatterMap are reflected in the Map. If the ScatterMap is modified while an iteration over the Map is in progress, the results of the iteration are undefined.

Link copied to clipboard

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.

Link copied to clipboard
fun clear()

Removes all mappings from this map.

Link copied to clipboard
inline fun compute(key: K, computeBlock: (key: K, value: V?) -> 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.

Link copied to clipboard
operator fun contains(key: K): Boolean

Returns true if the specified key is present in this hash map, false otherwise.

Link copied to clipboard
fun containsKey(key: K): Boolean

Returns true if the specified key is present in this hash map, false otherwise.

Link copied to clipboard
fun containsValue(value: V): Boolean

Returns true if the specified value is present in this hash map, false otherwise.

Link copied to clipboard
fun count(): Int

Returns the number of entries in this map.

inline fun count(predicate: (K, V) -> Boolean): Int

Returns the number of entries matching the given predicate.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Compares the specified object other with this hash map for equality. The two objects are considered equal if other:

Link copied to clipboard
inline fun forEach(block: (key: K, value: V) -> Unit)

Iterates over every key/value pair stored in this map by invoking the specified block lambda.

Link copied to clipboard
inline fun forEachKey(block: (key: K) -> Unit)

Iterates over every key stored in this map by invoking the specified block lambda.

Link copied to clipboard
inline fun forEachValue(block: (value: V) -> Unit)

Iterates over every value stored in this map by invoking the specified block lambda.

Link copied to clipboard
operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

Link copied to clipboard
fun getOrDefault(key: K, defaultValue: V): V

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

Link copied to clipboard
inline fun getOrElse(key: K, defaultValue: () -> V): V

Returns the value for the given key if the value is present and not null. Otherwise, returns the result of the defaultValue function.

Link copied to clipboard
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.

Link copied to clipboard
open override fun hashCode(): Int

Returns the hash code value for this map. The hash code the sum of the hash codes of each key/value pair.

Link copied to clipboard

Indicates whether this map is empty.

Link copied to clipboard

Returns true if this map is not empty.

Link copied to clipboard
fun joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: (key: K, value: V) -> CharSequence? = null): String

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

Link copied to clipboard
inline operator fun minusAssign(key: K)

Removes the specified key and its associated value from the map.

inline operator fun minusAssign(keys: ScatterSet<K>)
inline operator fun minusAssign(keys: Array<out K>)
inline operator fun minusAssign(keys: Iterable<K>)
inline operator fun minusAssign(keys: Sequence<K>)

Removes the specified keys and their associated value from the map.

Link copied to clipboard
fun none(): Boolean

Returns true if this map has no entries.

Link copied to clipboard
inline operator fun plusAssign(from: ScatterMap<K, V>)
inline operator fun plusAssign(from: Map<K, V>)

Puts all the key/value mappings in the from map into this map.

inline operator fun plusAssign(pairs: Array<out Pair<K, V>>)
inline operator fun plusAssign(pairs: Iterable<Pair<K, V>>)
inline operator fun plusAssign(pairs: Sequence<Pair<K, V>>)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

inline operator fun plusAssign(pair: Pair<K, V>)

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.

Link copied to clipboard
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.

Link copied to clipboard
fun putAll(from: ScatterMap<K, V>)
fun putAll(from: Map<K, V>)

Puts all the key/value mappings in the from map into this map.

fun putAll(pairs: Array<out Pair<K, V>>)
fun putAll(pairs: Iterable<Pair<K, V>>)
fun putAll(pairs: Sequence<Pair<K, V>>)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

Link copied to clipboard
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.

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.

Link copied to clipboard
inline fun removeIf(predicate: (K, V) -> Boolean)

Removes any mapping for which the specified predicate returns true.

Link copied to clipboard
operator fun set(key: K, value: 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.

Link copied to clipboard
open override fun toString(): String

Returns a string representation of this map. The map is denoted in the string by the {}. Each key/value pair present in the map is represented inside '{}by a substring of the form key=value, and pairs are separated by , `.

Link copied to clipboard
fun trim(): Int

Trims this MutableScatterMap's storage so it is sized appropriately to hold the current mappings.