返回 DeepSeek-Reasonix
range.go
根目录 / internal / readcoord / range.go
1 // Package readcoord tracks what a read actually delivered and decides whether
2 // a read requirement is met. It is host-only: it renders nothing, mutates no
3 // file, and never changes provider bytes.
4 package readcoord
5
6 import (
7 "slices"
8
9 "reasonix/internal/tool"
10 )
11
12 // Normalize drops empty ranges and merges overlaps and adjacency into the
13 // smallest equivalent set.
14 func Normalize(ranges []tool.ReadRange) []tool.ReadRange {
15 out := make([]tool.ReadRange, 0, len(ranges))
16 for _, r := range ranges {
17 if !r.Empty() {
18 out = append(out, r)
19 }
20 }
21 if len(out) == 0 {
22 return nil
23 }
24 slices.SortFunc(out, func(a, b tool.ReadRange) int {
25 if a.Start != b.Start {
26 return a.Start - b.Start
27 }
28 return a.End - b.End
29 })
30 merged := out[:1]
31 for _, r := range out[1:] {
32 last := &merged[len(merged)-1]
33 if r.Start <= last.End {
34 last.End = max(last.End, r.End)
35 continue
36 }
37 merged = append(merged, r)
38 }
39 return merged
40 }
41
42 // Subtract returns the parts of want that have does not cover.
43 func Subtract(want, have []tool.ReadRange) []tool.ReadRange {
44 have = Normalize(have)
45 var out []tool.ReadRange
46 for _, w := range Normalize(want) {
47 cur := w
48 for _, h := range have {
49 if h.End <= cur.Start {
50 continue
51 }
52 if h.Start >= cur.End {
53 break
54 }
55 if h.Start > cur.Start {
56 out = append(out, tool.ReadRange{Start: cur.Start, End: h.Start})
57 }
58 cur.Start = max(cur.Start, h.End)
59 if cur.Empty() {
60 break
61 }
62 }
63 if !cur.Empty() {
64 out = append(out, cur)
65 }
66 }
67 return out
68 }
69
70 // Covers reports whether have covers every line of want.
71 func Covers(have, want []tool.ReadRange) bool {
72 return len(Subtract(want, have)) == 0
73 }
74
74 lines GO