Introduce MutableList.fill and shuffle/shuffled extensions for JVM only

'shuffle' and 'fill' are inline only, but 'shuffled' is not.

#KT-8823 Fixed
#KT-9010 Fixed
This commit is contained in:
Ilya Gorbunov
2017-06-06 16:37:56 +03:00
parent 16d3d2f764
commit bb22d6647b
4 changed files with 82 additions and 0 deletions
@@ -283,3 +283,46 @@ public fun <T : Comparable<T>> MutableList<T>.sort(): Unit {
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1) java.util.Collections.sort(this, comparator)
}
/**
* Fills the list with the provided [value].
*
* Each element in the list gets replaced with the [value].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> MutableList<T>.fill(value: T) {
java.util.Collections.fill(this, value)
}
/**
* Randomly shuffles elements in this mutable list.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> MutableList<T>.shuffle() {
java.util.Collections.shuffle(this)
}
/**
* Randomly shuffles elements in this mutable list using the specified [random] instance as the source of randomness.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> MutableList<T>.shuffle(random: java.util.Random) {
java.util.Collections.shuffle(this, random)
}
/**
* Returns a new list with the elements of this list randomly shuffled.
*/
@kotlin.jvm.JvmVersion
public fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() }
/**
* Returns a new list with the elements of this list randomly shuffled
* using the specified [random] instance as the source of randomness.
*/
@kotlin.jvm.JvmVersion
public fun <T> Iterable<T>.shuffled(random: java.util.Random): List<T> = toMutableList().apply { shuffle(random) }