Introduce toMap with key-value pair selector.

#KT-6657
This commit is contained in:
Ilya Gorbunov
2015-11-30 04:19:52 +03:00
parent 2966420d24
commit 544bc9a70c
6 changed files with 232 additions and 3 deletions
+126
View File
@@ -6004,6 +6004,132 @@ public inline fun <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Sh
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <T, K, V> Array<out T>.toMap(transform: (T) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> BooleanArray.toMap(transform: (Boolean) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> ByteArray.toMap(transform: (Byte) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> CharArray.toMap(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> DoubleArray.toMap(transform: (Double) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> FloatArray.toMap(transform: (Float) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> IntArray.toMap(transform: (Int) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> LongArray.toMap(transform: (Long) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> ShortArray.toMap(transform: (Short) -> Pair<K, V>): Map<K, V> {
val capacity = (size/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing the elements from the given array indexed by the key
* returned from [selector] function applied to each element.
@@ -1082,6 +1082,20 @@ public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T)
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given collection.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <T, K, V> Iterable<T>.toMap(transform: (T) -> Pair<K, V>): Map<K, V> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing the elements from the given collection indexed by the key
* returned from [selector] function applied to each element.
@@ -539,6 +539,19 @@ public inline fun <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T)
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given sequence.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <T, K, V> Sequence<T>.toMap(transform: (T) -> Pair<K, V>): Map<K, V> {
val result = LinkedHashMap<K, V>()
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing the elements from the given sequence indexed by the key
* returned from [selector] function applied to each element.
@@ -842,6 +842,20 @@ public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -
return result
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function applied to characters of the given char sequence.
* If any of two pairs would have the same key the last one gets added to the map.
*/
@kotlin.jvm.JvmName("toMapOfPairs")
public inline fun <K, V> CharSequence.toMap(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = (length/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result += transform(element)
}
return result
}
/**
* Returns a [Map] containing the characters from the given char sequence indexed by the key
* returned from [selector] function applied to each character.