it.unibo.alchemist.util.Iterables.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemist-implementationbase Show documentation
Show all versions of alchemist-implementationbase Show documentation
Abstract, incarnation independent implementations of the Alchemist's interfaces. Provides support for those who want to write incarnations.
/*
* Copyright (C) 2010-2022, Danilo Pianini and contributors
* listed, for each module, in the respective subproject's build.gradle.kts file.
*
* This file is part of Alchemist, and is distributed under the terms of the
* GNU General Public License, with a linking exception,
* as described in the file LICENSE in the Alchemist distribution's top directory.
*/
package it.unibo.alchemist.util
import org.apache.commons.math3.random.RandomGenerator
import java.util.Collections
/**
* Utilities that extend the functionality of [Iterable].
*/
object Iterables {
/**
* Fisher–Yates shuffle algorithm
* using a [RandomGenerator].
* More information [on Wikipedia](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle).
*
* @param randomGenerator
* the simulation {@link RandomGenerator}.
*/
fun Iterable.shuffled(randomGenerator: RandomGenerator): Iterable = toMutableList().apply {
for (i in size - 1 downTo 1) {
Collections.swap(this, i, randomGenerator.nextInt(i + 1))
}
}
/**
* Returns a random element of the Iterable using the provided [randomGenerator].
*/
fun Iterable.randomElement(randomGenerator: RandomGenerator): R =
with(toList()) { get(randomGenerator.nextInt(size)) }
}