From 559b254b0cc138d3e020d70f86a92594338b95e9 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 15 Jan 2026 06:13:38 +0000 Subject: [PATCH] initial implementation of 'PersistentVector.splitLeft' function --- fcore/persistent-vector.sml | 43 ++++++++++++++++++++++++++++++++++++- todo.md | 1 + 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index 98225d0..89ff765 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -15,7 +15,7 @@ struct fun isEmpty t = case t of LEAF (_, sizes) => Vector.length sizes = 0 - | _ => false + | BRANCH (_, sizes) => Vector.length sizes = 0 val empty = LEAF (#[], #[]) @@ -363,4 +363,45 @@ struct LEAF (items, sizes) end end + | BRANCH (nodes, sizes) => + if cursorIdx < Vector.sub (sizes, 0) then + (* we want to split first node from rest *) + splitLeft (cursorIdx, Vector.sub (nodes, 0)) + else if cursorIdx > Vector.sub (sizes, Vector.length sizes - 1) then + (* split point is after this subtree, + * so return this subtree unchanged *) + tree + else + (* we want to split from somewhere in middle *) + let + val idx = BinSearch.equalOrMore (cursorIdx, sizes) + val prevSize = + if idx = 0 then + 0 + else + Vector.sub (sizes, idx - 1) + val child = + splitLeft (cursorIdx - prevSize, Vector.sub (nodes, idx)) + + val sizes = VectorSlice.slice (sizes, 0, SOME idx) + val nodes = VectorSlice.slice (nodes, 0, SOME idx) + in + if isEmpty child then + let + val sizes = VectorSlice.vector sizes + val nodes = VectorSlice.vector nodes + in + BRANCH (nodes, sizes) + end + else + let + val childSize = VectorSlice.full #[getFinishIdx child + prevSize] + val sizes =VectorSlice.concat [sizes, childSize] + + val childNode = VectorSlice.full #[child] + val nodes = VectorSlice.concat [nodes, childNode] + in + BRANCH (nodes, sizes) + end + end end diff --git a/todo.md b/todo.md index e50e805..ad95d36 100644 --- a/todo.md +++ b/todo.md @@ -1,2 +1,3 @@ # To-do list +- Test `PersistentVector.splitLeft` - Implement 'yj' motion and add tests for it