/
utils.go
61 lines (52 loc) 路 1.03 KB
/
utils.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
package astutils
import (
"go/token"
"sort"
)
// lists must contain only integers i.e > 0.
func mergeSortedListsUniq(aList, bList []int, max int) []int {
lenA := len(aList)
lenB := len(bList)
sort.Ints(aList)
sort.Ints(bList)
result := make([]int, 0, lenA+lenB)
a, b, prevValue := 0, 0, 0
for a < lenA || b < lenB {
value := 0
// If within bounds of both lists.
if a < lenA && b < lenB {
if aList[a] > bList[b] {
value = bList[b]
b++
} else if aList[a] < bList[b] {
value = aList[a]
a++
} else { // Equality
value = aList[a]
a++
b++
}
} else {
if a >= lenA {
value = bList[b]
b++
} else if b >= lenB {
value = aList[a]
a++
}
}
// Ensure no duplicate elements.
if value != prevValue && value < max {
result = append(result, value)
prevValue = value
}
}
return result
}
func fileGetLines(f *token.File) []int {
lines := make([]int, f.LineCount())
for i := 0; i < f.LineCount(); i++ {
lines[i] = f.Offset(f.LineStart(i + 1))
}
return lines
}