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
@@ -1577,6 +1577,24 @@ public final class kotlin/collections/ArraysKt {
public static final fun scanIndexed ([Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
public static final fun scanReduce ([Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public static final fun scanReduceIndexed ([Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/util/List;
public static final fun shuffle ([B)V
public static final fun shuffle ([BLkotlin/random/Random;)V
public static final fun shuffle ([C)V
public static final fun shuffle ([CLkotlin/random/Random;)V
public static final fun shuffle ([D)V
public static final fun shuffle ([DLkotlin/random/Random;)V
public static final fun shuffle ([F)V
public static final fun shuffle ([FLkotlin/random/Random;)V
public static final fun shuffle ([I)V
public static final fun shuffle ([ILkotlin/random/Random;)V
public static final fun shuffle ([J)V
public static final fun shuffle ([JLkotlin/random/Random;)V
public static final fun shuffle ([Ljava/lang/Object;)V
public static final fun shuffle ([Ljava/lang/Object;Lkotlin/random/Random;)V
public static final fun shuffle ([S)V
public static final fun shuffle ([SLkotlin/random/Random;)V
public static final fun shuffle ([Z)V
public static final fun shuffle ([ZLkotlin/random/Random;)V
public static final fun single ([B)B
public static final fun single ([BLkotlin/jvm/functions/Function1;)B
public static final fun single ([C)C
@@ -2553,6 +2571,14 @@ public final class kotlin/collections/unsigned/UArraysKt {
public static final fun reversed-GBYM_sE ([B)Ljava/util/List;
public static final fun reversed-QwZRm1k ([J)Ljava/util/List;
public static final fun reversed-rL5Bavg ([S)Ljava/util/List;
public static final fun shuffle--ajY-9A ([I)V
public static final fun shuffle-2D5oskM ([ILkotlin/random/Random;)V
public static final fun shuffle-GBYM_sE ([B)V
public static final fun shuffle-JzugnMA ([JLkotlin/random/Random;)V
public static final fun shuffle-QwZRm1k ([J)V
public static final fun shuffle-oSF2wD8 ([BLkotlin/random/Random;)V
public static final fun shuffle-rL5Bavg ([S)V
public static final fun shuffle-s5X_as8 ([SLkotlin/random/Random;)V
public static final fun singleOrNull--ajY-9A ([I)Lkotlin/UInt;
public static final fun singleOrNull-GBYM_sE ([B)Lkotlin/UByte;
public static final fun singleOrNull-QwZRm1k ([J)Lkotlin/ULong;
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -457,4 +457,57 @@ object Ordering : TemplateGroupBase() {
}
}
val f_shuffle = fn("shuffle()") {
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
} builder {
since("1.4")
returns("Unit")
doc {
"""
Randomly shuffles elements in this ${f.collection} in-place.
"""
}
body {
"shuffle(Random)"
}
}
val f_shuffleRandom = fn("shuffle(random: Random)") {
include(Lists, InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
} builder {
since("1.4")
returns("Unit")
doc {
"""
Randomly shuffles elements in this ${f.collection} 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
"""
}
specialFor(Lists) {
since("1.3")
receiver("MutableList<T>")
}
body {
"""
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
"""
}
specialFor(Lists) {
body {
"""
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
this[j] = this.set(i, this[j])
}
"""
}
}
}
}