mirror of
https://github.com/gonum/gonum.git
synced 2025-10-07 08:01:20 +08:00
stat/combin: allow user allocation for Cartesian product
This commit is contained in:
@@ -341,8 +341,8 @@ func (g *CartesianGenerator) Next() bool {
|
|||||||
|
|
||||||
// Product generates one product of the cartesian set according to the current index which is increased by Next().
|
// Product generates one product of the cartesian set according to the current index which is increased by Next().
|
||||||
// Next needs to be called at least one time before this method, otherwise it will panic.
|
// Next needs to be called at least one time before this method, otherwise it will panic.
|
||||||
func (g *CartesianGenerator) Product() []int {
|
func (g *CartesianGenerator) Product(dst []int) []int {
|
||||||
return SubFor(nil, g.idx, g.lens)
|
return SubFor(dst, g.idx, g.lens)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IdxFor converts a multi-dimensional index into a linear index for a
|
// IdxFor converts a multi-dimensional index into a linear index for a
|
||||||
|
@@ -318,7 +318,7 @@ func TestCartesianGenerator(t *testing.T) {
|
|||||||
gen := NewCartesianGenerator([]int{1, 2, 3})
|
gen := NewCartesianGenerator([]int{1, 2, 3})
|
||||||
iterations := 0
|
iterations := 0
|
||||||
for gen.Next() {
|
for gen.Next() {
|
||||||
got := gen.Product()
|
got := gen.Product(nil)
|
||||||
if !reflect.DeepEqual(got, want[iterations]) {
|
if !reflect.DeepEqual(got, want[iterations]) {
|
||||||
t.Errorf("Cartesian product does not match. want: %v got: %v", want[iterations], got)
|
t.Errorf("Cartesian product does not match. want: %v got: %v", want[iterations], got)
|
||||||
}
|
}
|
||||||
|
@@ -39,7 +39,7 @@ func ExampleCartesianGenerator() {
|
|||||||
// Now loop over all products.
|
// Now loop over all products.
|
||||||
var i int
|
var i int
|
||||||
for gen.Next() {
|
for gen.Next() {
|
||||||
fmt.Println(i, gen.Product())
|
fmt.Println(i, gen.Product(nil))
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user