-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathintersectby.go
106 lines (100 loc) · 3.66 KB
/
intersectby.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
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
package go2linq
import (
"iter"
"slices"
"sort"
"sync"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [IntersectBy] produces the set intersection of two sequences according to
// a specified key selector function and using [generichelper.DeepEqual] as key equaler.
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [IntersectBy]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.intersectby
func IntersectBy[Source, Key any](first iter.Seq[Source], second iter.Seq[Key], keySelector func(Source) Key) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
r, err := IntersectByEq(first, second, keySelector, generichelper.DeepEqual[Key])
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
func seqIntersectByEq[Source, Key any](first iter.Seq[Source], second iter.Seq[Key],
keySelector func(Source) Key, sourceEqual func(Source, Source) bool,
keyEqual func(Key, Key) bool, keyCompare func(Key, Key) int) func(func(Source) bool) {
return func(yield func(Source) bool) {
d1, _ := DistinctEq(first, sourceEqual)
var once sync.Once
var sl2 []Key
for s := range d1 {
once.Do(func() {
if keyCompare == nil {
d2, _ := DistinctEq(second, keyEqual)
sl2 = slices.Collect(d2)
} else {
d2, _ := DistinctCmp(second, keyCompare)
sl2 = slices.Collect(d2)
sort.Slice(sl2, func(i, j int) bool { return keyCompare(sl2[i], sl2[j]) < 0 })
}
})
k := keySelector(s)
if keyCompare == nil {
if elInElelEq(k, sl2, keyEqual) {
if !yield(s) {
return
}
}
} else {
if elInElelCmp(k, sl2, keyCompare) {
if !yield(s) {
return
}
}
}
}
}
}
// [IntersectByEq] produces the set intersection of two sequences according to
// a specified key selector function and using a specified key equaler.
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [IntersectByEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.intersectby
func IntersectByEq[Source, Key any](first iter.Seq[Source], second iter.Seq[Key],
keySelector func(Source) Key, keyEqual func(Key, Key) bool) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if keyEqual == nil {
return nil, errorhelper.CallerError(ErrNilEqual)
}
return seqIntersectByEq(first, second, keySelector, generichelper.DeepEqual[Source], keyEqual, nil),
nil
}
// [IntersectByCmp] produces the set intersection of two sequences according to
// a specified key selector function and using a specified key comparer. (See [DistinctCmp].)
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [IntersectByCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.intersectby
func IntersectByCmp[Source, Key any](first iter.Seq[Source], second iter.Seq[Key],
keySelector func(Source) Key, compare func(Key, Key) int) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if compare == nil {
return nil, errorhelper.CallerError(ErrNilCompare)
}
return seqIntersectByEq(first, second, keySelector, generichelper.DeepEqual[Source], nil, compare),
nil
}