-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.go
57 lines (47 loc) · 1 KB
/
join.go
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
package sparseset
type JoinIterator[A, B any] struct {
get func() (int, *A, *B, bool)
}
func (i *JoinIterator[A, B]) Next() (int, *A, *B, bool) {
return i.get()
}
func Join[A, B any](set1 *Set[A], set2 *Set[B]) *JoinIterator[A, B] {
var get func() (int, *A, *B, bool)
if len(set1.dense) <= len(set2.dense) {
iterator := Iterate(set1)
get = func() (int, *A, *B, bool) {
for {
key, a, ok := iterator.Next()
if !ok {
return 0, nil, nil, false
}
b, ok := set2.Get(key)
if !ok {
continue
}
return key, a, b, true
}
}
} else {
iterator := Iterate(set2)
get = func() (int, *A, *B, bool) {
for {
key, b, ok := iterator.Next()
if !ok {
return 0, nil, nil, false
}
a, ok := set1.Get(key)
if !ok {
continue
}
return key, a, b, true
}
}
}
return &JoinIterator[A, B]{get}
}
func EmptyJoinIterator[A, B any]() *JoinIterator[A, B] {
return &JoinIterator[A, B]{func() (int, *A, *B, bool) {
return 0, nil, nil, false
}}
}