permute: github.com/cespare/permute Index | Examples | Files

package permute

import "github.com/cespare/permute"

Package permute implements a generic method for in-place generation of all permutations for ordered collections.

Code:

package main

import (
    "fmt"

    "github.com/cespare/permute"
)

type ByteSlice []byte

func (s ByteSlice) Len() int      { return len(s) }
func (s ByteSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func main() {
    b := []byte{'A', 'B', 'C'}
    p := permute.New(ByteSlice(b))
    for p.Permute() {
        fmt.Println(string(b))
    }
}

Code:

s := []int{5, 7}
p := permute.Ints(s)
for p.Permute() {
    fmt.Println(s)
}

Output:

[5 7]
[7 5]

Index

Examples

Package Files

permute.go

type Interface Uses

type Interface interface {
    // Len is the number of elements in the collection.
    Len() int
    // Swap swaps the elements with indexes i and j.
    Swap(i, j int)
}

Interface is satisfied by types (usually ordered collections) which can have all permutations generated by a Permuter.

type Permuter Uses

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

A Permuter holds state about an ongoing iteration of permutations.

func Ints Uses

func Ints(s []int) *Permuter

Ints is a convenience function for generating permutations of []ints.

func New Uses

func New(v Interface) *Permuter

New gives a Permuter to generate all permutations of the elements of v.

func Strings Uses

func Strings(s []string) *Permuter

Strings is a convenience function for generating permutations of []strings.

func (*Permuter) Permute Uses

func (p *Permuter) Permute() bool

Permute generates the next permutation of the contained collection in-place. If it returns false, the iteration is finished.

Updated 2018-05-15. Refresh now. Tools for package owners.