-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patholdzip_test.go
More file actions
90 lines (78 loc) · 1.53 KB
/
Copy patholdzip_test.go
File metadata and controls
90 lines (78 loc) · 1.53 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
package xiter
import (
"iter"
"slices"
"testing"
)
func BenchmarkOldZip(b *testing.B) {
slice1 := []int{1, 2, 3, 4, 5}
slice2 := []int{2, 3, 4, 5, 6}
for b.Loop() {
s1 := slices.Values(slice1)
s2 := slices.Values(slice2)
seq := oldZip(s1, s2)
seq(func(v Zipped[int, int]) bool {
return true
})
}
}
func oldZipSend[T any](done <-chan struct{}, c chan<- T) func(v T) bool {
return func(v T) bool {
select {
case <-done:
return false
case c <- v:
return true
}
}
}
func oldZip[T1, T2 any](seq1 iter.Seq[T1], seq2 iter.Seq[T2]) iter.Seq[Zipped[T1, T2]] {
done := make(chan struct{})
c1 := make(chan T1)
go func() {
defer close(c1)
seq1(oldZipSend(done, c1))
}()
c2 := make(chan T2)
go func() {
defer close(c2)
seq2(oldZipSend(done, c2))
}()
return func(yield func(Zipped[T1, T2]) bool) {
defer close(done)
var c1hold chan T1
var c2hold chan T2
var val Zipped[T1, T2]
for {
select {
case v, ok := <-c1:
if !ok {
c1 = nil
goto send
}
c1, c1hold = nil, c1
val.V1 = v
val.OK1 = true
case v, ok := <-c2:
if !ok {
c2 = nil
goto send
}
c2, c2hold = nil, c2
val.V2 = v
val.OK2 = true
}
send: // Dear lord, what is even going on here?
if (val.OK1 || (c1 == c1hold)) && (val.OK2 || (c2 == c2hold)) && !(c1 == c1hold && c2 == c2hold) {
if !yield(val) {
return
}
c1, c1hold, c2, c2hold = c1hold, nil, c2hold, nil
val = Zipped[T1, T2]{}
}
if (c1 == c1hold) && (c2 == c2hold) {
return
}
}
}
}