-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.go
79 lines (64 loc) · 1.47 KB
/
internal.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
72
73
74
75
76
77
78
79
package internal
func ReturnError(err error) (string, string, error) {
return "", "", err
}
// Permutations https://en.wikipedia.org/wiki/Heap%27s_algorithm
func Permutations[T any](data []T) [][]T {
var result [][]T
var helper func(int, []T)
helper = func(n int, a []T) {
if n == 1 {
cp := make([]T, len(a))
copy(cp, a)
result = append(result, cp)
return
}
for i := 0; i < n; i++ {
helper(n-1, a)
if n%2 == 0 {
a[i], a[n-1] = a[n-1], a[i]
} else {
a[0], a[n-1] = a[n-1], a[0]
}
}
}
helper(len(data), data)
return result
}
type Range struct {
Start int
End int
}
func FindIntersection(r1, r2 Range) (Range, bool) {
if r1.End < r2.Start || r2.End < r1.Start {
return Range{}, false
}
iStart := max(r1.Start, r2.Start)
iEnd := min(r1.End, r2.End)
return Range{Start: iStart, End: iEnd}, true
}
func FindPartialIntersection(r1, r2 Range) (Range, []Range, bool) {
// no intersection at all
intersection, ok := FindIntersection(r1, r2)
if !ok {
return Range{}, nil, false
}
// no remainders
if r1.Start >= r2.Start && r1.End <= r2.End {
return intersection, nil, true
}
var remainder []Range
// left
if intersection.Start > r1.Start {
start := r1.Start
end := intersection.Start - 1
remainder = append(remainder, Range{start, end})
}
// right
if intersection.End < r1.End {
start := intersection.End + 1
end := r1.End
remainder = append(remainder, Range{start, end})
}
return intersection, remainder, true
}