Skip to content

Commit 9888395

Browse files
committed
rand cuber
make random cuber generate cubes with unique variables previously, literals could repeat.
1 parent b0d0306 commit 9888395

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

gen/cubes.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
package gen
55

66
import (
7-
"github.com/irifrance/gini/z"
87
"math/rand"
8+
9+
"github.com/irifrance/gini/z"
910
)
1011

1112
type RandCuber interface {
@@ -16,13 +17,15 @@ func NewRandCuber(maxSize int, maxVar z.Var) RandCuber {
1617
return &cubes{
1718
minSize: 1,
1819
maxSize: maxSize,
19-
maxVar: maxVar}
20+
maxVar: maxVar,
21+
uniq: make(map[z.Var]struct{}, maxSize)}
2022
}
2123

2224
type cubes struct {
2325
minSize int
2426
maxSize int
2527
maxVar z.Var
28+
uniq map[z.Var]struct{}
2629
}
2730

2831
func (c *cubes) SetMinSize(s int) {
@@ -36,9 +39,16 @@ func (c *cubes) SetMaxSize(s int) {
3639
func (c *cubes) RandCube(dst []z.Lit) []z.Lit {
3740
dst = dst[:0]
3841
sz := c.minSize + rand.Intn(c.maxSize-c.minSize+1)
42+
for k := range c.uniq {
43+
delete(c.uniq, k)
44+
}
3945
for i := 0; i < sz; i++ {
4046
m := z.Lit((rand.Intn(int(c.maxVar))+1)*2 + rand.Intn(2))
47+
if _, ok := c.uniq[m.Var()]; ok {
48+
continue
49+
}
4150
dst = append(dst, m)
51+
c.uniq[m.Var()] = struct{}{}
4252
}
4353
return dst
4454
}

0 commit comments

Comments
 (0)