WeightedSet

class WeightedSet<E> @JvmOverloads constructor(innerSet: MutableSet<WeightedSet.Element<E>> = mutableSetOf()) : AbstractMutableSet<WeightedSet.Element<E>> (source)

A set containing elements with associated weights. You can retrieve either a specified subset or a single element randomly, with the probability of each element being chosen proportional to its weight.

For example, if you have elements A, B, and C with weights 0.25, 0.25, and 0.5 respectively, element C will be chosen approximately 50% of the time, while A and B will each be chosen about 25% of the time.

Constructors

Link copied to clipboard
constructor(value: E, weight: Float)
constructor(vararg values: Pair<E, Float>)
constructor(innerSet: MutableSet<WeightedSet.Element<E>> = mutableSetOf())

Types

Link copied to clipboard
data class Element<E>(val element: E, val weight: Float) : Record

Properties

Link copied to clipboard
val elements: Set<E>
Link copied to clipboard
open override val size: Int

Functions

Link copied to clipboard
open override fun add(element: WeightedSet.Element<E>): Boolean
Link copied to clipboard
abstract override fun addAll(elements: Collection<WeightedSet.Element<E>>): Boolean
Link copied to clipboard
abstract override fun clear()
Link copied to clipboard
abstract operator override fun contains(element: WeightedSet.Element<E>): Boolean
Link copied to clipboard
abstract override fun containsAll(elements: Collection<WeightedSet.Element<E>>): Boolean
Link copied to clipboard
fun getRandom(random: Random = ThreadLocalRandom.current()): E

Returns a single random element from the set, with selection probability based on weights.

Link copied to clipboard
fun getRandomSubset(size: Int, random: Random = ThreadLocalRandom.current()): Set<E>

Returns a random subset of the specified size from the set, with selection probability based on weights. The subset will contain unique elements and always be of the requested size.

Link copied to clipboard
abstract override fun isEmpty(): Boolean
Link copied to clipboard
open operator override fun iterator(): MutableIterator<WeightedSet.Element<E>>
Link copied to clipboard
abstract override fun remove(element: WeightedSet.Element<E>): Boolean
Link copied to clipboard
abstract override fun removeAll(elements: Collection<WeightedSet.Element<E>>): Boolean
Link copied to clipboard
abstract override fun retainAll(elements: Collection<WeightedSet.Element<E>>): Boolean
Link copied to clipboard
open fun toArray(): Array<Any>
open fun <T : Any> toArray(p0: Array<T>): Array<T>