first pass implementing 'convertChar' function

This commit is contained in:
2025-10-05 20:19:26 +01:00
parent ecdf642f13
commit 988ef22e75

View File

@@ -393,17 +393,45 @@ struct
SOME (pos, #transitions record) SOME (pos, #transitions record)
end end
fun convertChar (char, regex, dstates, dtran, curStates) = type dtran = {states: int list, transitions: int list Set.set}
fun convertChar
(char, regex, dstates, dtran: dtran vector, curStates, curStatesIdx) =
if char < 0 then if char < 0 then
(dstates, Vector.fromList dtran) (dstates, dtran)
else else
let let
(* get union of all follow positions *) (* get union of all follow positions *)
val u = getFollowPositionsFromList (curStates, regex, char, Set.LEAF) val u = getFollowPositionsFromList (curStates, regex, char, Set.LEAF)
(* add follow positions to dstates if they are not already inside *) (* add follow positions to dstates if they are not already inside *)
val dstates = appendIfNew (0, dstates, u) val dstates = appendIfNew (0, dstates, u)
(* update dtran to include transitions for char.
* Todo: The code below updates the same vector each time.
* It would be more efficient if we accumulate the set,
* and then later append/update it once the loop is done. *)
val dtran =
if curStatesIdx >= Vector.length dtran then
(* corresponding idx doesn't exist in dtran
* so we append to dtran instead *)
let
val transitions = Set.insertOrReplace (char, u, Set.LEAF)
val record = {states = curStates, transitions = transitions}
in in
raise Fail "todo" Vector.concat [dtran, Vector.fromList [record]]
end
else
(* corresponding state idx does exist in dtran, so we update it *)
let
val {states, transitions} = Vector.sub (dtran, curStatesIdx)
val transitions = Set.insertOrReplace (char, u, transitions)
val record = {states = states, transitions = transitions}
in
Vector.update (dtran, curStatesIdx, record)
end
in
convertChar (char - 1, regex, dstates, dtran, curStates, curStatesIdx)
end end
fun convertLoop (regex, dstates) = fun convertLoop (regex, dstates) =