-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintersect.go
71 lines (58 loc) · 1.66 KB
/
intersect.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
package flinx
import "github.com/kom0055/go-flinx/hashset"
// Intersect produces the set intersection of the source collection and the
// provided input collection. The intersection of two sets A and B is defined as
// the set that contains all the elements of A that also appear in B, but no
// other elements.
func Intersect[T comparable](q, q2 Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
next2 := q2.Iterate()
set := hashset.NewAny[T]()
for item, ok := next2(); ok; item, ok = next2() {
set.Insert(item)
}
return func() (item T, ok bool) {
for item, ok = next(); ok; item, ok = next() {
if set.Has(item) {
set.Delete(item)
return
}
}
return
}
},
}
}
// IntersectBy produces the set intersection of the source collection and the
// provided input collection. The intersection of two sets A and B is defined as
// the set that contains all the elements of A that also appear in B, but no
// other elements.
//
// IntersectBy invokes a transform function on each element of both collections.
func IntersectBy[T any, V comparable](selector func(T) V) func(q, q2 Query[T]) Query[T] {
return func(q, q2 Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
next2 := q2.Iterate()
set := hashset.NewAny[V]()
for item, ok := next2(); ok; item, ok = next2() {
s := selector(item)
set.Insert(s)
}
return func() (item T, ok bool) {
for item, ok = next(); ok; item, ok = next() {
s := selector(item)
if set.Has(s) {
set.Delete(s)
return
}
}
return
}
},
}
}
}