/
set.go
52 lines (49 loc) · 1.31 KB
/
set.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 types
import "strconv"
// sortedSetDifference allows you to take the difference of two (pre-)sorted sets,
// returning the indices of all elements that are in A, but not in B.
//
// cmp should return the result as: 0 if a==b, -1 if a < b, and +1 if a > b.
func sortedSetDifference(lenA, lenB int, cmp func(a int, b int) int) (indices []int) {
var idxA, idxB, result int
for idxA < lenA && idxB < lenB {
switch result = cmp(idxA, idxB); result {
case 0:
idxA++
idxB++
case -1:
indices = append(indices, idxA)
idxA++
case 1:
idxB++
default:
panic("unexpected cmp result: " + strconv.Itoa(result))
}
}
for ; idxA < lenA; idxA++ {
indices = append(indices, idxA)
}
return
}
// sortedSetIntersection allows you to take the intersection of two (pre-)sorted sets,
// returning the indices of all elements that are in A AND in B.
//
// cmp should return the result as: 0 if a==b, -1 if a < b, and +1 if a > b.
func sortedSetIntersection(lenA, lenB int, cmp func(a int, b int) int) (indices []int) {
var idxA, idxB, result int
for idxA < lenA && idxB < lenB {
switch result = cmp(idxA, idxB); result {
case 0:
indices = append(indices, idxA)
idxA++
idxB++
case -1:
idxA++
case 1:
idxB++
default:
panic("unexpected cmp result: " + strconv.Itoa(result))
}
}
return
}