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
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 ¶
NewCombPerm returns a new CombPerm for length n.
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
Click to show internal directories.
Click to hide internal directories.