From 22aa18699e92760b28d0a3607567ddebd9fbe24c Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 3 Dec 2025 10:18:44 +0000 Subject: [PATCH] modify PersistentVector.nextMatch and relevant functions so that they index using the roles of rope-like metadata --- fcore/persistent-vector.sml | 40 ++++++++++++++++++++++++++++++------- temp.txt | 4 +++- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index d5e1524..47acf28 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -131,29 +131,42 @@ struct BRANCH (#[tree, newNode], #[maxSize, finish]) end - (* todo: modify below functions so that they also - * use rope-like metadata *) - fun getStart tree = case tree of LEAF (values, _) => Vector.sub (values, 0) | BRANCH (nodes, _) => getStart (Vector.sub (nodes, 0)) - fun helpNextMatch (cursorIdx, tree) = + fun helpNextMatch (cursorIdx, tree, acc) = case tree of LEAF (values, sizes) => let val idx = BinSearch.equalOrMore (cursorIdx, sizes) in if idx = ~1 then {start = ~1, finish = ~1} - else Vector.sub (values, idx) + else + let + val {start, finish} = Vector.sub (values, idx) + in + {start = start + acc, finish = finish + acc} + end end | BRANCH (nodes, sizes) => let val idx = BinSearch.equalOrMore (cursorIdx, sizes) in if idx = ~1 then {start = ~1, finish = ~1} - else helpNextMatch (cursorIdx, Vector.sub (nodes, idx)) + else + let + val prevSize = + if idx = 0 then + 0 + else + Vector.sub (sizes, idx - 1) + val acc = acc + prevSize + val cursorIdx = cursorIdx - prevSize + in + helpNextMatch (cursorIdx, Vector.sub (nodes, idx), acc) + end end fun startNextMatch (cursorIdx, tree) = @@ -173,7 +186,17 @@ struct val idx = BinSearch.equalOrMore (cursorIdx, sizes) in if idx = ~1 then {start = ~1, finish = ~1} - else helpNextMatch (cursorIdx, Vector.sub (nodes, idx)) + else + let + val prevSize = + if idx = 0 then + 0 + else + Vector.sub (sizes, idx - 1) + val cursorIdx = cursorIdx - prevSize + in + helpNextMatch (cursorIdx, Vector.sub (nodes, idx), prevSize) + end end fun loopNextMatch (prevStart, prevFinish, tree, count) = @@ -208,6 +231,9 @@ struct loopNextMatch (start, finish, tree, count - 1) end + (* todo: modify below functions so that they also + * use rope-like metadata *) + fun getLast tree = case tree of LEAF (values, _) => Vector.sub (values, Vector.length values - 1) diff --git a/temp.txt b/temp.txt index b0e341c..6215641 100644 --- a/temp.txt +++ b/temp.txt @@ -1 +1,3 @@ -999999999999999999999999999999991111111111111111111111111111111122222222222222222222222222222222 +99999999999999999999999999999999 +11111111111111111111111111111111 +22222222222222222222222222222222