in BinSearch.equalOrMore and BinSearch.equalOrLess, return ~1 if no item is found, for the sake of less ambiguity in usage. Functions that call these have also been adapted.

This commit is contained in:
2025-09-13 02:18:14 +01:00
parent 53a4265b07
commit 20a542df29
6 changed files with 17 additions and 7 deletions

View File

@@ -3,7 +3,7 @@ struct
local local
fun reverseLinearSearch (findNum, idx, vec) = fun reverseLinearSearch (findNum, idx, vec) =
if idx < 0 then if idx < 0 then
idx ~1
else else
let let
val curVal = Vector.sub (vec, idx) val curVal = Vector.sub (vec, idx)
@@ -38,7 +38,7 @@ struct
local local
fun forwardLinearSearch (findNum, idx, vec) = fun forwardLinearSearch (findNum, idx, vec) =
if idx = Vector.length vec then if idx = Vector.length vec then
idx ~1
else else
let let
val curVal = Vector.sub (vec, idx) val curVal = Vector.sub (vec, idx)

View File

@@ -29,7 +29,13 @@ struct
val linePos = BinSearch.equalOrLess (strPos - 1, lhd) val linePos = BinSearch.equalOrLess (strPos - 1, lhd)
val lineIdx = Vector.sub (lhd, linePos) val lineIdx = Vector.sub (lhd, linePos)
in in
absIdx - strPos + lineIdx + 1 if linePos = ~1 then
(* no previous line in lhd *)
helpVi0 (absIdx - strPos, stl, ltl)
else
let val lineIdx = Vector.sub (lhd, linePos)
in absIdx - strPos + lineIdx + 1
end
end end
else else
helpVi0 (absIdx - strPos, stl, ltl) helpVi0 (absIdx - strPos, stl, ltl)
@@ -224,6 +230,7 @@ struct
* because we know lnHd definitely contains * because we know lnHd definitely contains
* a lineIdx less or equal to strIdx *) * a lineIdx less or equal to strIdx *)
let let
(* todo: what if BinSearch doesn't find anything? *)
val lnIdx = BinSearch.equalOrLess (strIdx, lnHd) val lnIdx = BinSearch.equalOrLess (strIdx, lnHd)
val lnIdx = Vector.sub (lnHd, lnIdx) val lnIdx = Vector.sub (lnHd, lnIdx)
in in

View File

@@ -262,7 +262,7 @@ struct
else else
let let
val pos = BinSearch.equalOrMore (cursorIdx + 1, searchList) val pos = BinSearch.equalOrMore (cursorIdx + 1, searchList)
val pos = if pos < Vector.length searchList then pos else 0 val pos = if pos = ~1 then 0 else pos
val count = count - 1 val count = count - 1
in in
loopNextMatch (pos, searchList, count) loopNextMatch (pos, searchList, count)
@@ -286,7 +286,7 @@ struct
else else
let let
val pos = BinSearch.equalOrLess (cursorIdx - 1, searchList) val pos = BinSearch.equalOrLess (cursorIdx - 1, searchList)
val pos = if pos < 0 then Vector.length searchList - 1 else pos val pos = if pos = ~1 then Vector.length searchList - 1 else pos
val count = count - 1 val count = count - 1
in in
loopPrevMatch (pos, searchList, count) loopPrevMatch (pos, searchList, count)

View File

@@ -29,7 +29,10 @@ struct
Utils.getRelativeLineStartFromRightHead (startLine, curLine, lhd) Utils.getRelativeLineStartFromRightHead (startLine, curLine, lhd)
(* get absolute idx of line *) (* get absolute idx of line *)
val absIdx = curIdx + strPos val absIdx = curIdx + strPos
val searchPos = BinSearch.equalOrMore (absIdx, searchList) val searchPos = BinSearch.equalOrMore (absIdx, searchList)
val searchPos =
if searchPos = ~1 then Vector.length searchList else searchPos
val env = Utils.initEnv val env = Utils.initEnv
( windowWidth ( windowWidth

View File

@@ -60,7 +60,7 @@ struct
let let
val searchPos = BinSearch.equalOrMore (pos + 1, #searchList env) val searchPos = BinSearch.equalOrMore (pos + 1, #searchList env)
in in
if searchPos = Vector.length line then if searchPos = ~1 then
(* next line is not in this node *) (* next line is not in this node *)
let let
val absIdx = absIdx - pos val absIdx = absIdx - pos

View File

@@ -75,7 +75,7 @@ struct
let let
val searchPos = BinSearch.equalOrMore (pos + 1, #searchList env) val searchPos = BinSearch.equalOrMore (pos + 1, #searchList env)
in in
if searchPos = Vector.length line then if searchPos = ~1 then
(* next line is not in this node *) (* next line is not in this node *)
let let
val absIdx = absIdx - pos val absIdx = absIdx - pos