perm

package module
v0.0.0-...-918e356 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package perm permutes data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comb

type Comb struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"

	"github.com/bmizerany/perm"
)

func main() {
	animals := []string{"bird", "cat", "fish"}
	c := perm.NewComb(len(animals))
	for {
		fmt.Print("animals:")
		c.Visit(func(i int) {
			fmt.Print(" ", animals[i])
		})
		fmt.Println()

		if !c.Next() {
			break
		}
	}

}
Output:

animals:
animals: bird
animals: bird cat
animals: cat
animals: cat fish
animals: bird cat fish
animals: bird fish
animals: fish
Example (Nonempty)

The empty subset is always visited first, so if you need to visit all nonempty subsets, check c.Next() at the beginning of the loop instead of at the end.

package main

import (
	"fmt"

	"github.com/bmizerany/perm"
)

func main() {
	animals := []string{"bird", "cat", "fish"}
	c := perm.NewComb(len(animals))
	for c.Next() {
		fmt.Print("animals:")
		c.Visit(func(i int) {
			fmt.Print(" ", animals[i])
		})
		fmt.Println()
	}

}
Output:

animals: bird
animals: bird cat
animals: cat
animals: cat fish
animals: bird cat fish
animals: bird fish
animals: fish

func NewComb

func NewComb(n int) *Comb

func (*Comb) Next

func (c *Comb) Next() bool

func (*Comb) Reset

func (c *Comb) Reset(n int)

func (*Comb) Visit

func (c *Comb) Visit(f func(i int))

type CombPerm

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

CombPerm generates all permutations of all subsets of sequences of a given length.

The first subset is always the empty set. To skip the empty set, call Next before the first call to Visit.

Example
package main

import (
	"fmt"

	"github.com/bmizerany/perm"
)

func main() {
	animals := []string{"bird", "cat", "fish"}
	b := perm.NewCombPerm(len(animals))

	for {
		fmt.Print("animals:")
		b.Visit(func(i int) {
			fmt.Print(" ", animals[i])
		})
		fmt.Println()
		if !b.Next() {
			break
		}
	}

}
Output:

animals:
animals: bird
animals: bird cat
animals: cat bird
animals: cat
animals: cat fish
animals: fish cat
animals: bird cat fish
animals: bird fish cat
animals: cat bird fish
animals: cat fish bird
animals: fish bird cat
animals: fish cat bird
animals: bird fish
animals: fish bird
animals: fish

func NewCombPerm

func NewCombPerm(n int) *CombPerm

NewCombPerm returns a new CombPerm for length n.

func (*CombPerm) Next

func (b *CombPerm) Next() bool

Next advances b to the next permutation.

func (*CombPerm) Reset

func (b *CombPerm) Reset(n int)

func (*CombPerm) Visit

func (b *CombPerm) Visit(f func(i int))

Visit calls f for each index in the current permutation.

type Perm

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

Perm generates all permutations of a given length. The zero value is a Perm for length 0.

Example
package main

import (
	"fmt"

	"github.com/bmizerany/perm"
)

func main() {
	animals := []string{"bird", "cat", "fish"}
	p := perm.NewPerm(len(animals))

	for {
		fmt.Print("animals:")
		p.Visit(func(i int) {
			fmt.Print(" ", animals[i])
		})
		fmt.Println()
		if !p.Next() {
			break
		}
	}

}
Output:

animals: bird cat fish
animals: bird fish cat
animals: cat bird fish
animals: cat fish bird
animals: fish bird cat
animals: fish cat bird
Example (Multi)
package main

import (
	"fmt"

	"github.com/bmizerany/perm"
)

func main() {
	p := perm.NewPerm(3)

	a := []byte("abc")
	b := []string{"foo", "bar", "baz"}

	for {
		p.Visit(func(i int) {
			fmt.Printf("%c", a[i])
		})
		fmt.Print(":")
		p.Visit(func(i int) {
			fmt.Print(" ", b[i])
		})
		fmt.Println()
		if !p.Next() {
			break
		}
	}

}
Output:

abc: foo bar baz
acb: foo baz bar
bac: bar foo baz
bca: bar baz foo
cab: baz foo bar
cba: baz bar foo

func NewPerm

func NewPerm(n int) *Perm

New returns a new Perm that permutes data.

The function assumes data is sorted.

func (*Perm) Next

func (p *Perm) Next() bool

Next advances p to the next permutation.

func (*Perm) Reset

func (p *Perm) Reset(n int)

func (*Perm) Visit

func (p *Perm) Visit(f func(i int))

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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