permute

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2021 License: MIT Imports: 0 Imported by: 1

README

permute

Go Reference

Permute is a Go package for generating permutations.

Documentation

Overview

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

Example (CustomType)
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))
	}
}
Output:

ABC
BAC
CAB
ACB
BCA
CBA
Example (Ints)
package main

import (
	"fmt"

	"github.com/cespare/permute"
)

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

[5 7]
[7 5]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

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

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

A Permuter holds state about an ongoing iteration of permutations.

func Ints

func Ints(s []int) *Permuter

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

func New

func New(v Interface) *Permuter

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

func Strings

func Strings(s []string) *Permuter

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

func (*Permuter) Permute

func (p *Permuter) Permute() bool

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

Jump to

Keyboard shortcuts

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