All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.unibo.alchemist.util.Iterables.kt Maven / Gradle / Ivy

Go to download

Abstract, incarnation independent implementations of the Alchemist's interfaces. Provides support for those who want to write incarnations.

There is a newer version: 35.0.1
Show newest version
/*
 * 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)) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy