From 5e3e8b39b157771d07dac5476254588d2b51b02e Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 18 Jan 2026 08:52:38 +0000 Subject: [PATCH] add function in PersistentVector to insert a match while keeping same absolute indices (to be used when a new match appears after a deletion) --- fcore/persistent-vector.sml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index 6a33c76..5849bad 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -722,13 +722,20 @@ struct else let val finish = start + length + val matchBeforeStart = prevMatch (start, tree, 1) val matchBeforeStart = if matchBeforeStart >= start then ~1 else matchBeforeStart + val matchAfterFinish = nextMatch (finish, tree, 1) + val matchAfterFinish = + if matchAfterFinish < finish then + ~1 + else + matchAfterFinish in if matchBeforeStart = ~1 andalso matchAfterFinish = ~1 then empty @@ -789,6 +796,36 @@ struct end end + (* Usually, when inserting, we want the absolute metadata + * to be adjusted appropriately. + * An insertion should cause the absolute metadata to increment. + * However, we sometimes want to insert a match without adjusting + * the absolute metadata in this way. + * We want to do this when deleting some part of the buffer + * would cause a new match to be found, for example. *) + fun insertMatchKeepingAbsoluteInddices (start, finish, tree) = + let + val matchAfterFinish = nextMatch (finish, tree, 1) + in + if matchAfterFinish <= finish then + (* no match after the 'finish', so we just append *) + append (start, finish, tree) + else + let + val left = splitLeft (start, tree) + val right = splitRight (finish, tree) + + val left = append (start, finish, tree) + + val rightStartRelative = getStartIdx right + val rightStartAbsolute = rightStartRelative + finish + val difference = rightStartAbsolute - matchAfterFinish + val right = decrementBy (difference, right) + in + merge (left, right) + end + end + (* functions only for testing *) fun fromListLoop (lst, acc) = case lst of