From 2f2d530dae2191bc068e386dfecda4689924f31a Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 9 Dec 2025 13:10:23 +0000 Subject: [PATCH] fix calculation for delete function (we needed to fix the calculation of how much to decrement by) --- fcore/persistent-vector.sml | 17 ++++++++--------- test/persistent-vector-tests.sml | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index a094262..c36a4d8 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -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 diff --git a/test/persistent-vector-tests.sml b/test/persistent-vector-tests.sml index 4cb53d8..17fd50e 100644 --- a/test/persistent-vector-tests.sml +++ b/test/persistent-vector-tests.sml @@ -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"