add 'SearchList.exists' function to fcore/search-list.sml function, and make sure we check if mmatchedIdx exists before inserting or appending (so we maintain set-like semantics where each number exists only once)

This commit is contained in:
2024-11-24 21:38:58 +00:00
parent c6343cac40
commit d399016c1a
3 changed files with 120 additions and 44 deletions

View File

@@ -69,4 +69,28 @@ struct
fun equalOrMore (findNum, vec) =
helpBinSearch (findNum, vec, 0, Vector.length vec - 1)
end
local
fun helpExists (findNum, vec, low, high) =
let
val mid = low + ((high - low) div 2)
in
if high >= low then
let
val midVal = Vector.sub (vec, mid)
in
if midVal = findNum then
true
else if midVal < findNum then
helpExists (findNum, vec, mid + 1, high)
else
helpExists (findNum, vec, low, mid - 1)
end
else
false
end
in
fun exists (findNum, vec) =
helpExists (findNum, vec, 0, Vector.length vec - 1)
end
end