roundrobinrandom

package
v0.0.0-...-3f013c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package roundrobinrandom implements a randomized round-based ordering type.

Each element in an Order is visited in random sequence, once per round (possibly excluding the round it is added to the Order). Therefore, in an Order of n elements, successive visits to the same element will occur at intervals of at most 2n-1 calls to Next.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Order

type Order[T comparable] struct {
	// contains filtered or unexported fields
}

An Order holds values to return in a randomized round-based sequence such that each value is returned once per round. The sequence is shuffled after each round. The zero value of type Order is an empty Order ready for use.

func (*Order[T]) Add

func (o *Order[T]) Add(t T)

Add inserts t into a random position in the Order. Depending on where it is inserted, t may or may not be returned in the current round.

func (*Order[T]) IndependentSample

func (o *Order[T]) IndependentSample(n int, exclude T) []T

IndependentSample returns a slice of unique elements besides exclude, chosen at random. If there are at least n such elements, IndependentSample returns n of them, or else all of them.

func (*Order[T]) Next

func (o *Order[T]) Next() T

Next returns the next value in the Order, shuffling first if necessary. If the Order is empty, it returns the zero value of type T.

func (*Order[T]) Remove

func (o *Order[T]) Remove(t T)

Remove removes the first instance of t from the Order, if any.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL