ArrayBufferの一部を、できればインプレースでシャッフルする必要があるので、コピーは必要ありません。たとえば、ArrayBufferに10個の要素があり、要素を3〜7個シャッフルしたい場合:
// Unshuffled ArrayBuffer of ints numbered 0-9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
// Region I want to shuffle is between the pipe symbols (3-7)
0, 1, 2 | 3, 4, 5, 6, 7 | 8, 9
// Example of how it might look after shuffling
0, 1, 2 | 6, 3, 5, 7, 4 | 8, 9
// Leaving us with a partially shuffled ArrayBuffer
0, 1, 2, 6, 3, 5, 7, 4, 8, 9
以下に示すようなものを作成しましたが、コピーとループの反復が数回必要です。これを行うにはもっと効率的な方法があるはずです。
def shufflePart(startIndex: Int, endIndex: Int) {
val part: ArrayBuffer[Int] = ArrayBuffer[Int]()
for (i <- startIndex to endIndex ) {
part += this.children(i)
}
// Shuffle the part of the array we copied
val shuffled = this.random.shuffle(part)
var count: Int = 0
// Overwrite the part of the array we chose with the shuffled version of it
for (i <- startIndex to endIndex ) {
this.children(i) = shuffled(count)
count += 1
}
}
GoogleでArrayBufferを部分的にシャッフルすることについては何も見つかりませんでした。自分でメソッドを書く必要があると思いますが、そうすることでコピーを防ぎたいと思います。