Documentation ¶
Index ¶
- func Binom(k, n int) int
- func CombinationCount(m, n int) int
- func Combinations[T any](m int, elems []T) ([][]T, error)
- func Fac(n int) int
- func IntPow(n, m int) int
- func MultiPermutations[T any](elems []T, reps []int) ([][]T, error)
- func MultiPermutationsCount(reps []int) int
- func PermutationCount(n int) int
- func Permutations[T any](elems []T) ([][]T, error)
- func VariationCount(k, n int) int
- func Variations[T any](k int, elems []T) ([][]T, error)
- type CombinationGenerator
- func (gen *CombinationGenerator[T]) Current() []T
- func (gen *CombinationGenerator[T]) CurrentCopy() []T
- func (gen *CombinationGenerator[T]) Init(m int, elems []T) error
- func (gen *CombinationGenerator[T]) Next() bool
- func (gen *CombinationGenerator[T]) Reset()
- func (gen *CombinationGenerator[T]) SetDest(dest []T) error
- type Generator
- type MultiPermutationGenerator
- func (gen *MultiPermutationGenerator[T]) Current() []T
- func (gen *MultiPermutationGenerator[T]) CurrentCopy() []T
- func (gen *MultiPermutationGenerator[T]) Init(elems []T, reps []int) error
- func (gen *MultiPermutationGenerator[T]) Next() bool
- func (gen *MultiPermutationGenerator[T]) Reset()
- func (gen *MultiPermutationGenerator[T]) SetDest(dest []T) error
- type PermutationGenerator
- func (gen *PermutationGenerator[T]) Current() []T
- func (gen *PermutationGenerator[T]) CurrentCopy() []T
- func (gen *PermutationGenerator[T]) Init(elems []T) error
- func (gen *PermutationGenerator[T]) Next() bool
- func (gen *PermutationGenerator[T]) Reset()
- func (gen *PermutationGenerator[T]) SetDest(dest []T) error
- type VariationGenerator
- func (gen *VariationGenerator[T]) Current() []T
- func (gen *VariationGenerator[T]) CurrentCopy() []T
- func (gen *VariationGenerator[T]) Init(k int, elems []T) error
- func (gen *VariationGenerator[T]) Next() bool
- func (gen *VariationGenerator[T]) Reset()
- func (gen *VariationGenerator[T]) SetDest(dest []T) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CombinationCount ¶
func Combinations ¶
Combinations by the Chase's Twiddle algorithm (1970), based on implementation in C by Matthew Belmonte in 1996.
Returns error if the input slice is empty or nil, or if m is less than 1 or bigger than len(elems).
func MultiPermutations ¶
This function generates permutations of a multiset by following the algorithm described below. It accepts slices
- elems: set of elements to make permutations from
- reps: number of repetitions per element, must be 1 or higher.
Number of items in both slices must match, as reps correspond to the elems.
Algorithm 1 Visits the permutations of multiset E. The permutations are stored in a singly-linked list pointed to by head pointer h. Each node in the linked list has a value field v and a next field n. The init(E) call creates a singly-linked list storing the elements of E in non-increasing order with h, i, and j pointing to its first, second-last, and last nodes, respectively. The null pointer is given by φ. Note: If E is empty, then init(E) should exit. Also, if E contains only one element, then init(E) does not need to provide a value for i.
[h, i, j] ← init(E) visit(h) while j.n ≠ φ orj.v < h.v do if j.n ≠ φ and i.v ≥ j.n.v then s←j else s←i end if t ← s.n s.n ← t.n t.n ← h if t.v < h.v then i←t end if j←i.n h←t visit(h) end while
Found in "Loopless Generation of Multiset Permutations using a Constant Number of Variables by Prefix Shifts." Aaron Williams, 2009
For implementations in other languages, check out multipermute.
func MultiPermutationsCount ¶
func PermutationCount ¶
func Permutations ¶
Permutations without repetition by using non-recursive Heap's algorithm. Returns an error if elems is empty or nil.
func VariationCount ¶
func Variations ¶
Variations with repetitions. Returns an error when k <= 0 or when elems is nil or empty.
Types ¶
type CombinationGenerator ¶
type CombinationGenerator[T any] struct { // contains filtered or unexported fields }
CombinationGenerator implements a Generator interface for generating combinations of size m out of elements in elems slice on every invocation of the CombinationGenerator.Next method. For algorithm details see Combinations.
func NewCombinationGenerator ¶
func NewCombinationGenerator[T any](m int, elems []T) (*CombinationGenerator[T], error)
NewCombinationGenerator creates and initializes a new CombinationGenerator. Arguments and returned errors are the same ones from the CombinationGenerator.Init method.
func (*CombinationGenerator[T]) Current ¶
func (gen *CombinationGenerator[T]) Current() []T
Current returns the internal slice that holds the current combination. If you need to modify the returned slice, use CombinationGenerator.CurrentCopy instead.
func (*CombinationGenerator[T]) CurrentCopy ¶
func (gen *CombinationGenerator[T]) CurrentCopy() []T
CurrentCopy returns a copy of the internal slice that holds the current combination. If you don't need to modify the returned slice, use CombinationGenerator.Current to avoid allocation.
func (*CombinationGenerator[T]) Init ¶
func (gen *CombinationGenerator[T]) Init(m int, elems []T) error
Init initializes a generator of combinations of size m out of elements in the elems slice.
func (*CombinationGenerator[T]) Next ¶
func (gen *CombinationGenerator[T]) Next() bool
Next produces a new combination in the generator. If it returns false, there are no more combinations available.
func (*CombinationGenerator[T]) Reset ¶
func (gen *CombinationGenerator[T]) Reset()
Reset resets the generator to the beginning of the sequence.
func (*CombinationGenerator[T]) SetDest ¶
func (gen *CombinationGenerator[T]) SetDest(dest []T) error
SetDest sets a destination slice that will receive the results. Returns an error if there's not enough capacity in the slice.
After the destination slice is set, subsequent calls to CombinationGenerator.Current will return the provided slice.
type MultiPermutationGenerator ¶
type MultiPermutationGenerator[T any] struct { // contains filtered or unexported fields }
MultiPermutationGenerator implements a Generator interface for generating multiset permutations by an algorithm described in MultiPermutations.
func NewMultiPermutationGenerator ¶
func NewMultiPermutationGenerator[T any](elems []T, reps []int) (*MultiPermutationGenerator[T], error)
NewMultiPermutationGenerator creates and initializes a new MultiPermutationGenerator. Arguments and returned errors are the same ones from the MultiPermutationGenerator.Init method.
func (*MultiPermutationGenerator[T]) Current ¶
func (gen *MultiPermutationGenerator[T]) Current() []T
Current returns the internal slice that holds the current permutation. If you need to modify the returned slice, use MultiPermutationGenerator.CurrentCopy instead.
func (*MultiPermutationGenerator[T]) CurrentCopy ¶
func (gen *MultiPermutationGenerator[T]) CurrentCopy() []T
CurrentCopy returns a copy of the internal slice that holds the current permutation. If you don't need to modify the returned slice, use MultiPermutationGenerator.Current to avoid allocation.
func (*MultiPermutationGenerator[T]) Init ¶
func (gen *MultiPermutationGenerator[T]) Init(elems []T, reps []int) error
Init initializes a generator of multiset permutations.
It accepts slices
- elems: set of elements to make permutations from
- reps: number of repetitions per element, rep must be 1 or higher.
Number of items in both slices must match, as reps correspond to the elems.
func (*MultiPermutationGenerator[T]) Next ¶
func (gen *MultiPermutationGenerator[T]) Next() bool
Next produces a new permutation in the generator. If it returns false, there are no more permutations available.
func (*MultiPermutationGenerator[T]) Reset ¶
func (gen *MultiPermutationGenerator[T]) Reset()
Reset resets the generator to the beginning of the sequence.
func (*MultiPermutationGenerator[T]) SetDest ¶
func (gen *MultiPermutationGenerator[T]) SetDest(dest []T) error
SetDest sets a destination slice that will receive the results. Returns an error if there's not enough capacity in the slice.
After the destination slice is set, subsequent calls to MultiPermutationGenerator.Current will return the provided slice.
type PermutationGenerator ¶
type PermutationGenerator[T any] struct { // contains filtered or unexported fields }
PermutationGenerator implements a Generator interface for generating permutations by an algorithm described in Permutations.
func NewPermutationGenerator ¶
func NewPermutationGenerator[T any](elems []T) (*PermutationGenerator[T], error)
NewPermutationGenerator creates and initializes a new PermutationGenerator. Arguments and returned errors are the same ones from the PermutationGenerator.Init method.
func (*PermutationGenerator[T]) Current ¶
func (gen *PermutationGenerator[T]) Current() []T
Current returns the internal slice that holds the current permutation. If you need to modify the returned slice, use PermutationGenerator.CurrentCopy instead.
func (*PermutationGenerator[T]) CurrentCopy ¶
func (gen *PermutationGenerator[T]) CurrentCopy() []T
CurrentCopy returns a copy of the internal slice that holds the current permutation. If you don't need to modify the returned slice, use PermutationGenerator.Current to avoid allocation.
func (*PermutationGenerator[T]) Init ¶
func (gen *PermutationGenerator[T]) Init(elems []T) error
Init initializes a generator of permutations. Returns an error if input slice is nil or empty.
func (*PermutationGenerator[T]) Next ¶
func (gen *PermutationGenerator[T]) Next() bool
Next produces a new permutation in the generator. If it returns false, there are no more permutations available.
func (*PermutationGenerator[T]) Reset ¶
func (gen *PermutationGenerator[T]) Reset()
Reset resets the generator to the beginning of the sequence.
func (*PermutationGenerator[T]) SetDest ¶
func (gen *PermutationGenerator[T]) SetDest(dest []T) error
SetDest sets a destination slice that will receive the results. Returns an error if there's not enough capacity in the slice.
After the destination slice is set, subsequent calls to PermutationGenerator.Current will return the provided slice.
type VariationGenerator ¶
type VariationGenerator[T any] struct { // contains filtered or unexported fields }
VariationGenerator implements a Generator interface for generating variations.
func NewVariationGenerator ¶
func NewVariationGenerator[T any](k int, elems []T) (*VariationGenerator[T], error)
NewVariationGenerator creates and initializes a new VariationGenerator. Arguments and returned errors are the same ones from the VariationGenerator.Init method.
func (*VariationGenerator[T]) Current ¶
func (gen *VariationGenerator[T]) Current() []T
Current returns the internal slice that holds the current variation. If you need to modify the returned slice, use VariationGenerator.CurrentCopy instead.
func (*VariationGenerator[T]) CurrentCopy ¶
func (gen *VariationGenerator[T]) CurrentCopy() []T
CurrentCopy returns a copy of the internal slice that holds the current variation. If you don't need to modify the returned slice, use VariationGenerator.Current to avoid allocation.
func (*VariationGenerator[T]) Init ¶
func (gen *VariationGenerator[T]) Init(k int, elems []T) error
Init initializes a generator for variations of size k out of elements in the elems slice. Returns an error if input slice is nil or empty, or if k < 1.
func (*VariationGenerator[T]) Next ¶
func (gen *VariationGenerator[T]) Next() bool
Next produces a new variation in the generator. If it returns false, there are no more variations available.
func (*VariationGenerator[T]) Reset ¶
func (gen *VariationGenerator[T]) Reset()
Reset resets the generator to the beginning of the sequence.
func (*VariationGenerator[T]) SetDest ¶
func (gen *VariationGenerator[T]) SetDest(dest []T) error
SetDest sets a destination slice that will receive the results. Returns an error if there's not enough capacity in the slice.
After the destination slice is set, subsequent calls to VariationGenerator.Current will return the provided slice.