fix calculation for delete function (we needed to fix the calculation of how much to decrement by)

This commit is contained in:
2025-12-09 13:10:23 +00:00
parent 7a5aca7bf2
commit 2f2d530dae
2 changed files with 25 additions and 9 deletions

View File

@@ -900,7 +900,7 @@ struct
fun delete (start, length, tree) = fun delete (start, length, tree) =
let let
val finishIdx = start + length val finishIdx = start + length + 1
val {start, finish} = startNextMatch (start, tree) val {start, finish} = startNextMatch (start, tree)
in in
if start = ~1 then if start = ~1 then
@@ -910,15 +910,14 @@ struct
let let
(* split left and right side *) (* split left and right side *)
val left = splitLeft (start, tree) val left = splitLeft (start, tree)
val left = root left
val right = splitRight (finishIdx, tree) val right = splitRight (finishIdx, tree)
val right = root right
in in
if isEmpty right then if isEmpty right then
root left left
else else
let let
val left = root left
val right = root right
(* calculate what the new index should be (* calculate what the new index should be
* for the first match in the right tree, * for the first match in the right tree,
* and decrement the right tree to reach this index * and decrement the right tree to reach this index
@@ -937,13 +936,13 @@ struct
* This step gives us the difference: how much * This step gives us the difference: how much
* we need to decrement the right node by. * we need to decrement the right node by.
* *) * *)
val {start = shouldBeStart, ...} = val {start = originalStart, ...} = startNextMatch (finishIdx, tree)
startNextMatch (finishIdx, tree) val originalStart = originalStart - length
val shouldBeStart = shouldBeStart - length
val leftSize = getMaxSize left val leftSize = getMaxSize left
val {start = rightStart, ...} = getFirstItem right val {start = rightStart, ...} = getFirstItem right
val difference = rightStart - shouldBeStart val rightStart = rightStart + leftSize
val difference = rightStart - originalStart
val right = val right =
if difference = 0 then if difference = 0 then
right right

View File

@@ -50,6 +50,23 @@ struct
in in
Expect.isTrue (outputList = expectedList) Expect.isTrue (outputList = expectedList)
end) end)
, test
"deletes second-last value correctly \
\and adjusts last value as expected"
(fn _ =>
let
(* arrange *)
val pv = Pv.fromList [(0, 0), (3, 3), (5, 5), (7, 7)]
(* act *)
val pv = Pv.delete (4, 1, pv)
(* assert *)
val outputList = Pv.toList pv
val expectedList = [(0, 0), (3, 3), (6, 6)]
in
Expect.isTrue (outputList = expectedList)
end)
, test , test
"deletes middle value correctly \ "deletes middle value correctly \
\and adjusts values-after-middle as well" \and adjusts values-after-middle as well"