-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunion.go
45 lines (35 loc) · 867 Bytes
/
union.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
package flinx
import "github.com/kom0055/go-flinx/hashset"
// Union produces the set union of two collections.
//
// This method excludes duplicates from the return set. This is different
// behavior to the Concat method, which returns all the elements in the input
// collection including duplicates.
func Union[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]()
use1 := true
return func() (item T, ok bool) {
if use1 {
for item, ok = next(); ok; item, ok = next() {
if !set.Has(item) {
set.Insert(item)
return
}
}
use1 = false
}
for item, ok = next2(); ok; item, ok = next2() {
if !set.Has(item) {
set.Insert(item)
return
}
}
return
}
},
}
}