Add associateWith to Array<T> #KT-30372
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -1247,6 +1247,93 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun associateWith() {
|
||||
val items = arrayOf("Alice", "Bob", "Carol")
|
||||
val itemsWithTheirLength = items.associateWith { it.length }
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 3, "Carol" to 5), itemsWithTheirLength)
|
||||
|
||||
val updatedLength = items.copyOfRange(1, 3)
|
||||
.associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.toLowerCase().count { it in "aeuio" } }
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 1, "Carol" to 2), updatedLength)
|
||||
}
|
||||
|
||||
@Test fun associateWithPrimitives() {
|
||||
assertEquals(
|
||||
mapOf(1 to "1", 2 to "2", 3 to "3"),
|
||||
intArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toByte() to "1", 2.toByte() to "2", 3.toByte() to "3"),
|
||||
byteArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toShort() to "1", 2.toShort() to "2", 3.toShort() to "3"),
|
||||
shortArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1L to "1", 2L to "2", 3L to "3"),
|
||||
longArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1f to "1", 2f to "2", 3f to "3"),
|
||||
floatArrayOf(1f, 2f, 3f).associateWith { if (it == 1f) "1" else if (it == 2f) "2" else "3" }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.0 to "1", 2.0 to "2", 3.0 to "3"),
|
||||
doubleArrayOf(1.0, 2.0, 3.0).associateWith { if (it == 1.0) "1" else if (it == 2.0) "2" else "3" }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf('1' to "1", '2' to "2", '3' to "3"),
|
||||
charArrayOf('1', '2', '3').associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(false to "false", true to "true"),
|
||||
booleanArrayOf(false, true).associateWith { it.toString() }
|
||||
)
|
||||
}
|
||||
|
||||
@Test fun associateWithToPrimitives() {
|
||||
val expected = mapOf(1 to "one", 2 to "two", 3 to "three")
|
||||
assertEquals(
|
||||
mapOf(1 to "one", 2 to "2", 3 to "3"),
|
||||
intArrayOf(2, 3).associateWithTo(expected.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toByte() to "one", 2.toByte() to "2", 3.toByte() to "3"),
|
||||
byteArrayOf(2, 3).associateWithTo(expected.mapKeys { it.key.toByte() }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toShort() to "one", 2.toShort() to "2", 3.toShort() to "3"),
|
||||
shortArrayOf(2, 3).associateWithTo(expected.mapKeys { it.key.toShort() }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1L to "one", 2L to "2", 3L to "3"),
|
||||
longArrayOf(2, 3).associateWithTo(expected.mapKeys { it.key.toLong() }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1f to "one", 2f to "2", 3f to "3"),
|
||||
floatArrayOf(2f, 3f).associateWithTo(expected.mapKeys { it.key.toFloat() }.toMutableMap()) {
|
||||
if (it == 1f) "1" else if (it == 2f) "2" else "3"
|
||||
}
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.0 to "one", 2.0 to "2", 3.0 to "3"),
|
||||
doubleArrayOf(2.0, 3.0).associateWithTo(expected.mapKeys { it.key.toDouble() }.toMutableMap()) {
|
||||
if (it == 1.0) "1" else if (it == 2.0) "2" else "3"
|
||||
}
|
||||
)
|
||||
assertEquals(
|
||||
mapOf('1' to "one", '2' to "2", '3' to "3"),
|
||||
charArrayOf('2', '3').associateWithTo(expected.mapKeys { '0' + it.key }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(false to "three", true to "true"),
|
||||
booleanArrayOf(true, true).associateWithTo(expected.mapKeys { it.key % 2 == 0 }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
}
|
||||
|
||||
@Test fun reverseInPlace() {
|
||||
|
||||
fun <TArray, T> doTest(build: Iterable<Int>.() -> TArray, reverse: TArray.() -> Unit, snapshot: TArray.() -> List<T>) {
|
||||
|
||||
@@ -723,6 +723,47 @@ class UnsignedArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun associateWithPrimitives() {
|
||||
assertEquals(
|
||||
mapOf(1u to "1", 2u to "2", 3u to "3"),
|
||||
uintArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toUByte() to "1", 2.toUByte() to "2", 3.toUByte() to "3"),
|
||||
ubyteArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toUShort() to "1", 2.toUShort() to "2", 3.toUShort() to "3"),
|
||||
ushortArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1UL to "1", 2UL to "2", 3UL to "3"),
|
||||
ulongArrayOf(1, 2, 3).associateWith { it.toString() }
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun associateWithToPrimitives() {
|
||||
val expected = mapOf(1u to "one", 2u to "two", 3u to "three")
|
||||
assertEquals(
|
||||
mapOf(1u to "one", 2u to "2", 3u to "3"),
|
||||
uintArrayOf(2, 3).associateWithTo(expected.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toUByte() to "one", 2.toUByte() to "2", 3.toUByte() to "3"),
|
||||
ubyteArrayOf(2, 3).associateWithTo(expected.mapKeys { it.key.toUByte() }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1.toUShort() to "one", 2.toUShort() to "2", 3.toUShort() to "3"),
|
||||
ushortArrayOf(2, 3).associateWithTo(expected.mapKeys { it.key.toUShort() }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
assertEquals(
|
||||
mapOf(1UL to "one", 2UL to "2", 3UL to "3"),
|
||||
ulongArrayOf(2, 3).associateWithTo(expected.mapKeys { it.key.toULong() }.toMutableMap()) { it.toString() }
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun elementAt() {
|
||||
expect(0u) { ubyteArrayOf(0, 1, 2).elementAt(0) }
|
||||
|
||||
+2
@@ -805,6 +805,8 @@ public final class kotlin/collections/ArraysKt {
|
||||
public static final fun associateTo ([Ljava/lang/Object;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateTo ([SLjava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateTo ([ZLjava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWith ([Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun associateWithTo ([Ljava/lang/Object;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun average ([B)D
|
||||
public static final fun average ([D)D
|
||||
public static final fun average ([F)D
|
||||
|
||||
@@ -437,10 +437,15 @@ object Snapshots : TemplateGroupBase() {
|
||||
}
|
||||
|
||||
val f_associateWith = fn("associateWith(valueSelector: (K) -> V)") {
|
||||
include(Iterables, Sequences, CharSequences)
|
||||
include(Iterables, Sequences, CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
inline()
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
since("1.3")
|
||||
specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
since("1.4")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
}
|
||||
typeParam("K", primary = true)
|
||||
typeParam("V")
|
||||
returns("Map<K, V>")
|
||||
@@ -472,10 +477,15 @@ object Snapshots : TemplateGroupBase() {
|
||||
}
|
||||
|
||||
val f_associateWithTo = fn("associateWithTo(destination: M, valueSelector: (K) -> V)") {
|
||||
include(Iterables, Sequences, CharSequences)
|
||||
include(Iterables, Sequences, CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
inline()
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() }
|
||||
since("1.3")
|
||||
specialFor(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
since("1.4")
|
||||
annotation("@ExperimentalStdlibApi")
|
||||
}
|
||||
typeParam("K", primary = true)
|
||||
typeParam("V")
|
||||
typeParam("M : MutableMap<in K, in V>")
|
||||
|
||||
Reference in New Issue
Block a user