2020-06-08 03:29:51 +03:00
|
|
|
package ranges
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-30 16:24:08 +03:00
|
|
|
"sort"
|
2020-06-08 03:29:51 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func max(a, b int64) int64 {
|
|
|
|
if a < b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int64) int64 {
|
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
type Range struct {
|
|
|
|
Start int64
|
|
|
|
Len int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Range) Stop() int64 { return r.Start + r.Len }
|
|
|
|
|
2021-10-18 22:38:16 +03:00
|
|
|
func (r Range) String() string { return fmt.Sprintf("%d:%d", r.Start, r.Len) }
|
|
|
|
|
|
|
|
func (r Range) IsZero() bool { return r.Start == 0 && r.Len == 0 }
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-08-30 16:24:08 +03:00
|
|
|
type rangeStartSort []Range
|
|
|
|
|
|
|
|
func (p rangeStartSort) Len() int { return len(p) }
|
|
|
|
func (p rangeStartSort) Less(i, j int) bool { return p[i].Start < p[j].Start }
|
|
|
|
func (p rangeStartSort) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func RangeFromString(s string) Range {
|
|
|
|
ps := strings.Split(s, ":")
|
|
|
|
start, _ := strconv.Atoi(ps[0])
|
|
|
|
l, _ := strconv.Atoi(ps[1])
|
|
|
|
return Range{Start: int64(start), Len: int64(l)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SliceFromString(s string) []Range {
|
|
|
|
rr := []Range{}
|
|
|
|
for _, p := range strings.Split(s, " ") {
|
|
|
|
if p == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rr = append(rr, RangeFromString(p))
|
|
|
|
}
|
|
|
|
return rr
|
|
|
|
}
|
|
|
|
|
|
|
|
func MinMax(a, b Range) Range {
|
|
|
|
minStart := min(a.Start, b.Start)
|
|
|
|
maxStop := max(a.Stop(), b.Stop())
|
|
|
|
return Range{Start: minStart, Len: maxStop - minStart}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gaps in ranges limited by total range
|
|
|
|
func Gaps(total Range, ranges []Range) []Range {
|
|
|
|
if len(ranges) == 0 {
|
|
|
|
return []Range{total}
|
|
|
|
}
|
|
|
|
|
2022-08-30 16:24:08 +03:00
|
|
|
sort.Sort(rangeStartSort(ranges))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
// worst case ranges+1 gaps
|
|
|
|
merged := make([]Range, 0, len(ranges)+1)
|
|
|
|
var madded bool
|
|
|
|
var m Range
|
|
|
|
|
|
|
|
for i := 0; i < len(ranges); {
|
|
|
|
madded = false
|
|
|
|
m = ranges[i]
|
|
|
|
j := i + 1
|
|
|
|
for ; j < len(ranges); j++ {
|
|
|
|
if m.Start <= ranges[j].Start && m.Stop()+1 >= ranges[j].Start {
|
|
|
|
if ranges[j].Stop() > m.Stop() {
|
|
|
|
m.Len = ranges[j].Stop() - m.Start
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = j
|
|
|
|
merged = append(merged, m)
|
|
|
|
madded = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if j >= len(ranges) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !madded {
|
|
|
|
merged = append(merged, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
gaps := make([]Range, 0, len(merged))
|
|
|
|
if merged[0].Start != total.Start {
|
|
|
|
gaps = append(gaps, Range{Start: 0, Len: merged[0].Start})
|
|
|
|
}
|
|
|
|
for i := 0; i < len(merged)-1; i++ {
|
|
|
|
gaps = append(gaps, Range{Start: merged[i].Stop(), Len: merged[i+1].Start - merged[i].Stop()})
|
|
|
|
}
|
|
|
|
l := merged[len(merged)-1]
|
|
|
|
if l.Stop() != total.Stop() {
|
|
|
|
gaps = append(gaps, Range{Start: l.Stop(), Len: total.Stop() - l.Stop()})
|
|
|
|
}
|
|
|
|
|
|
|
|
return gaps
|
|
|
|
}
|