finish initial implementation for 'PersistentVector.splitRight'

This commit is contained in:
2026-01-16 22:16:44 +00:00
parent eb316d7be2
commit ddcb0ea56d

View File

@@ -364,6 +364,9 @@ struct
end end
end end
| BRANCH (nodes, sizes) => | BRANCH (nodes, sizes) =>
if Vector.length nodes = 0 then
tree
else
if splitIdx < Vector.sub (sizes, 0) then if splitIdx < Vector.sub (sizes, 0) then
(* we want to split first node from rest *) (* we want to split first node from rest *)
splitLeft (splitIdx, Vector.sub (nodes, 0)) splitLeft (splitIdx, Vector.sub (nodes, 0))
@@ -462,6 +465,40 @@ struct
BRANCH (nodes, sizes) BRANCH (nodes, sizes)
end end
end end
| LEAF (items, sizes) =>
if Vector.length items = 0 then
tree
else
if splitIdx > Vector.sub (sizes, Vector.length sizes - 1) then
empty
else if splitIdx < Vector.sub (sizes, 0) then
tree
else
let
val idx = BinSearch.equalOrMore (splitIdx, sizes)
val {start, finish} = Vector.sub (items, idx)
val idx =
if splitIdx >= start then
idx + 1
else
idx
in
if idx >= Vector.length items then
empty
else
let
val len = Vector.length items - idx
val itemsSlice = VectorSlice.slice (items, idx, SOME len)
val items = VectorSlice.map
(fn {start, finish} =>
{start = start - splitIdx, finish = finish - splitIdx}
)
itemsSlice
val sizes = Vector.map #finish items
in
LEAF (items, sizes)
end
end
(* functions only for testing *) (* functions only for testing *)
fun fromListLoop (lst, acc) = fun fromListLoop (lst, acc) =