-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
except.go
57 lines (52 loc) · 1.3 KB
/
except.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 linq
type exceptEnumerator[T any, H comparable] struct {
fst Enumerator[T]
snd Enumerator[T]
eq func(T, T) (bool, error)
hash func(T) (H, error)
hmap *hashMap[H, T]
}
// Except produces the set difference of two sequences by using the specified comparer functions.
func Except[T any, E IEnumerable[T]](first, second E, equals func(T, T) (bool, error), getHashCode func(T) (int, error)) Enumerable[T] {
return func() Enumerator[T] {
return &exceptEnumerator[T, int]{
fst: first(),
snd: second(),
eq: equals,
hash: getHashCode,
}
}
}
// ExceptBy produces the set difference of two sequences according to a specified key selector function.
func ExceptBy[T any, K comparable, E IEnumerable[T]](first, second E, keySelector func(v T) (K, error)) Enumerable[T] {
return func() Enumerator[T] {
return &exceptEnumerator[T, K]{
fst: first(),
snd: second(),
eq: alwaysEqual[T],
hash: keySelector,
}
}
}
func (e *exceptEnumerator[T, H]) Next() (def T, _ error) {
if e.hmap == nil {
hm := newHashMap(e.hash, e.eq)
if err := hm.addAll(e.snd); err != nil {
return def, err
}
e.hmap = hm
}
for {
v, err := e.fst.Next()
if err != nil {
return def, err
}
has, err := e.hmap.has(v)
if err != nil {
return def, err
}
if !has {
return v, nil
}
}
}