add backtracking functionality to search; when we find a partial but incomplete match, we want to start looking for a match from ('idx + 1'), where 'idx' is the index we started this failed search at.
This commit is contained in:
@@ -4,10 +4,49 @@ struct
|
|||||||
|
|
||||||
val empty = Vector.fromList []
|
val empty = Vector.fromList []
|
||||||
|
|
||||||
fun loopSearch (pos, hd, absIdx, tl, acc, searchPos, searchString) =
|
fun backtrackFull (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl) =
|
||||||
|
if searchPos <= 1 then
|
||||||
|
(* we are trying to backtrack to index 1,
|
||||||
|
* and then continue are search from here *)
|
||||||
|
loopSearch (pos, hd, absIdx, tl, acc, 0, searchString, prevTl)
|
||||||
|
else if pos < 0 then
|
||||||
|
case prevTl of
|
||||||
|
prevHd :: prevTl =>
|
||||||
|
let
|
||||||
|
val tl = hd :: tl
|
||||||
|
in
|
||||||
|
backtrackFull
|
||||||
|
( String.size prevHd - 1
|
||||||
|
, prevHd
|
||||||
|
, absIdx
|
||||||
|
, tl
|
||||||
|
, acc
|
||||||
|
, searchPos
|
||||||
|
, searchString
|
||||||
|
, prevTl
|
||||||
|
)
|
||||||
|
end
|
||||||
|
| [] =>
|
||||||
|
(* Should never be called *)
|
||||||
|
raise Fail "SearchList.backtrackFull error: line 24\n"
|
||||||
|
else
|
||||||
|
backtrackFull
|
||||||
|
(pos - 1, hd, absIdx - 1, tl, acc, searchPos - 1, searchString, prevTl)
|
||||||
|
|
||||||
|
and loopSearch (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl) =
|
||||||
if pos = String.size hd then
|
if pos = String.size hd then
|
||||||
case tl of
|
case tl of
|
||||||
hd :: tl => loopSearch (0, hd, absIdx, tl, acc, searchPos, searchString)
|
newHd :: newTl =>
|
||||||
|
loopSearch
|
||||||
|
( 0
|
||||||
|
, newHd
|
||||||
|
, absIdx
|
||||||
|
, newTl
|
||||||
|
, acc
|
||||||
|
, searchPos
|
||||||
|
, searchString
|
||||||
|
, hd :: prevTl
|
||||||
|
)
|
||||||
| [] => PersistentVector.toVector acc
|
| [] => PersistentVector.toVector acc
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
@@ -21,22 +60,32 @@ struct
|
|||||||
val foundIdx = absIdx - String.size searchString + 1
|
val foundIdx = absIdx - String.size searchString + 1
|
||||||
val acc = PersistentVector.append (foundIdx, acc)
|
val acc = PersistentVector.append (foundIdx, acc)
|
||||||
in
|
in
|
||||||
loopSearch (pos + 1, hd, absIdx + 1, tl, acc, 0, searchString)
|
loopSearch
|
||||||
|
(pos + 1, hd, absIdx + 1, tl, acc, 0, searchString, prevTl)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
loopSearch
|
loopSearch
|
||||||
(pos + 1, hd, absIdx + 1, tl, acc, searchPos + 1, searchString)
|
( pos + 1
|
||||||
|
, hd
|
||||||
|
, absIdx + 1
|
||||||
|
, tl
|
||||||
|
, acc
|
||||||
|
, searchPos + 1
|
||||||
|
, searchString
|
||||||
|
, prevTl
|
||||||
|
)
|
||||||
else
|
else
|
||||||
(if searchPos = 0 then
|
(if searchPos = 0 then
|
||||||
loopSearch (pos + 1, hd, absIdx + 1, tl, acc, 0, searchString)
|
loopSearch
|
||||||
|
(pos + 1, hd, absIdx + 1, tl, acc, 0, searchString, prevTl)
|
||||||
else
|
else
|
||||||
loopSearch (pos, hd, absIdx, tl, acc, 0, searchString))
|
loopSearch (pos, hd, absIdx, tl, acc, 0, searchString, prevTl))
|
||||||
end
|
end
|
||||||
|
|
||||||
fun search ({rightStrings, leftStrings, ...}: LineGap.t, searchString) =
|
fun search ({rightStrings, leftStrings, ...}: LineGap.t, searchString) =
|
||||||
case rightStrings of
|
case rightStrings of
|
||||||
hd :: tl =>
|
hd :: tl =>
|
||||||
loopSearch (0, hd, 0, tl, PersistentVector.empty, 0, searchString)
|
loopSearch (0, hd, 0, tl, PersistentVector.empty, 0, searchString, [])
|
||||||
| [] => empty
|
| [] => empty
|
||||||
|
|
||||||
(* Prerequisite: move buffer/LineGap to start *)
|
(* Prerequisite: move buffer/LineGap to start *)
|
||||||
|
|||||||
Reference in New Issue
Block a user