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

View File

@@ -50,6 +50,23 @@ struct
in
Expect.isTrue (outputList = expectedList)
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
"deletes middle value correctly \
\and adjusts values-after-middle as well"