-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproduct.go
More file actions
201 lines (182 loc) · 3.95 KB
/
Copy pathproduct.go
File metadata and controls
201 lines (182 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package iterium
import (
"iter"
"math"
)
// ProductCount calculates the number of Cartesian products with repeat.
func ProductCount(countOfSymbols, repeat int) int64 {
count, ok := ProductCountOK(countOfSymbols, repeat)
if !ok {
return math.MaxInt64
}
return count
}
// ProductCountOK calculates Cartesian product count and reports overflow.
func ProductCountOK(countOfSymbols, repeat int) (int64, bool) {
if countOfSymbols < 0 || repeat < 0 {
return 0, false
}
if repeat == 0 {
return 1, true
}
result := int64(1)
base := int64(countOfSymbols)
for i := 0; i < repeat; i++ {
if base != 0 && result > math.MaxInt64/base {
return math.MaxInt64, false
}
result *= base
}
return result, true
}
// Product returns a reusable Go iterator sequence for Cartesian products.
// Each yielded slice is safe to keep.
func Product[T any](symbols []T, repeat int) iter.Seq[[]T] {
return func(yield func([]T) bool) {
ProductInto(symbols, repeat, func(value []T) bool {
out := make([]T, repeat)
copy(out, value)
return yield(out)
})
}
}
// ProductInto generates Cartesian products using a reused result buffer.
// The yielded slice is only valid until the next yield call.
func ProductInto[T any](symbols []T, repeat int, yield func([]T) bool) {
n := len(symbols)
if repeat < 0 {
return
}
if repeat == 0 {
yield([]T{})
return
}
if n == 0 {
return
}
indices := make([]int, repeat)
result := make([]T, repeat)
first := symbols[0]
for i := range result {
result[i] = first
}
for {
if !yield(result) {
return
}
for i := repeat - 1; i >= 0; i-- {
indices[i]++
if indices[i] < n {
result[i] = symbols[indices[i]]
for j := i + 1; j < repeat; j++ {
indices[j] = 0
result[j] = first
}
break
}
if i == 0 {
return
}
indices[i] = 0
}
}
}
// ProductBytesInto is a specialized zero-allocation product generator for byte alphabets.
// The yielded slice is reused and must be copied by callers that keep it.
func ProductBytesInto(symbols []byte, repeat int, yield func([]byte) bool) {
n := len(symbols)
if repeat < 0 {
return
}
if repeat == 0 {
yield([]byte{})
return
}
if n == 0 {
return
}
indices := make([]int, repeat)
result := make([]byte, repeat)
first := symbols[0]
for i := range result {
result[i] = first
}
for {
if !yield(result) {
return
}
for i := repeat - 1; i >= 0; i-- {
indices[i]++
if indices[i] < n {
result[i] = symbols[indices[i]]
for j := i + 1; j < repeat; j++ {
indices[j] = 0
result[j] = first
}
break
}
if i == 0 {
return
}
indices[i] = 0
}
}
}
// ProductStringInto is a specialized product generator for string alphabets.
// The yielded byte slice is reused and must be copied by callers that keep it.
func ProductStringInto(symbols string, repeat int, yield func([]byte) bool) {
n := len(symbols)
if repeat < 0 {
return
}
if repeat == 0 {
yield([]byte{})
return
}
if n == 0 {
return
}
indices := make([]int, repeat)
result := make([]byte, repeat)
first := symbols[0]
for i := range result {
result[i] = first
}
for {
if !yield(result) {
return
}
for i := repeat - 1; i >= 0; i-- {
indices[i]++
if indices[i] < n {
result[i] = symbols[indices[i]]
for j := i + 1; j < repeat; j++ {
indices[j] = 0
result[j] = first
}
break
}
if i == 0 {
return
}
indices[i] = 0
}
}
}
// ProductRunesInto is a specialized product generator for rune alphabets.
// The yielded slice is reused and must be copied by callers that keep it.
func ProductRunesInto(symbols []rune, repeat int, yield func([]rune) bool) {
ProductInto(symbols, repeat, yield)
}
// Product2 returns a reusable Go iterator sequence for a two-slice Cartesian product.
func Product2[A, B any](first []A, second []B) iter.Seq2[A, B] {
return func(yield func(A, B) bool) {
for _, a := range first {
for _, b := range second {
if !yield(a, b) {
return
}
}
}
}
}