From 2058a570263e77e3e0a7f31fea6d8751d4c5007a Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 15 Jan 2026 07:16:04 +0000 Subject: [PATCH] add functions for testing persistent-vector.sml --- fcore/persistent-vector.sml | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index 89ff765..e270386 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -404,4 +404,61 @@ struct BRANCH (nodes, sizes) end end + + (* functions only for testing *) + fun fromListLoop (lst, acc) = + case lst of + {start, finish} :: tl => + let + val acc = append (start, finish, acc) + in + fromListLoop (tl, acc) + end + | [] => acc + + fun fromList coords = fromListLoop (coords, empty) + + fun toListLoop (tree, acc) = + case tree of + BRANCH (nodes, _) => + let + fun branchLoop (pos, acc) = + if pos = Vector.length nodes then + acc + else + let + val acc = toListLoop (Vector.sub (nodes, pos), acc) + in + branchLoop (pos + 1, acc) + end + in + branchLoop (0, acc) + end + | LEAF (items, _) => + let + fun itemLoop (pos, acc, offset) = + if pos = Vector.length items then + acc + else + let + val {start, finish} = Vector.sub (items, pos) + val item = {start = start + offset, finish = finish + offset} + in + itemLoop (pos + 1, item :: acc, offset) + end + + val offset = + case acc of + {finish, ...} :: _ => finish + | [] => 0 + in + itemLoop (0, acc, offset) + end + + fun toList tree = + let + val result = toListLoop (tree, []) + in + List.rev result + end end