diff --git a/src/rrb_rope.sml b/src/rrb_rope.sml index 17ffbbe..c80381a 100644 --- a/src/rrb_rope.sml +++ b/src/rrb_rope.sml @@ -21,31 +21,6 @@ struct Word.<< (w, bits) end - fun getLast tree = - case tree of - BRANCH n => getLast (Vector.sub (n, Vector.length n - 1)) - | LEAF i => Vector.sub (i, Vector.length i - 1) - - fun helpGet (key: Word.word, level, tree) = - case tree of - BRANCH nodes => - let - val w = Word.>> (key, level) - val w = Word.andb (w, mask) - val node = Vector.sub (nodes, Word.toInt w) - in - helpGet (key, level - bits, node) - end - | LEAF items => - let val idx = Word.andb (key, mask) - in Vector.sub (items, Word.toInt idx) - end - - fun get (key, {shift, root, count}: t) = - let val key = Word.fromInt key - in if key >= tailoff count then getLast root else helpGet (key, shift, root) - end - datatype append_result = UPDATE | APPEND fun helpAppend (item, tree) = @@ -95,11 +70,56 @@ struct {count = count + 1, root = root, shift = shift} end - fun mk (count, acc) = - if count = 97 then acc - else let val acc = append (count, acc) in mk (count + 1, acc) end + fun getLast tree = + case tree of + BRANCH n => getLast (Vector.sub (n, Vector.length n - 1)) + | LEAF i => Vector.sub (i, Vector.length i - 1) - val root = mk (0, empty) + fun helpGet (key: Word.word, level, tree) = + case tree of + BRANCH nodes => + let + val w = Word.>> (key, level) + val w = Word.andb (w, mask) + val node = Vector.sub (nodes, Word.toInt w) + in + helpGet (key, level - bits, node) + end + | LEAF items => + let val idx = Word.andb (key, mask) + in Vector.sub (items, Word.toInt idx) + end - val result = get (99, root) + fun get (key, {shift, root, count}: t) = + let val key = Word.fromInt key + in if key >= tailoff count then getLast root else helpGet (key, shift, root) + end + + fun splitKeepingLeft (idx, level, tree) = + case tree of + BRANCH nodes => + let + val w = Word.>> (idx, level) + val w = Word.andb (w, mask) + val nodeIdx = Word.toInt w + + val node = Vector.sub (nodes, nodeIdx) + val newNode = splitKeepingLeft (idx, level - bits, node) + val newNode = Vector.fromList [newNode] + val newNode = VectorSlice.full newNode + + val newNodes = VectorSlice.slice (nodes, 0, SOME nodeIdx) + val newNodes = VectorSlice.concat [newNodes, newNode] + in + BRANCH newNodes + end + | LEAF items => + let + val w = Word.andb (idx, mask) + val idx = Word.toInt w + val items = VectorSlice.slice (items, 0, SOME idx) + val items = VectorSlice.vector items + in + LEAF items + end end