| 1 | // Copyright 2022 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/ |
| 6 | |
| 7 | import ( |
| 8 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 9 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 10 | ) |
| 11 | |
| 12 | // lcs is a longest common sequence |
| 13 | type lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs# []diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag# |
| 14 | |
| 15 | // A diag is a piece of the edit graph where A[X+i] == B[Y+i], for 0<=i<Len. |
| 16 | // All computed diagonals are parts of a longest common subsequence. |
| 17 | type diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag# struct { |
| 18 | Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X., Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. int |
| 19 | Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. int |
| 20 | } |
| 21 | |
| 22 | // sort sorts in place, by lowest X, and if tied, inversely by Len |
| 23 | func (l lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#) sortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#sort().() lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs# { |
| 24 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(l, func(i, j int) bool { |
| 25 | if l[i].Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. != l[j].Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. { |
| 26 | return l[i].Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. < l[j].Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. |
| 27 | } |
| 28 | return l[i].Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. > l[j].Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. |
| 29 | }) |
| 30 | return l |
| 31 | } |
| 32 | |
| 33 | // validate that the elements of the lcs do not overlap |
| 34 | // (can only happen when the two-sided algorithm ends early) |
| 35 | // expects the lcs to be sorted |
| 36 | func (l lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#) validscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#valid().() bool { |
| 37 | for i := 1; i < len(l); i++ { |
| 38 | if l[i-1].Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.+l[i-1].Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. > l[i].Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. { |
| 39 | return false |
| 40 | } |
| 41 | if l[i-1].Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.+l[i-1].Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. > l[i].Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. { |
| 42 | return false |
| 43 | } |
| 44 | } |
| 45 | return true |
| 46 | } |
| 47 | |
| 48 | // repair overlapping lcs |
| 49 | // only called if two-sided stops early |
| 50 | func (l lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#) fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#fix().() lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs# { |
| 51 | // from the set of diagonals in l, find a maximal non-conflicting set |
| 52 | // this problem may be NP-complete, but we use a greedy heuristic, |
| 53 | // which is quadratic, but with a better data structure, could be D log D. |
| 54 | // independent is not enough: {0,3,1} and {3,0,2} can't both occur in an lcs |
| 55 | // which has to have monotone x and y |
| 56 | if len(l) == 0 { |
| 57 | return nil |
| 58 | } |
| 59 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(l, func(i, j int) bool { return l[i].Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. > l[j].Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. }) |
| 60 | tmp := make(lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#, 0, len(l)) |
| 61 | tmp = append(tmp, l[0]) |
| 62 | for i := 1; i < len(l); i++ { |
| 63 | var dir directionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/direction# |
| 64 | nxt := l[i] |
| 65 | for _, in := range tmp { |
| 66 | if dir, nxt = overlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/overlap().(in, nxt); dir == emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/empty. || dir == badscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/bad. { |
| 67 | break |
| 68 | } |
| 69 | } |
| 70 | if nxt.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. > 0 && dir != badscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/bad. { |
| 71 | tmp = append(tmp, nxt) |
| 72 | } |
| 73 | } |
| 74 | tmp.sortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#sort().() |
| 75 | if false && !tmp.validscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#valid().() { // debug checking |
| 76 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 log/Fatalf().("here %d", len(tmp)) |
| 77 | } |
| 78 | return tmp |
| 79 | } |
| 80 | |
| 81 | type directionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/direction# int |
| 82 | |
| 83 | const ( |
| 84 | emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/empty. directionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/direction# = iota // diag is empty (so not in lcs) |
| 85 | leftdownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/leftdown. // proposed acceptably to the left and below |
| 86 | rightupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/rightup. // proposed diag is acceptably to the right and above |
| 87 | badscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/bad. // proposed diag is inconsistent with the lcs so far |
| 88 | ) |
| 89 | |
| 90 | // overlap trims the proposed diag prop so it doesn't overlap with |
| 91 | // the existing diag that has already been added to the lcs. |
| 92 | func overlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/overlap().(exist, prop diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#) (directionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/direction#, diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#) { |
| 93 | if prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. <= exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. && exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. < prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.+prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. { |
| 94 | // remove the end of prop where it overlaps with the X end of exist |
| 95 | delta := prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. + prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. - exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. |
| 96 | prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. -= delta |
| 97 | if prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= 0 { |
| 98 | return emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/empty., prop |
| 99 | } |
| 100 | } |
| 101 | if exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. <= prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. && prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. < exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.+exist.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. { |
| 102 | // remove the beginning of prop where overlaps with exist |
| 103 | delta := exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. + exist.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. - prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. |
| 104 | prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. -= delta |
| 105 | if prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= 0 { |
| 106 | return emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/empty., prop |
| 107 | } |
| 108 | prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. += delta |
| 109 | prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. += delta |
| 110 | } |
| 111 | if prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. <= exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. && exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. < prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.+prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. { |
| 112 | // remove the end of prop that overlaps (in Y) with exist |
| 113 | delta := prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. + prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. - exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. |
| 114 | prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. -= delta |
| 115 | if prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= 0 { |
| 116 | return emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/empty., prop |
| 117 | } |
| 118 | } |
| 119 | if exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. <= prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. && prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. < exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.+exist.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. { |
| 120 | // remove the beginning of peop that overlaps with exist |
| 121 | delta := exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. + exist.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. - prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. |
| 122 | prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. -= delta |
| 123 | if prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= 0 { |
| 124 | return emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/empty., prop |
| 125 | } |
| 126 | prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. += delta // no test reaches this code |
| 127 | prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. += delta |
| 128 | } |
| 129 | if prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.+prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. && prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.+prop.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. { |
| 130 | return leftdownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/leftdown., prop |
| 131 | } |
| 132 | if exist.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.+exist.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= prop.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X. && exist.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.+exist.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. <= prop.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. { |
| 133 | return rightupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/rightup., prop |
| 134 | } |
| 135 | // prop can't be in an lcs that contains exist |
| 136 | return badscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/bad., prop |
| 137 | } |
| 138 | |
| 139 | // manipulating Diag and lcs |
| 140 | |
| 141 | // prepend a diagonal (x,y)-(x+1,y+1) segment either to an empty lcs |
| 142 | // or to its first Diag. prepend is only called to extend diagonals |
| 143 | // the backward direction. |
| 144 | func (lcs lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#) prependscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#prepend().(x, y int) lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs# { |
| 145 | if len(lcs) > 0 { |
| 146 | d := &lcs[0] |
| 147 | if int(d.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.) == x+1 && int(d.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.) == y+1 { |
| 148 | // extend the diagonal down and to the left |
| 149 | d.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X., d.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y. = int(x), int(y) |
| 150 | d.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len.++ |
| 151 | return lcs |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | r := diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#{Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.: int(x), Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.: int(y), Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len.: 1} |
| 156 | lcs = append([]diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#{r}, lcs...) |
| 157 | return lcs |
| 158 | } |
| 159 | |
| 160 | // append appends a diagonal, or extends the existing one. |
| 161 | // by adding the edge (x,y)-(x+1.y+1). append is only called |
| 162 | // to extend diagonals in the forward direction. |
| 163 | func (lcs lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#) appendscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs#append().(x, y int) lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/lcs# { |
| 164 | if len(lcs) > 0 { |
| 165 | last := &lcs[len(lcs)-1] |
| 166 | // Expand last element if adjoining. |
| 167 | if last.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.+last.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. == x && last.Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.+last.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len. == y { |
| 168 | last.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len.++ |
| 169 | return lcs |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return append(lcs, diagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#{Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#X.: x, Yscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Y.: y, Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/diag#Len.: 1}) |
| 174 | } |
| 175 | |
| 176 | // enforce constraint on d, k |
| 177 | func okscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/ok().(d, k int) bool { |
| 178 | return d >= 0 && -d <= k && k <= d |
| 179 | } |