Provide MutableList.fill for JS, annotate new API with @SinceKotlin

#KT-8823 #KT-9010
This commit is contained in:
Ilya Gorbunov
2017-06-09 01:29:29 +03:00
parent bb22d6647b
commit a8fef3a385
4 changed files with 19 additions and 1 deletions
+12
View File
@@ -79,6 +79,18 @@ public fun <T> setOf(element: T): Set<T> = hashSetOf(element)
*/ */
public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair) public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair)
/**
* Fills the list with the provided [value].
*
* Each element in the list gets replaced with the [value].
*/
@SinceKotlin("1.2")
public fun <T> MutableList<T>.fill(value: T) {
for (index in 0..lastIndex) {
this[index] = value
}
}
/** /**
* Sorts elements in the list in-place according to their natural sort order. * Sorts elements in the list in-place according to their natural sort order.
*/ */
@@ -149,6 +149,8 @@ expect inline fun <reified T> Array<out T>?.orEmpty(): Array<out T>
expect inline fun <reified T> Collection<T>.toTypedArray(): Array<T> expect inline fun <reified T> Collection<T>.toTypedArray(): Array<T>
@SinceKotlin("1.2")
expect fun <T> MutableList<T>.fill(value: T): Unit
expect fun <T : Comparable<T>> MutableList<T>.sort(): Unit expect fun <T : Comparable<T>> MutableList<T>.sort(): Unit
expect fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit expect fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit
@@ -291,6 +291,7 @@ public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
@SinceKotlin("1.2")
public inline fun <T> MutableList<T>.fill(value: T) { public inline fun <T> MutableList<T>.fill(value: T) {
java.util.Collections.fill(this, value) java.util.Collections.fill(this, value)
} }
@@ -301,6 +302,7 @@ public inline fun <T> MutableList<T>.fill(value: T) {
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
@SinceKotlin("1.2")
public inline fun <T> MutableList<T>.shuffle() { public inline fun <T> MutableList<T>.shuffle() {
java.util.Collections.shuffle(this) java.util.Collections.shuffle(this)
} }
@@ -310,6 +312,7 @@ public inline fun <T> MutableList<T>.shuffle() {
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
@SinceKotlin("1.2")
public inline fun <T> MutableList<T>.shuffle(random: java.util.Random) { public inline fun <T> MutableList<T>.shuffle(random: java.util.Random) {
java.util.Collections.shuffle(this, random) java.util.Collections.shuffle(this, random)
} }
@@ -318,6 +321,7 @@ public inline fun <T> MutableList<T>.shuffle(random: java.util.Random) {
* Returns a new list with the elements of this list randomly shuffled. * Returns a new list with the elements of this list randomly shuffled.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
@SinceKotlin("1.2")
public fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() } public fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() }
/** /**
@@ -325,4 +329,5 @@ public fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle
* using the specified [random] instance as the source of randomness. * using the specified [random] instance as the source of randomness.
*/ */
@kotlin.jvm.JvmVersion @kotlin.jvm.JvmVersion
@SinceKotlin("1.2")
public fun <T> Iterable<T>.shuffled(random: java.util.Random): List<T> = toMutableList().apply { shuffle(random) } public fun <T> Iterable<T>.shuffled(random: java.util.Random): List<T> = toMutableList().apply { shuffle(random) }
@@ -95,7 +95,6 @@ class MutableCollectionTest {
} }
} }
@JvmVersion
@Test fun listFill() { @Test fun listFill() {
val list = MutableList(3) { it } val list = MutableList(3) { it }
list.fill(42) list.fill(42)