MCPcopy Create free account
hub / github.com/Effect-TS/effect / findSplit

Function findSplit

packages/effect/src/TextDiff.ts:823–888  ·  view source on GitHub ↗
(aLo: number, aHi: number, bLo: number, bHi: number)

Source from the content-addressed store, hash-verified

821 // empty snake at the furthest point reached when the region needs more than
822 // `maxEdits` edits, or undefined when no point inside the region was reached
823 const findSplit = (aLo: number, aHi: number, bLo: number, bHi: number): Snake | undefined => {
824 const N = aHi - aLo
825 const M = bHi - bLo
826 const delta = N - M
827 const odd = (delta & 1) === 1
828 const limit = Math.min((N + M + 1) >> 1, maxD)
829 vf[offset + 1] = 0
830 vb[offset + 1] = 0
831 for (let d = 0; d <= limit; d++) {
832 for (let k = -d; k <= d; k += 2) {
833 let x = k === -d || (k !== d && vf[offset + k - 1] < vf[offset + k + 1])
834 ? vf[offset + k + 1]
835 : vf[offset + k - 1] + 1
836 let y = x - k
837 const x0 = x
838 const y0 = y
839 while (x < N && y < M && equals(aLo + x, bLo + y)) {
840 x++
841 y++
842 }
843 vf[offset + k] = x
844 if (odd && k >= delta - (d - 1) && k <= delta + (d - 1) && x + vb[offset + delta - k] >= N) {
845 return { x: aLo + x0, y: bLo + y0, u: aLo + x, v: bLo + y }
846 }
847 }
848 for (let k = -d; k <= d; k += 2) {
849 let x = k === -d || (k !== d && vb[offset + k - 1] < vb[offset + k + 1])
850 ? vb[offset + k + 1]
851 : vb[offset + k - 1] + 1
852 let y = x - k
853 const x0 = x
854 const y0 = y
855 while (x < N && y < M && equals(aHi - x - 1, bHi - y - 1)) {
856 x++
857 y++
858 }
859 vb[offset + k] = x
860 if (!odd && k >= delta - d && k <= delta + d && x + vf[offset + delta - k] >= N) {
861 return { x: aHi - x, y: bHi - y, u: aHi - x0, v: bHi - y0 }
862 }
863 }
864 }
865
866 // Over the cap: split at the reached point that covers the most items. It
867 // must lie strictly inside the region so that both parts shrink
868 let best = 0
869 let x = 0
870 let y = 0
871 for (let k = -limit; k <= limit; k += 2) {
872 const xf = vf[offset + k]
873 const yf = xf - k
874 if (xf <= N && yf >= 0 && yf <= M && xf + yf > best) {
875 best = xf + yf
876 x = aLo + xf
877 y = bLo + yf
878 }
879 const xb = vb[offset + k]
880 const yb = xb - k

Callers 1

matchingBlocksFunction · 0.85

Calls 1

equalsFunction · 0.70

Tested by

no test coverage detected