add function to split left side of tree, given an index
This commit is contained in:
@@ -21,31 +21,6 @@ struct
|
|||||||
Word.<< (w, bits)
|
Word.<< (w, bits)
|
||||||
end
|
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
|
datatype append_result = UPDATE | APPEND
|
||||||
|
|
||||||
fun helpAppend (item, tree) =
|
fun helpAppend (item, tree) =
|
||||||
@@ -95,11 +70,56 @@ struct
|
|||||||
{count = count + 1, root = root, shift = shift}
|
{count = count + 1, root = root, shift = shift}
|
||||||
end
|
end
|
||||||
|
|
||||||
fun mk (count, acc) =
|
fun getLast tree =
|
||||||
if count = 97 then acc
|
case tree of
|
||||||
else let val acc = append (count, acc) in mk (count + 1, acc) end
|
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
|
||||||
val result = get (99, root)
|
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
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user