shuffle

package
v0.0.0-...-2597ab7 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package shuffle provides mechanisms for permuting data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FisherYates

type FisherYates struct {
	// contains filtered or unexported fields
}

FisherYates implements a Fisher–Yates shuffle with an extendable pool size.

func NewFisherYates

func NewFisherYates(n uint32) *FisherYates

NewFisherYates returns a new FisherYates with a specified pool size.

func (*FisherYates) Grow

func (f *FisherYates) Grow(delta uint32)

Grow increases the size of the pool of values from which Permute can choose. It doesn't change already-permuted values in any way, and simply appends the next n indices to those available to future calls to Permute.

func (*FisherYates) GrowTo

func (f *FisherYates) GrowTo(n uint32)

GrowTo is equivalent to f.Grow(max(0, f.Size()-n)); i.e. it grows the size of the pool to n, unless it is already greater than or equal to this size.

func (*FisherYates) Permutation

func (f *FisherYates) Permutation() []int

Permutation returns the already-permuted values.

func (*FisherYates) Permute

func (f *FisherYates) Permute(n uint32, rng Rand) ([]int, error)

Permute chooses up to n values from the pool, limited by the number of already-permuted values.

func (*FisherYates) PermuteUpTo

func (f *FisherYates) PermuteUpTo(n uint32, rng Rand) []int

PermuteUpTo is equivalent to Permute(min(n, f.Remaining())).

func (*FisherYates) Remaining

func (f *FisherYates) Remaining() uint32

Remaining returns the number of unshuffled values in the pool; i.e. the difference between Size() and the sum of all n passed to Permute().

func (*FisherYates) Size

func (f *FisherYates) Size() int

Size returns the size of the pool from which Permute() chooses the next indices. It is the sum of the values passed to NewFisherYates() and all calls to Grow().

type Rand

type Rand interface {
	// Intn returns an integer in [0,n).
	Intn(n int) int
}

A Rand can return a random integer from a range. It is typically provided by a math/rand.Rand pointer, but is defined as an interface to allow for alternatives such as piping crypto/rand into a custom type.

Jump to

Keyboard shortcuts

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