refactor some code to use looping in DFA (which is faster than looping outside of the data structure)

This commit is contained in:
2025-08-03 14:17:25 +01:00
parent 904526cd63
commit e078ca89d2
4 changed files with 86 additions and 36 deletions

View File

@@ -6,7 +6,7 @@ end
functor MakeNextDfaLoop(M: MAKE_DFA_LOOP) =
struct
fun next (lineGap: LineGap.t, cursorIdx) =
fun next (lineGap: LineGap.t, cursorIdx, count) =
let
val {rightStrings, idx = bufferIdx, ...} = lineGap
(* convert absolute cursorIdx to idx relative to hd string *)
@@ -16,13 +16,13 @@ struct
shd :: stl =>
if strIdx < String.size shd then
(* strIdx is in this string *)
M.fStart (strIdx, cursorIdx, shd, stl, M.startState, 1)
M.fStart (strIdx, cursorIdx, shd, stl, M.startState, count)
else
(* strIdx is in tl *)
(case stl of
stlhd :: stltl =>
let val strIdx = strIdx - String.size shd
in M.fStart (strIdx, cursorIdx, stlhd, stltl, M.startState, 1)
in M.fStart (strIdx, cursorIdx, stlhd, stltl, M.startState, count)
end
| _ => cursorIdx)
| [] => cursorIdx
@@ -31,7 +31,7 @@ end
functor MakeNextDfaLoopPlus1(M: MAKE_DFA_LOOP) =
struct
fun next (lineGap: LineGap.t, cursorIdx) =
fun next (lineGap: LineGap.t, cursorIdx, count) =
let
val {rightStrings, idx = bufferIdx, ...} = lineGap
(* convert absolute cursorIdx to idx relative to hd string *)
@@ -42,13 +42,13 @@ struct
shd :: stl =>
if strIdx < String.size shd then
(* strIdx is in this string *)
M.fStart (strIdx, absIdx, shd, stl, M.startState, 1)
M.fStart (strIdx, absIdx, shd, stl, M.startState, count)
else
(* strIdx is in tl *)
(case stl of
stlhd :: stltl =>
let val strIdx = strIdx - String.size shd
in M.fStart (strIdx, absIdx, stlhd, stltl, M.startState, 1)
in M.fStart (strIdx, absIdx, stlhd, stltl, M.startState, count)
end
| _ => cursorIdx)
| [] => cursorIdx
@@ -57,7 +57,7 @@ end
functor MakePrevDfaLoop(M: MAKE_DFA_LOOP) =
struct
fun prev (lineGap: LineGap.t, cursorIdx) =
fun prev (lineGap: LineGap.t, cursorIdx, count) =
let
val {rightStrings, leftStrings, idx = bufferIdx, ...} = lineGap
(* convert absolute cursorIdx to idx relative to hd string *)
@@ -67,7 +67,7 @@ struct
shd :: stl =>
if strIdx < String.size shd then
(* strIdx is in this string *)
M.fStart (strIdx, cursorIdx, shd, leftStrings, M.startState, 1)
M.fStart (strIdx, cursorIdx, shd, leftStrings, M.startState, count)
else
(* strIdx is in tl *)
(case stl of
@@ -77,7 +77,7 @@ struct
val leftStrings = shd :: leftStrings
in
M.fStart
(strIdx, cursorIdx, stlhd, leftStrings, M.startState, 1)
(strIdx, cursorIdx, stlhd, leftStrings, M.startState, count)
end
| [] => cursorIdx)
| [] => cursorIdx
@@ -86,7 +86,7 @@ end
functor MakePrevDfaLoopMinus1(M: MAKE_DFA_LOOP) =
struct
fun prev (lineGap: LineGap.t, cursorIdx) =
fun prev (lineGap: LineGap.t, cursorIdx, count) =
let
val {idx = bufferIdx, leftStrings, ...} = lineGap
val strIdx = cursorIdx - bufferIdx - 1
@@ -95,12 +95,12 @@ struct
if strIdx < 0 then
case leftStrings of
lhd :: ltl =>
M.fStart (String.size lhd - 1, absIdx, lhd, ltl, M.startState, 1)
M.fStart (String.size lhd - 1, absIdx, lhd, ltl, M.startState, count)
| [] => 0
else
case #rightStrings lineGap of
rhd :: _ =>
M.fStart (strIdx, absIdx, rhd, leftStrings, M.startState, 1)
M.fStart (strIdx, absIdx, rhd, leftStrings, M.startState, count)
| [] => Int.max (0, cursorIdx - 2)
end
end