-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissubsetof.go
52 lines (39 loc) · 858 Bytes
/
issubsetof.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
package intervals
import "sort"
// IsSubsetOf reports whether elements in x are all in y.
func (x Set[E]) IsSubsetOf(y Set[E]) bool {
inv := false
for {
if len(x) == 0 || len(y) == 0 {
if len(x) == 0 {
x, y = y, x
inv = !inv
}
return inv || len(x) == 0
}
if x[0].High.Compare(y[0].High) > 0 {
x, y = y, x
inv = !inv
}
r := y[0]
y = y[1:]
if inv {
i := sort.Search(len(x), func(i int) bool { return x[i].High.Compare(r.Low) > 0 })
x = x[i:]
if len(x) == 0 || x[0].Low.Compare(r.Low) > 0 || x[0].High.Compare(r.High) < 0 {
return false
}
continue
}
if x[0].Low.Compare(r.Low) < 0 {
return false
}
j := sort.Search(len(x), func(i int) bool { return x[i].Low.Compare(r.High) >= 0 })
if j > 0 {
if x[j-1].High.Compare(r.High) > 0 {
return false
}
x = x[j:]
}
}
}