combinatoric

package module
v0.0.0-...-16ea832 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2016 License: MIT Imports: 2 Imported by: 1

README

Combinatoric Build Status Coverage Status Go Report Card

Combinatoric is a simple Go port of the "combinatoric" parts of Python's itertools--specifically, combinations, permutations, and product.

See godocs for more.

Installation

$ go get https://github.com/ecooper/combinatoric

Quickstart

package main

import (
    "fmt"
    "github.com/ecooper/combinatoric"
)

func main() {
    src := []interface{}{"A", "B", "C", "D"}

    // Create a new CombinationIterator of 2 elements using src
    iter, _ := combinatoric.Combinations(src, 2)

    // Print the length of the iterator
    fmt.Printf("Expecting %d combinations:\n", iter.Len())

    // Set c to the next combination until Next returns a nil slice.
    for c := iter.First(); c != nil; c = iter.Next() {
        fmt.Printf("%s\n", c)
    }

    // Restore the CombinationsIterator to its original state
    iter.Reset()
}

Usage

See godocs for more documentation and usage examples.

Documentation

Overview

Package combinatoric is a simple Go port of the "combinatoric" parts of Python's itertools--specifically, combinations, permutations, and product.

None of the iterators are threadsafe. Implement mutexes as required. Additionally, it should be assumed the return values for First and Next always share the same memory address. If return values must be persisted between iterations, copy them into another slice.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CombinationIterator

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

CombinationIterator implements an Iterator to generate unique combinations of a given slice.

CombinationIterator is not threadsafe, and should always be initialized through Combinations. Return values from First and Next share the same memory address. Use copy if the value must persist between iterations.

func Combinations

func Combinations(pool []interface{}, r int) (*CombinationIterator, error)

Combinations creates a new CombinationIterator for a given slice and desired output size.

func (*CombinationIterator) First

func (iter *CombinationIterator) First() []interface{}

First resets the iterator to its starting state and returns the first combination.

func (*CombinationIterator) HasNext

func (iter *CombinationIterator) HasNext() bool

HasNext returns true if the iterator is not yet exhausted.

func (*CombinationIterator) Len

func (iter *CombinationIterator) Len() uint64

Len returns the maximum iterations.

func (*CombinationIterator) Next

func (iter *CombinationIterator) Next() []interface{}

Next returns the next value in the iterator or returns nil.

func (*CombinationIterator) Reset

func (iter *CombinationIterator) Reset()

Reset returns the iterator to its starting state.

type Iterator

type Iterator interface {
	First() []interface{}
	Next() []interface{}
	HasNext() bool
	Len() uint64
	Reset()
}

Iterator is the interface that wraps a basic iterator.

Iterators are expected to track state and be able to calculate the number of iterations for a given implementation.

First and Next should return nil when a result slice is not available.

type PermutationIterator

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

PermutationIterator implements an Iterator to generate all permutations of a given slice.

PermutationIterator is not threadsafe, and should always be initialized through Permutations. Return values from First and Next share the same memory address. Use copy if the value must persist between iterations.

func Permutations

func Permutations(pool []interface{}, r int) (*PermutationIterator, error)

Permutations creates a new PermutationIterator for a given slice and desired output size.

func (*PermutationIterator) First

func (iter *PermutationIterator) First() []interface{}

First resets the iterator to its starting state and returns the first permutation.

func (*PermutationIterator) HasNext

func (iter *PermutationIterator) HasNext() bool

HasNext returns true if the iterator is not yet exhausted.

func (*PermutationIterator) Len

func (iter *PermutationIterator) Len() uint64

Len returns the maximum iterations.

func (*PermutationIterator) Next

func (iter *PermutationIterator) Next() []interface{}

Next returns the next value in the iterator or returns nil.

func (*PermutationIterator) Reset

func (iter *PermutationIterator) Reset()

Reset returns the iterator to its starting state.

type ProductIterator

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

ProductIterator implements an Iterator to generate the Cartesian product of a slice of slices.

ProductIterator is not threadsafe, and should always be initialized through Products. Return values from First and Next share the same memory address. Use copy if the value must persist between iterations.

func Product

func Product(pools [][]interface{}) (*ProductIterator, error)

Product creates a new ProductIterator for a given slice and desired output size.

func (*ProductIterator) First

func (iter *ProductIterator) First() []interface{}

First resets the iterator to its starting state and returns the first product.

func (*ProductIterator) HasNext

func (iter *ProductIterator) HasNext() bool

HasNext returns true if the iterator is not yet exhausted.

func (*ProductIterator) Len

func (iter *ProductIterator) Len() uint64

Len returns the maximum iterations.

func (*ProductIterator) Next

func (iter *ProductIterator) Next() []interface{}

Next returns the next value in the iterator or returns nil.

func (*ProductIterator) Reset

func (iter *ProductIterator) Reset()

Reset returns the iterator to its starting state.

Jump to

Keyboard shortcuts

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