-
Notifications
You must be signed in to change notification settings - Fork 6
/
intersectby.go
135 lines (124 loc) · 5.21 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
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
package go2linq
import (
"sort"
"sync"
"github.com/solsw/collate"
"github.com/solsw/errorhelper"
)
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.intersectby
// [IntersectBy] produces the set intersection of two sequences according to
// a specified key selector function and using [collate.DeepEqualer] as key equaler.
// 'second' is enumerated on the first [Enumerator.MoveNext] call.
// 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 Enumerable[Source], second Enumerable[Key], keySelector func(Source) Key) (Enumerable[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
return IntersectByEq(first, second, keySelector, nil)
}
// IntersectByMust is like [IntersectBy] but panics in case of error.
func IntersectByMust[Source, Key any](first Enumerable[Source], second Enumerable[Key], keySelector func(Source) Key) Enumerable[Source] {
return errorhelper.Must(IntersectBy(first, second, keySelector))
}
func factoryIntersectByEq[Source, Key any](first Enumerable[Source], second Enumerable[Key],
keySelector func(Source) Key, equaler collate.Equaler[Key]) func() Enumerator[Source] {
return func() Enumerator[Source] {
enrD1 := DistinctEqMust(first, collate.Equaler[Source](collate.DeepEqualer[Source]{})).GetEnumerator()
var once sync.Once
var dsl2 []Key
var c Source
return enrFunc[Source]{
mvNxt: func() bool {
once.Do(func() { dsl2 = ToSliceMust(DistinctEqMust(second, equaler)) })
for enrD1.MoveNext() {
c = enrD1.Current()
k := keySelector(c)
if elInElelEq(k, dsl2, equaler) {
return true
}
}
return false
},
crrnt: func() Source { return c },
rst: func() { enrD1.Reset() },
}
}
}
// [IntersectByEq] produces the set intersection of two sequences according to
// a specified key selector function and using a specified key equaler.
// If 'equaler' is nil, [collate.DeepEqualer] is used.
// 'second' is enumerated on the first [Enumerator.MoveNext] call.
// 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 Enumerable[Source], second Enumerable[Key],
keySelector func(Source) Key, equaler collate.Equaler[Key]) (Enumerable[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if equaler == nil {
equaler = collate.DeepEqualer[Key]{}
}
return OnFactory(factoryIntersectByEq(first, second, keySelector, equaler)), nil
}
// IntersectByEqMust is like [IntersectByEq] but panics in case of error.
func IntersectByEqMust[Source, Key any](first Enumerable[Source], second Enumerable[Key],
keySelector func(Source) Key, equaler collate.Equaler[Key]) Enumerable[Source] {
return errorhelper.Must(IntersectByEq(first, second, keySelector, equaler))
}
func factoryIntersectByCmp[Source, Key any](first Enumerable[Source], second Enumerable[Key],
keySelector func(Source) Key, comparer collate.Comparer[Key]) func() Enumerator[Source] {
return func() Enumerator[Source] {
enrD1 := DistinctEqMust(first, collate.Equaler[Source](collate.DeepEqualer[Source]{})).GetEnumerator()
var once sync.Once
var dsl2 []Key
var c Source
return enrFunc[Source]{
mvNxt: func() bool {
once.Do(func() {
dsl2 = ToSliceMust(DistinctCmpMust(second, comparer))
sort.Slice(dsl2, func(i, j int) bool { return comparer.Compare(dsl2[i], dsl2[j]) < 0 })
})
for enrD1.MoveNext() {
c = enrD1.Current()
k := keySelector(c)
if elInElelCmp(k, dsl2, comparer) {
return true
}
}
return false
},
crrnt: func() Source { return c },
rst: func() { enrD1.Reset() },
}
}
}
// [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 [Enumerator.MoveNext] call.
// 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 Enumerable[Source], second Enumerable[Key],
keySelector func(Source) Key, comparer collate.Comparer[Key]) (Enumerable[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if comparer == nil {
return nil, errorhelper.CallerError(ErrNilComparer)
}
return OnFactory(factoryIntersectByCmp(first, second, keySelector, comparer)), nil
}
// IntersectByCmpMust is like [IntersectByCmp] but panics in case of error.
func IntersectByCmpMust[Source, Key any](first Enumerable[Source], second Enumerable[Key],
keySelector func(Source) Key, comparer collate.Comparer[Key]) Enumerable[Source] {
return errorhelper.Must(IntersectByCmp(first, second, keySelector, comparer))
}