From 185ef3fc12b28a52c7f8b03bd11a7ef1d3c6da48 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 18 Jan 2026 06:58:41 +0000 Subject: [PATCH] done implementing 'PersistentVector.merge', which means that 'PersistentVector.delete' is now complete. (Todo: test 'PersistentVector.delete'.) --- fcore/persistent-vector.sml | 27 +++++++++++++++++++++++++-- todo.md | 1 - 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index e7eb3f0..6a33c76 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -671,8 +671,7 @@ struct val difference = newChildSize - oldChildSize val nodes = Vector.update (nodes, 0, child) - val sizes = - Vector.map (fn el => el + difference) + val sizes = Vector.map (fn el => el + difference) sizes in BRANCH (nodes, sizes) end @@ -680,6 +679,30 @@ struct raise Fail "PersistentVector.mergeWhenRightDepthIsGreater: \ \reached LEAF before (curDepth = leftDepth)" + fun mergeWhenLeftDepthIsGreater (left, right, rightDepth, curDepth) = + if rightDepth = curDepth then + mergeSameDepth (left, right) + else + case left of + BRANCH (nodes, sizes) => + let + val lastIdx = Vector.length sizes - 1 + val child = mergeWhenLeftDepthIsGreater + (Vector.sub (nodes, lastIdx), right, rightDepth, curDepth + 1) + + val oldChildSize = Vector.sub (sizes, lastIdx) + val newChildSize = getFinishIdx child + val difference = newChildSize - oldChildSize + + val nodes = Vector.update (nodes, lastIdx, child) + val sizes = Vector.map (fn el => el + difference) sizes + in + BRANCH (nodes, sizes) + end + | LEAF _ => + raise Fail "PersistentVector.mergeWhenLeftDepthIsGreater: \ + \reached LEAF before (curDepth = rightDepth)" + fun merge (left, right) = let val leftDepth = countDepth left diff --git a/todo.md b/todo.md index e497af4..fb27a52 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,3 @@ # To-do list -- Implement function to merge left and right subtrees for `PersistentVector.delete` function - Test `PersistentVector.delete` function - Implement 'yj' motion and add tests for it