Implement in-place shuffle for arrays

Minor: reorder shuffle/shuffled extensions more consistently

#KT-25651 Fixed
This commit is contained in:
Ilya Gorbunov
2020-03-26 21:52:11 +03:00
parent e58f1c8932
commit 15319eb88e
10 changed files with 487 additions and 43 deletions
@@ -5447,6 +5447,213 @@ public fun CharArray.reversedArray(): CharArray {
return result
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun <T> Array<T>.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun ByteArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun ShortArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun IntArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun LongArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun FloatArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun DoubleArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun BooleanArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
public fun CharArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun <T> Array<T>.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun ByteArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun ShortArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun IntArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun LongArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun FloatArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun DoubleArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun BooleanArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
public fun CharArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Sorts elements in the array in-place according to natural sort order of the value returned by specified [selector] function.
*
@@ -906,6 +906,19 @@ public fun <T> Iterable<T>.reversed(): List<T> {
return list
}
/**
* Randomly shuffles elements in this list in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.3")
public fun <T> MutableList<T>.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
this[j] = this.set(i, this[j])
}
}
/**
* Sorts elements in the list in-place according to natural sort order of the value returned by specified [selector] function.
*
@@ -2702,6 +2702,106 @@ public inline fun UShortArray.reversedArray(): UShortArray {
return UShortArray(storage.reversedArray())
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun UIntArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun ULongArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun UByteArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun UShortArray.shuffle(): Unit {
shuffle(Random)
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun UIntArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun ULongArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun UByteArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Randomly shuffles elements in this array in-place using the specified [random] instance as the source of randomness.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun UShortArray.shuffle(random: Random): Unit {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
/**
* Sorts elements in the array in-place descending according to their natural sort order.
*/