Add associateWith to Array<T> #KT-30372

This commit is contained in:
Abduqodiri Qurbonzoda
2020-01-31 07:08:14 +03:00
parent e632d58936
commit b60633d79a
6 changed files with 608 additions and 2 deletions
@@ -8428,6 +8428,328 @@ public inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateTo(desti
return destination
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
public inline fun <K, V> Array<out K>.associateWith(valueSelector: (K) -> V): Map<K, V> {
val result = LinkedHashMap<K, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> ByteArray.associateWith(valueSelector: (Byte) -> V): Map<Byte, V> {
val result = LinkedHashMap<Byte, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> ShortArray.associateWith(valueSelector: (Short) -> V): Map<Short, V> {
val result = LinkedHashMap<Short, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> IntArray.associateWith(valueSelector: (Int) -> V): Map<Int, V> {
val result = LinkedHashMap<Int, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> LongArray.associateWith(valueSelector: (Long) -> V): Map<Long, V> {
val result = LinkedHashMap<Long, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> FloatArray.associateWith(valueSelector: (Float) -> V): Map<Float, V> {
val result = LinkedHashMap<Float, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> DoubleArray.associateWith(valueSelector: (Double) -> V): Map<Double, V> {
val result = LinkedHashMap<Double, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> BooleanArray.associateWith(valueSelector: (Boolean) -> V): Map<Boolean, V> {
val result = LinkedHashMap<Boolean, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> CharArray.associateWith(valueSelector: (Char) -> V): Map<Char, V> {
val result = LinkedHashMap<Char, V>()
return associateWithTo(result, valueSelector)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
public inline fun <K, V, M : MutableMap<in K, in V>> Array<out K>.associateWithTo(destination: M, valueSelector: (K) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Byte, in V>> ByteArray.associateWithTo(destination: M, valueSelector: (Byte) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Short, in V>> ShortArray.associateWithTo(destination: M, valueSelector: (Short) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Int, in V>> IntArray.associateWithTo(destination: M, valueSelector: (Int) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Long, in V>> LongArray.associateWithTo(destination: M, valueSelector: (Long) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Float, in V>> FloatArray.associateWithTo(destination: M, valueSelector: (Float) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Double, in V>> DoubleArray.associateWithTo(destination: M, valueSelector: (Double) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Boolean, in V>> BooleanArray.associateWithTo(destination: M, valueSelector: (Boolean) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in Char, in V>> CharArray.associateWithTo(destination: M, valueSelector: (Char) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Appends all elements to the given [destination] collection.
*/
@@ -3970,6 +3970,150 @@ public inline fun ShortArray.toUShortArray(): UShortArray {
return UShortArray(this.copyOf())
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> UIntArray.associateWith(valueSelector: (UInt) -> V): Map<UInt, V> {
val result = LinkedHashMap<UInt, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> ULongArray.associateWith(valueSelector: (ULong) -> V): Map<ULong, V> {
val result = LinkedHashMap<ULong, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> UByteArray.associateWith(valueSelector: (UByte) -> V): Map<UByte, V> {
val result = LinkedHashMap<UByte, V>()
return associateWithTo(result, valueSelector)
}
/**
* Returns a [Map] where keys are elements from the given array and values are
* produced by the [valueSelector] function applied to each element.
*
* If any two elements are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original array.
*
* @sample samples.collections.Collections.Transformations.associateWith
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> UShortArray.associateWith(valueSelector: (UShort) -> V): Map<UShort, V> {
val result = LinkedHashMap<UShort, V>()
return associateWithTo(result, valueSelector)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in UInt, in V>> UIntArray.associateWithTo(destination: M, valueSelector: (UInt) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in ULong, in V>> ULongArray.associateWithTo(destination: M, valueSelector: (ULong) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in UByte, in V>> UByteArray.associateWithTo(destination: M, valueSelector: (UByte) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two elements are equal, the last one overwrites the former value in the map.
*
* @sample samples.collections.Collections.Transformations.associateWithTo
*/
@SinceKotlin("1.4")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V, M : MutableMap<in UShort, in V>> UShortArray.associateWithTo(destination: M, valueSelector: (UShort) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array.
*