permute: Index | Examples | Files

package permute

import ""

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


package main

import (


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() {


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


[5 7]
[7 5]



Package Files


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.