From a709ba4a6e4f0ed023fce7c2d245f7a9bc8ce07a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 22 Jan 2016 20:59:06 +0300 Subject: [PATCH] Provide toMap with the destination mutable map to populate. Tune type projections of map mutating operations. --- .../stdlib/src/kotlin/collections/Maps.kt | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 75c05a83c47..6567958735d 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -232,30 +232,22 @@ public operator fun MutableMap.iterator(): MutableIterator> Map.mapValuesTo(destination: C, transform: (Map.Entry) -> R): C { - for (e in this) { - val newValue = transform(e) - destination.put(e.key, newValue) - } - return destination +public inline fun > Map.mapValuesTo(destination: C, transform: (Map.Entry) -> R): C { + return entries.associateByTo(destination, { it.key }, { transform(it) }) } /** * Populates the given `destination` [Map] with entries having the keys obtained * by applying the `transform` function to each entry in this [Map] and the values of this map. */ -public inline fun > Map.mapKeysTo(destination: C, transform: (Map.Entry) -> R): C { - for (e in this) { - val newKey = transform(e) - destination.put(newKey, e.value) - } - return destination +public inline fun > Map.mapKeysTo(destination: C, transform: (Map.Entry) -> R): C { + return entries.associateByTo(destination, { transform(it) }, { it.value }) } /** * Puts all the given [pairs] into this [MutableMap] with the first component in the pair being the key and the second the value. */ -public fun MutableMap.putAll(pairs: Array>): Unit { +public fun MutableMap.putAll(pairs: Array>): Unit { for ((key, value) in pairs) { put(key, value) } @@ -264,7 +256,7 @@ public fun MutableMap.putAll(pairs: Array>): Unit { /** * Puts all the elements of the given collection into this [MutableMap] with the first component in the pair being the key and the second the value. */ -public fun MutableMap.putAll(pairs: Iterable>): Unit { +public fun MutableMap.putAll(pairs: Iterable>): Unit { for ((key, value) in pairs) { put(key, value) } @@ -273,7 +265,7 @@ public fun MutableMap.putAll(pairs: Iterable>): Unit { /** * Puts all the elements of the given sequence into this [MutableMap] with the first component in the pair being the key and the second the value. */ -public fun MutableMap.putAll(pairs: Sequence>): Unit { +public fun MutableMap.putAll(pairs: Sequence>): Unit { for ((key, value) in pairs) { put(key, value) } @@ -331,7 +323,7 @@ public inline fun Map.filterValues(predicate: (V) -> Boolean): Map< * * @return the destination map. */ -public inline fun > Map.filterTo(destination: C, predicate: (Map.Entry) -> Boolean): C { +public inline fun > Map.filterTo(destination: C, predicate: (Map.Entry) -> Boolean): C { for (element in this) { if (predicate(element)) { destination.put(element.key, element.value) @@ -352,7 +344,7 @@ public inline fun Map.filter(predicate: (Map.Entry) -> Boolea * * @return the destination map. */ -public inline fun > Map.filterNotTo(destination: C, predicate: (Map.Entry) -> Boolean): C { +public inline fun > Map.filterNotTo(destination: C, predicate: (Map.Entry) -> Boolean): C { for (element in this) { if (!predicate(element)) { destination.put(element.key, element.value) @@ -371,21 +363,37 @@ public inline fun Map.filterNot(predicate: (Map.Entry) -> Boo /** * Returns a new map containing all key-value pairs from the given collection of pairs. */ -public fun Iterable>.toMap(): Map - = LinkedHashMap(collectionSizeOrNull()?.let { mapCapacity(it) } ?: 16).apply { putAll(this@toMap) } +public fun Iterable>.toMap(): Map = toMap(LinkedHashMap(collectionSizeOrNull()?.let { mapCapacity(it) } ?: 16)) + +/** + * Populates and returns the [destination] mutable map with key-value pairs from the given collection of pairs. + */ +public fun > Iterable>.toMap(destination: M): M + = destination.apply { putAll(this@toMap) } /** * Returns a new map containing all key-value pairs from the given array of pairs. */ -public fun Array>.toMap(): Map - = LinkedHashMap(mapCapacity(size)).apply { putAll(this@toMap) } +public fun Array>.toMap(): Map = toMap(LinkedHashMap(mapCapacity(size))) + +/** + * Populates and returns the [destination] mutable map with key-value pairs from the given array of pairs. + */ +public fun > Array>.toMap(destination: M): M + = destination.apply { putAll(this@toMap) } /** * Returns a new map containing all key-value pairs from the given sequence of pairs. */ -public fun Sequence>.toMap(): Map - = LinkedHashMap().apply { putAll(this@toMap) } +public fun Sequence>.toMap(): Map = toMap(LinkedHashMap()) + +/** + * Populates and returns the [destination] mutable map with key-value pairs from the given sequence of pairs. + */ + +public fun > Sequence>.toMap(destination: M): M + = destination.apply { putAll(this@toMap) } /** * Converts this [Map] to a [LinkedHashMap], maintaining the insertion order of elements added to that map afterwards. @@ -427,35 +435,35 @@ public operator fun Map.plus(map: Map): Map /** * Appends or replaces the given [pair] in this mutable map. */ -public operator fun MutableMap.plusAssign(pair: Pair) { +public operator fun MutableMap.plusAssign(pair: Pair) { put(pair.first, pair.second) } /** * Appends or replaces all pairs from the given collection of [pairs] in this mutable map. */ -public operator fun MutableMap.plusAssign(pairs: Iterable>) { +public operator fun MutableMap.plusAssign(pairs: Iterable>) { putAll(pairs) } /** * Appends or replaces all pairs from the given array of [pairs] in this mutable map. */ -public operator fun MutableMap.plusAssign(pairs: Array>) { +public operator fun MutableMap.plusAssign(pairs: Array>) { putAll(pairs) } /** * Appends or replaces all pairs from the given sequence of [pairs] in this mutable map. */ -public operator fun MutableMap.plusAssign(pairs: Sequence>) { +public operator fun MutableMap.plusAssign(pairs: Sequence>) { putAll(pairs) } /** * Appends or replaces all entries from the given [map] in this mutable map. */ -public operator fun MutableMap.plusAssign(map: Map) { +public operator fun MutableMap.plusAssign(map: Map) { putAll(map) }