formatting for persistent-vector.sml (absolutely no changes other than formatting)
This commit is contained in:
@@ -104,10 +104,7 @@ struct
|
|||||||
val prevFinish = Vector.sub (sizes, Vector.length sizes - 1)
|
val prevFinish = Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
val start = start - prevFinish
|
val start = start - prevFinish
|
||||||
val finish = finish - prevFinish
|
val finish = finish - prevFinish
|
||||||
val newNode = LEAF
|
val newNode = LEAF (#[{start = start, finish = finish}], #[finish])
|
||||||
( #[{start = start, finish = finish}]
|
|
||||||
, #[finish]
|
|
||||||
)
|
|
||||||
in
|
in
|
||||||
APPEND newNode
|
APPEND newNode
|
||||||
end
|
end
|
||||||
@@ -125,11 +122,8 @@ struct
|
|||||||
case helpAppend (start, finish, tree) of
|
case helpAppend (start, finish, tree) of
|
||||||
UPDATE t => t
|
UPDATE t => t
|
||||||
| APPEND newNode =>
|
| APPEND newNode =>
|
||||||
let
|
let val maxSize = getFinishIdx tree
|
||||||
val maxSize = getFinishIdx tree
|
in BRANCH (#[tree, newNode], #[maxSize, finish])
|
||||||
in
|
|
||||||
BRANCH
|
|
||||||
(#[tree, newNode], #[maxSize, finish])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getStart tree =
|
fun getStart tree =
|
||||||
@@ -350,10 +344,8 @@ struct
|
|||||||
case tree of
|
case tree of
|
||||||
BRANCH (nodes, _) => getFirstItem (Vector.sub (nodes, 0))
|
BRANCH (nodes, _) => getFirstItem (Vector.sub (nodes, 0))
|
||||||
| LEAF (items, _) =>
|
| LEAF (items, _) =>
|
||||||
if Vector.length items = 0 then
|
if Vector.length items = 0 then {start = ~1, finish = ~1}
|
||||||
{start = ~1, finish = ~1}
|
else Vector.sub (items, 0)
|
||||||
else
|
|
||||||
Vector.sub (items, 0)
|
|
||||||
|
|
||||||
fun getMaxSize tree =
|
fun getMaxSize tree =
|
||||||
case tree of
|
case tree of
|
||||||
@@ -372,9 +364,8 @@ struct
|
|||||||
Vector.sub (sizes, Vector.length sizes - 1)
|
Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
val result =
|
val result = splitLeft (offset - prevSize, Vector.sub
|
||||||
splitLeft
|
(nodes, Vector.length nodes - 1))
|
||||||
(offset - prevSize, Vector.sub (nodes, Vector.length nodes - 1))
|
|
||||||
in
|
in
|
||||||
if isEmpty result then
|
if isEmpty result then
|
||||||
let
|
let
|
||||||
@@ -389,8 +380,10 @@ struct
|
|||||||
else
|
else
|
||||||
let
|
let
|
||||||
val newChildSize = getMaxSize result + prevSize
|
val newChildSize = getMaxSize result + prevSize
|
||||||
val sizes = Vector.update (sizes, Vector.length sizes - 1, newChildSize)
|
val sizes = Vector.update
|
||||||
val nodes = Vector.update (nodes, Vector.length nodes - 1, result)
|
(sizes, Vector.length sizes - 1, newChildSize)
|
||||||
|
val nodes = Vector.update
|
||||||
|
(nodes, Vector.length nodes - 1, result)
|
||||||
in
|
in
|
||||||
BRANCH (nodes, sizes)
|
BRANCH (nodes, sizes)
|
||||||
end
|
end
|
||||||
@@ -398,11 +391,7 @@ struct
|
|||||||
else
|
else
|
||||||
let
|
let
|
||||||
val idx = BinSearch.equalOrMore (offset, sizes)
|
val idx = BinSearch.equalOrMore (offset, sizes)
|
||||||
val prevSize =
|
val prevSize = if idx > 1 then Vector.sub (sizes, idx - 1) else 0
|
||||||
if idx > 1 then
|
|
||||||
Vector.sub (sizes, idx - 1)
|
|
||||||
else
|
|
||||||
0
|
|
||||||
val result = splitLeft (offset - prevSize, Vector.sub (nodes, idx))
|
val result = splitLeft (offset - prevSize, Vector.sub (nodes, idx))
|
||||||
in
|
in
|
||||||
if isEmpty result then
|
if isEmpty result then
|
||||||
@@ -418,20 +407,11 @@ struct
|
|||||||
else
|
else
|
||||||
let
|
let
|
||||||
val len = idx + 1
|
val len = idx + 1
|
||||||
val sizes = Vector.tabulate (len,
|
val sizes = Vector.tabulate (len, fn i =>
|
||||||
fn i =>
|
if i = idx then getMaxSize result + prevSize
|
||||||
if i = idx then
|
else Vector.sub (sizes, i))
|
||||||
getMaxSize result + prevSize
|
val nodes = Vector.tabulate (len, fn i =>
|
||||||
else
|
if i = idx then result else Vector.sub (nodes, i))
|
||||||
Vector.sub (sizes, i)
|
|
||||||
)
|
|
||||||
val nodes = Vector.tabulate (len,
|
|
||||||
fn i =>
|
|
||||||
if i = idx then
|
|
||||||
result
|
|
||||||
else
|
|
||||||
Vector.sub (nodes, i)
|
|
||||||
)
|
|
||||||
in
|
in
|
||||||
BRANCH (nodes, sizes)
|
BRANCH (nodes, sizes)
|
||||||
end
|
end
|
||||||
@@ -442,27 +422,27 @@ struct
|
|||||||
tree
|
tree
|
||||||
else if offset <= Vector.sub (sizes, 0) then
|
else if offset <= Vector.sub (sizes, 0) then
|
||||||
LEAF (#[], #[])
|
LEAF (#[], #[])
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val len = BinSearch.equalOrMore (offset, sizes)
|
val len = BinSearch.equalOrMore (offset, sizes)
|
||||||
val sizes = VectorSlice.slice (sizes, 0, SOME len)
|
val sizes = VectorSlice.slice (sizes, 0, SOME len)
|
||||||
val sizes = VectorSlice.vector sizes
|
val sizes = VectorSlice.vector sizes
|
||||||
val items = VectorSlice.slice (items, 0, SOME len)
|
val items = VectorSlice.slice (items, 0, SOME len)
|
||||||
val items = VectorSlice.vector items
|
val items = VectorSlice.vector items
|
||||||
in
|
in
|
||||||
LEAF (items, sizes)
|
LEAF (items, sizes)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
tree
|
tree
|
||||||
|
|
||||||
(* Unlike 'splitLeft' which leaves the size metadata alone
|
(* Unlike 'splitLeft' which leaves the size metadata alone
|
||||||
* (except for splitting it),
|
* (except for splitting it),
|
||||||
* we want splitRight to decrement the size metadata
|
* we want splitRight to decrement the size metadata
|
||||||
* by the largest entry in the size table that was split.
|
* by the largest entry in the size table that was split.
|
||||||
* This is so that we can maintain relative indexing metadata. *)
|
* This is so that we can maintain relative indexing metadata. *)
|
||||||
fun splitRight (offset, tree) =
|
fun splitRight (offset, tree) =
|
||||||
case tree of
|
case tree of
|
||||||
BRANCH (nodes, sizes) =>
|
BRANCH (nodes, sizes) =>
|
||||||
if offset <= Vector.sub (sizes, 0) then
|
if offset <= Vector.sub (sizes, 0) then
|
||||||
(* we want to split first node *)
|
(* we want to split first node *)
|
||||||
let
|
let
|
||||||
@@ -474,7 +454,8 @@ struct
|
|||||||
val len = Vector.length sizes - 1
|
val len = Vector.length sizes - 1
|
||||||
|
|
||||||
val sizeSlice = VectorSlice.slice (sizes, 1, SOME len)
|
val sizeSlice = VectorSlice.slice (sizes, 1, SOME len)
|
||||||
val sizes = VectorSlice.map (fn el => el - firstSizeBefore) sizeSlice
|
val sizes =
|
||||||
|
VectorSlice.map (fn el => el - firstSizeBefore) sizeSlice
|
||||||
|
|
||||||
val nodeSlice = VectorSlice.slice (nodes, 1, SOME len)
|
val nodeSlice = VectorSlice.slice (nodes, 1, SOME len)
|
||||||
val nodes = VectorSlice.vector nodeSlice
|
val nodes = VectorSlice.vector nodeSlice
|
||||||
@@ -486,12 +467,10 @@ struct
|
|||||||
val firstSizeAfter = getMaxSize result
|
val firstSizeAfter = getMaxSize result
|
||||||
val sizeDiff = firstSizeBefore - firstSizeAfter
|
val sizeDiff = firstSizeBefore - firstSizeAfter
|
||||||
|
|
||||||
val sizes = Vector.mapi (fn (idx, el) =>
|
val sizes =
|
||||||
if idx = 0 then
|
Vector.mapi
|
||||||
firstSizeAfter
|
(fn (idx, el) =>
|
||||||
else
|
if idx = 0 then firstSizeAfter else el - sizeDiff) sizes
|
||||||
el - sizeDiff
|
|
||||||
) sizes
|
|
||||||
|
|
||||||
val nodes = Vector.update (nodes, 0, result)
|
val nodes = Vector.update (nodes, 0, result)
|
||||||
in
|
in
|
||||||
@@ -536,351 +515,332 @@ struct
|
|||||||
BRANCH (nodes, sizes)
|
BRANCH (nodes, sizes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
| LEAF (items, sizes) =>
|
| LEAF (items, sizes) =>
|
||||||
if offset > Vector.sub (sizes, Vector.length sizes - 1) then
|
if offset > Vector.sub (sizes, Vector.length sizes - 1) then
|
||||||
LEAF (#[], #[])
|
LEAF (#[], #[])
|
||||||
else if offset <= Vector.sub (sizes, 0) then
|
else if offset <= Vector.sub (sizes, 0) then
|
||||||
tree
|
tree
|
||||||
else
|
else
|
||||||
let
|
|
||||||
val idx = BinSearch.equalOrMore (offset, sizes)
|
|
||||||
val len = Vector.length sizes - idx
|
|
||||||
val len = SOME len
|
|
||||||
|
|
||||||
val prevSize =
|
|
||||||
if idx < 1 then
|
|
||||||
0
|
|
||||||
else
|
|
||||||
Vector.sub (sizes, idx - 1)
|
|
||||||
val sizes = VectorSlice.slice (sizes, idx, len)
|
|
||||||
val sizes = VectorSlice.map (fn i => i - prevSize) sizes
|
|
||||||
|
|
||||||
val items = VectorSlice.slice (items, idx, len)
|
|
||||||
val items =
|
|
||||||
VectorSlice.map
|
|
||||||
(fn {start, finish} =>
|
|
||||||
{start = start - prevSize, finish = finish - prevSize})
|
|
||||||
items
|
|
||||||
in
|
|
||||||
LEAF (items, sizes)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun getDepth (tree, acc) =
|
|
||||||
case tree of
|
|
||||||
BRANCH (nodes, _) => getDepth (Vector.sub (nodes, 0), acc + 1)
|
|
||||||
| LEAF (_, _) => acc
|
|
||||||
|
|
||||||
fun getRootVecLength tree =
|
|
||||||
case tree of
|
|
||||||
BRANCH (_, sizes) => Vector.length sizes
|
|
||||||
| LEAF (_, sizes) => Vector.length sizes
|
|
||||||
|
|
||||||
fun joinSameDepth (left, right) =
|
|
||||||
case (left, right) of
|
|
||||||
(BRANCH (leftNodes, leftSizes), BRANCH (rightNodes, rightSizes)) =>
|
|
||||||
let
|
let
|
||||||
val nodes = Vector.concat [leftNodes, rightNodes]
|
val idx = BinSearch.equalOrMore (offset, sizes)
|
||||||
val maxLeftSize = Vector.sub (leftSizes, Vector.length leftSizes - 1)
|
val len = Vector.length sizes - idx
|
||||||
val sizes = Vector.tabulate
|
val len = SOME len
|
||||||
(Vector.length leftSizes + Vector.length rightSizes,
|
|
||||||
fn i =>
|
val prevSize = if idx < 1 then 0 else Vector.sub (sizes, idx - 1)
|
||||||
if i < Vector.length leftSizes then
|
val sizes = VectorSlice.slice (sizes, idx, len)
|
||||||
Vector.sub (leftSizes, i)
|
val sizes = VectorSlice.map (fn i => i - prevSize) sizes
|
||||||
else
|
|
||||||
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
val items = VectorSlice.slice (items, idx, len)
|
||||||
+ maxLeftSize
|
|
||||||
)
|
|
||||||
in
|
|
||||||
BRANCH (nodes, sizes)
|
|
||||||
end
|
|
||||||
| (LEAF (leftItems, leftSizes), LEAF (rightItems, rightSizes)) =>
|
|
||||||
let
|
|
||||||
val maxLeftSize = Vector.sub (leftSizes, Vector.length leftSizes - 1)
|
|
||||||
val len = Vector.length leftItems + Vector.length rightItems
|
|
||||||
val items =
|
val items =
|
||||||
Vector.tabulate (len,
|
VectorSlice.map
|
||||||
fn i =>
|
(fn {start, finish} =>
|
||||||
if i < Vector.length leftItems then
|
{start = start - prevSize, finish = finish - prevSize}) items
|
||||||
Vector.sub (leftItems, i)
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val {start, finish} = Vector.sub (rightItems, i - Vector.length leftItems)
|
|
||||||
in
|
|
||||||
{start = start + maxLeftSize, finish = finish + maxLeftSize}
|
|
||||||
end
|
|
||||||
)
|
|
||||||
val sizes =
|
|
||||||
Vector.tabulate (len,
|
|
||||||
fn i =>
|
|
||||||
if i < Vector.length leftSizes then
|
|
||||||
Vector.sub (leftSizes, i)
|
|
||||||
else
|
|
||||||
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
|
||||||
+ maxLeftSize
|
|
||||||
)
|
|
||||||
in
|
in
|
||||||
LEAF (items, sizes)
|
LEAF (items, sizes)
|
||||||
end
|
end
|
||||||
| _ => raise Fail "PersistentVector.joinSameDepth: one is BRANCH and other is LEAF"
|
|
||||||
|
|
||||||
datatype append_join_result =
|
fun getDepth (tree, acc) =
|
||||||
APPEND_JOIN_APPEND of t
|
case tree of
|
||||||
| APPEND_JOIN_UPDATE of t
|
BRANCH (nodes, _) => getDepth (Vector.sub (nodes, 0), acc + 1)
|
||||||
|
| LEAF (_, _) => acc
|
||||||
|
|
||||||
fun appendJoin (left, right, joinDepth, rightLength) =
|
fun getRootVecLength tree =
|
||||||
case left of
|
case tree of
|
||||||
BRANCH (nodes, sizes) =>
|
BRANCH (_, sizes) => Vector.length sizes
|
||||||
if joinDepth = 0 then
|
| LEAF (_, sizes) => Vector.length sizes
|
||||||
(* base case: should join at this depth *)
|
|
||||||
if Vector.length nodes + rightLength > maxSize then
|
|
||||||
APPEND_JOIN_APPEND right
|
|
||||||
else
|
|
||||||
(case right of
|
|
||||||
BRANCH (rightNodes, rightSizes) =>
|
|
||||||
let
|
|
||||||
val nodes = Vector.concat [nodes, rightNodes]
|
|
||||||
|
|
||||||
val lastLeftSize = Vector.sub (sizes, Vector.length sizes - 1)
|
fun joinSameDepth (left, right) =
|
||||||
val sizes = Vector.tabulate (Vector.length nodes,
|
case (left, right) of
|
||||||
fn i =>
|
(BRANCH (leftNodes, leftSizes), BRANCH (rightNodes, rightSizes)) =>
|
||||||
if i < Vector.length sizes then
|
let
|
||||||
Vector.sub (sizes, i)
|
val nodes = Vector.concat [leftNodes, rightNodes]
|
||||||
else
|
val maxLeftSize = Vector.sub (leftSizes, Vector.length leftSizes - 1)
|
||||||
Vector.sub (rightSizes, i - Vector.length sizes)
|
val sizes =
|
||||||
+ lastLeftSize
|
Vector.tabulate
|
||||||
)
|
( Vector.length leftSizes + Vector.length rightSizes
|
||||||
in
|
, fn i =>
|
||||||
APPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
if i < Vector.length leftSizes then
|
||||||
end
|
Vector.sub (leftSizes, i)
|
||||||
| LEAF _ =>
|
|
||||||
raise Fail
|
|
||||||
"persistent-vector.sml appendJoin: \
|
|
||||||
\expected to join when left and right are both BRANCH \
|
|
||||||
\but left is BRANCH and right is LEAF"
|
|
||||||
)
|
|
||||||
else
|
|
||||||
(* recursion case: join below *)
|
|
||||||
let
|
|
||||||
val lastIdx = Vector.length nodes - 1
|
|
||||||
val lastNode = Vector.sub (nodes, lastIdx)
|
|
||||||
in
|
|
||||||
case appendJoin (lastNode, right, joinDepth - 1, rightLength) of
|
|
||||||
APPEND_JOIN_UPDATE newLast =>
|
|
||||||
let
|
|
||||||
val prevSize =
|
|
||||||
if lastIdx > 0 then Vector.sub (sizes, lastIdx - 1)
|
|
||||||
else 0
|
|
||||||
val newLastSize = getMaxSize newLast + prevSize
|
|
||||||
val sizes = Vector.update (sizes, lastIdx, newLastSize)
|
|
||||||
val nodes = Vector.update (nodes, lastIdx, newLast)
|
|
||||||
in
|
|
||||||
APPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
|
||||||
end
|
|
||||||
| APPEND_JOIN_APPEND newNode =>
|
|
||||||
if Vector.length nodes = maxSize then
|
|
||||||
(* parent has to append insead as this node
|
|
||||||
* would exceed capacity if appended here *)
|
|
||||||
APPEND_JOIN_APPEND (BRANCH (#[newNode], #[getMaxSize newNode]))
|
|
||||||
else
|
else
|
||||||
let
|
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
||||||
val prevSize =
|
+ maxLeftSize
|
||||||
Vector.sub (sizes, Vector.length sizes - 1)
|
)
|
||||||
val newNodeSize = #[getMaxSize newNode + prevSize]
|
in
|
||||||
val sizes = Vector.concat [sizes, newNodeSize]
|
BRANCH (nodes, sizes)
|
||||||
|
end
|
||||||
|
| (LEAF (leftItems, leftSizes), LEAF (rightItems, rightSizes)) =>
|
||||||
|
let
|
||||||
|
val maxLeftSize = Vector.sub (leftSizes, Vector.length leftSizes - 1)
|
||||||
|
val len = Vector.length leftItems + Vector.length rightItems
|
||||||
|
val items = Vector.tabulate (len, fn i =>
|
||||||
|
if i < Vector.length leftItems then
|
||||||
|
Vector.sub (leftItems, i)
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val {start, finish} = Vector.sub
|
||||||
|
(rightItems, i - Vector.length leftItems)
|
||||||
|
in
|
||||||
|
{start = start + maxLeftSize, finish = finish + maxLeftSize}
|
||||||
|
end)
|
||||||
|
val sizes = Vector.tabulate (len, fn i =>
|
||||||
|
if i < Vector.length leftSizes then
|
||||||
|
Vector.sub (leftSizes, i)
|
||||||
|
else
|
||||||
|
Vector.sub (rightSizes, i - Vector.length leftSizes) + maxLeftSize)
|
||||||
|
in
|
||||||
|
LEAF (items, sizes)
|
||||||
|
end
|
||||||
|
| _ =>
|
||||||
|
raise Fail
|
||||||
|
"PersistentVector.joinSameDepth: one is BRANCH and other is LEAF"
|
||||||
|
|
||||||
val newNode = #[newNode]
|
datatype append_join_result =
|
||||||
val nodes = Vector.concat [nodes, newNode]
|
APPEND_JOIN_APPEND of t
|
||||||
in
|
| APPEND_JOIN_UPDATE of t
|
||||||
APPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
|
||||||
end
|
fun appendJoin (left, right, joinDepth, rightLength) =
|
||||||
end
|
case left of
|
||||||
| LEAF (items, sizes) =>
|
BRANCH (nodes, sizes) =>
|
||||||
(* joinDepth should = 0, and we assume it is *)
|
if joinDepth = 0 then
|
||||||
if Vector.length items + rightLength > maxSize then
|
(* base case: should join at this depth *)
|
||||||
|
if Vector.length nodes + rightLength > maxSize then
|
||||||
APPEND_JOIN_APPEND right
|
APPEND_JOIN_APPEND right
|
||||||
else
|
else
|
||||||
(case right of
|
(case right of
|
||||||
LEAF (rightItems, rightSizes) =>
|
BRANCH (rightNodes, rightSizes) =>
|
||||||
|
let
|
||||||
|
val nodes = Vector.concat [nodes, rightNodes]
|
||||||
|
|
||||||
|
val lastLeftSize = Vector.sub
|
||||||
|
(sizes, Vector.length sizes - 1)
|
||||||
|
val sizes = Vector.tabulate (Vector.length nodes, fn i =>
|
||||||
|
if i < Vector.length sizes then
|
||||||
|
Vector.sub (sizes, i)
|
||||||
|
else
|
||||||
|
Vector.sub (rightSizes, i - Vector.length sizes)
|
||||||
|
+ lastLeftSize)
|
||||||
|
in
|
||||||
|
APPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
||||||
|
end
|
||||||
|
| LEAF _ =>
|
||||||
|
raise Fail
|
||||||
|
"persistent-vector.sml appendJoin: \
|
||||||
|
\expected to join when left and right are both BRANCH \
|
||||||
|
\but left is BRANCH and right is LEAF")
|
||||||
|
else
|
||||||
|
(* recursion case: join below *)
|
||||||
|
let
|
||||||
|
val lastIdx = Vector.length nodes - 1
|
||||||
|
val lastNode = Vector.sub (nodes, lastIdx)
|
||||||
|
in
|
||||||
|
case appendJoin (lastNode, right, joinDepth - 1, rightLength) of
|
||||||
|
APPEND_JOIN_UPDATE newLast =>
|
||||||
let
|
let
|
||||||
val leftMaxSize = Vector.sub (sizes, Vector.length sizes - 1)
|
val prevSize =
|
||||||
|
if lastIdx > 0 then Vector.sub (sizes, lastIdx - 1) else 0
|
||||||
val newLen = Vector.length items + Vector.length rightItems
|
val newLastSize = getMaxSize newLast + prevSize
|
||||||
val items = Vector.tabulate (newLen,
|
val sizes = Vector.update (sizes, lastIdx, newLastSize)
|
||||||
fn i =>
|
val nodes = Vector.update (nodes, lastIdx, newLast)
|
||||||
if i < Vector.length items then
|
|
||||||
Vector.sub (items, i)
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val {start, finish} = Vector.sub (rightItems, i - Vector.length items)
|
|
||||||
in
|
|
||||||
{start = start + leftMaxSize, finish = finish + leftMaxSize}
|
|
||||||
end
|
|
||||||
)
|
|
||||||
val sizes = Vector.tabulate (newLen,
|
|
||||||
fn i =>
|
|
||||||
if i < Vector.length sizes then
|
|
||||||
Vector.sub (sizes, i)
|
|
||||||
else
|
|
||||||
Vector.sub (rightSizes, i - Vector.length sizes)
|
|
||||||
+ leftMaxSize
|
|
||||||
)
|
|
||||||
in
|
in
|
||||||
APPEND_JOIN_UPDATE (LEAF (items, sizes))
|
APPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
||||||
end
|
end
|
||||||
| BRANCH _ =>
|
| APPEND_JOIN_APPEND newNode =>
|
||||||
raise Fail
|
if Vector.length nodes = maxSize then
|
||||||
"persistent-vector.sml appendJoin: \
|
(* parent has to append insead as this node
|
||||||
\left is LEAF and expected right to also be LEAF \
|
* would exceed capacity if appended here *)
|
||||||
\but right is BRANCH"
|
APPEND_JOIN_APPEND (BRANCH
|
||||||
)
|
(#[newNode], #[getMaxSize newNode]))
|
||||||
|
else
|
||||||
datatype prepend_join_result =
|
|
||||||
PREPEND_JOIN_PREPEND of t
|
|
||||||
| PREPEND_JOIN_UPDATE of t
|
|
||||||
|
|
||||||
fun prependJoin (left, right, joinDepth, leftLength) =
|
|
||||||
case right of
|
|
||||||
BRANCH (rightNodes, rightSizes) =>
|
|
||||||
if joinDepth = 0 then
|
|
||||||
(* base case: join *)
|
|
||||||
if Vector.length rightNodes + leftLength > maxSize then
|
|
||||||
PREPEND_JOIN_PREPEND left
|
|
||||||
else
|
|
||||||
(case left of
|
|
||||||
BRANCH (leftNodes, leftSizes) =>
|
|
||||||
let
|
let
|
||||||
val nodes = Vector.concat [leftNodes, rightNodes]
|
val prevSize = Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
val maxLeftSize =
|
val newNodeSize = #[getMaxSize newNode + prevSize]
|
||||||
Vector.sub (leftSizes, Vector.length leftSizes - 1)
|
val sizes = Vector.concat [sizes, newNodeSize]
|
||||||
val sizes = Vector.tabulate (Vector.length nodes,
|
|
||||||
fn i =>
|
|
||||||
if i < Vector.length leftSizes then
|
|
||||||
Vector.sub (leftSizes, i)
|
|
||||||
else
|
|
||||||
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
|
||||||
+ maxLeftSize
|
|
||||||
)
|
|
||||||
in
|
|
||||||
PREPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
|
||||||
end
|
|
||||||
| LEAF _ =>
|
|
||||||
raise Fail
|
|
||||||
"persistent-vector.sml prependJoin: \
|
|
||||||
\expected left and right to be BRANCH \
|
|
||||||
\but right is BRANCH while left is LEAF"
|
|
||||||
)
|
|
||||||
else
|
|
||||||
(* recursive case *)
|
|
||||||
let
|
|
||||||
val firstRightNode = Vector.sub (rightNodes, 0)
|
|
||||||
val firstRightSize = getMaxSize firstRightNode
|
|
||||||
in
|
|
||||||
(case prependJoin (left, firstRightNode, joinDepth - 1, leftLength) of
|
|
||||||
PREPEND_JOIN_UPDATE newFirst =>
|
|
||||||
let
|
|
||||||
val newFirstSize = getMaxSize newFirst
|
|
||||||
val sizeDiff = newFirstSize - firstRightSize
|
|
||||||
val sizes = Vector.map (fn el => el + sizeDiff) rightSizes
|
|
||||||
val nodes = Vector.update (rightNodes, 0, newFirst)
|
|
||||||
in
|
|
||||||
PREPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
|
||||||
end
|
|
||||||
| PREPEND_JOIN_PREPEND newFirst =>
|
|
||||||
if Vector.length rightSizes = maxSize then
|
|
||||||
PREPEND_JOIN_PREPEND (BRANCH (#[newFirst], #[getMaxSize newFirst]))
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val nodes = Vector.concat [#[newFirst], rightNodes]
|
|
||||||
val newFirstSize = getMaxSize newFirst
|
|
||||||
|
|
||||||
val sizes = Vector.tabulate (Vector.length nodes,
|
val newNode = #[newNode]
|
||||||
fn i =>
|
val nodes = Vector.concat [nodes, newNode]
|
||||||
if i = 0 then
|
in
|
||||||
newFirstSize
|
APPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
||||||
else
|
end
|
||||||
Vector.sub (rightSizes, i - 1) + newFirstSize
|
end
|
||||||
)
|
| LEAF (items, sizes) =>
|
||||||
in
|
(* joinDepth should = 0, and we assume it is *)
|
||||||
PREPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
if Vector.length items + rightLength > maxSize then
|
||||||
end)
|
APPEND_JOIN_APPEND right
|
||||||
end
|
else
|
||||||
| LEAF (rightItems, rightSizes) =>
|
(case right of
|
||||||
(* joinDepth should = 0, and we assume it is *)
|
LEAF (rightItems, rightSizes) =>
|
||||||
if Vector.length rightItems + leftLength > maxSize then
|
let
|
||||||
|
val leftMaxSize = Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
|
|
||||||
|
val newLen = Vector.length items + Vector.length rightItems
|
||||||
|
val items = Vector.tabulate (newLen, fn i =>
|
||||||
|
if i < Vector.length items then
|
||||||
|
Vector.sub (items, i)
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val {start, finish} = Vector.sub
|
||||||
|
(rightItems, i - Vector.length items)
|
||||||
|
in
|
||||||
|
{ start = start + leftMaxSize
|
||||||
|
, finish = finish + leftMaxSize
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
val sizes = Vector.tabulate (newLen, fn i =>
|
||||||
|
if i < Vector.length sizes then
|
||||||
|
Vector.sub (sizes, i)
|
||||||
|
else
|
||||||
|
Vector.sub (rightSizes, i - Vector.length sizes)
|
||||||
|
+ leftMaxSize)
|
||||||
|
in
|
||||||
|
APPEND_JOIN_UPDATE (LEAF (items, sizes))
|
||||||
|
end
|
||||||
|
| BRANCH _ =>
|
||||||
|
raise Fail
|
||||||
|
"persistent-vector.sml appendJoin: \
|
||||||
|
\left is LEAF and expected right to also be LEAF \
|
||||||
|
\but right is BRANCH")
|
||||||
|
|
||||||
|
datatype prepend_join_result =
|
||||||
|
PREPEND_JOIN_PREPEND of t
|
||||||
|
| PREPEND_JOIN_UPDATE of t
|
||||||
|
|
||||||
|
fun prependJoin (left, right, joinDepth, leftLength) =
|
||||||
|
case right of
|
||||||
|
BRANCH (rightNodes, rightSizes) =>
|
||||||
|
if joinDepth = 0 then
|
||||||
|
(* base case: join *)
|
||||||
|
if Vector.length rightNodes + leftLength > maxSize then
|
||||||
PREPEND_JOIN_PREPEND left
|
PREPEND_JOIN_PREPEND left
|
||||||
else
|
else
|
||||||
(case left of
|
(case left of
|
||||||
LEAF (leftItems, leftSizes) =>
|
BRANCH (leftNodes, leftSizes) =>
|
||||||
let
|
let
|
||||||
val maxLeftSize = Vector.sub (leftSizes, Vector.length leftSizes - 1)
|
val nodes = Vector.concat [leftNodes, rightNodes]
|
||||||
val len = Vector.length leftSizes + Vector.length rightItems
|
val maxLeftSize = Vector.sub
|
||||||
val sizes = Vector.tabulate (len,
|
(leftSizes, Vector.length leftSizes - 1)
|
||||||
fn i =>
|
val sizes = Vector.tabulate (Vector.length nodes, fn i =>
|
||||||
if i < Vector.length leftSizes then
|
if i < Vector.length leftSizes then
|
||||||
Vector.sub (leftSizes, i)
|
Vector.sub (leftSizes, i)
|
||||||
else
|
else
|
||||||
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
||||||
+ maxLeftSize)
|
+ maxLeftSize)
|
||||||
val items = Vector.tabulate (len,
|
in
|
||||||
fn i =>
|
PREPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
||||||
if i < Vector.length leftItems then
|
end
|
||||||
Vector.sub (leftItems, i)
|
| LEAF _ =>
|
||||||
else
|
raise Fail
|
||||||
let
|
"persistent-vector.sml prependJoin: \
|
||||||
val {start, finish} =
|
\expected left and right to be BRANCH \
|
||||||
Vector.sub (rightItems, i - Vector.length leftItems)
|
\but right is BRANCH while left is LEAF")
|
||||||
in
|
else
|
||||||
{start = start + maxLeftSize, finish = finish + maxLeftSize}
|
(* recursive case *)
|
||||||
end
|
let
|
||||||
)
|
val firstRightNode = Vector.sub (rightNodes, 0)
|
||||||
in
|
val firstRightSize = getMaxSize firstRightNode
|
||||||
PREPEND_JOIN_UPDATE (LEAF (items, sizes))
|
in
|
||||||
end
|
(case prependJoin (left, firstRightNode, joinDepth - 1, leftLength) of
|
||||||
| BRANCH _ =>
|
PREPEND_JOIN_UPDATE newFirst =>
|
||||||
raise Fail
|
let
|
||||||
"persistent-vector.sml prependJoin: \
|
val newFirstSize = getMaxSize newFirst
|
||||||
\expected left and right to be LEAF \
|
val sizeDiff = newFirstSize - firstRightSize
|
||||||
\but right is LEAF while left is BRANCH")
|
val sizes = Vector.map (fn el => el + sizeDiff) rightSizes
|
||||||
|
val nodes = Vector.update (rightNodes, 0, newFirst)
|
||||||
|
in
|
||||||
|
PREPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
||||||
|
end
|
||||||
|
| PREPEND_JOIN_PREPEND newFirst =>
|
||||||
|
if Vector.length rightSizes = maxSize then
|
||||||
|
PREPEND_JOIN_PREPEND (BRANCH
|
||||||
|
(#[newFirst], #[getMaxSize newFirst]))
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val nodes = Vector.concat [#[newFirst], rightNodes]
|
||||||
|
val newFirstSize = getMaxSize newFirst
|
||||||
|
|
||||||
fun join (left, right) =
|
val sizes = Vector.tabulate (Vector.length nodes, fn i =>
|
||||||
if isEmpty left then
|
if i = 0 then newFirstSize
|
||||||
right
|
else Vector.sub (rightSizes, i - 1) + newFirstSize)
|
||||||
else if isEmpty right then
|
in
|
||||||
left
|
PREPEND_JOIN_UPDATE (BRANCH (nodes, sizes))
|
||||||
else
|
end)
|
||||||
let
|
end
|
||||||
val leftDepth = getDepth (left, 0)
|
| LEAF (rightItems, rightSizes) =>
|
||||||
val rightDepth = getDepth (right, 0)
|
(* joinDepth should = 0, and we assume it is *)
|
||||||
in
|
if Vector.length rightItems + leftLength > maxSize then
|
||||||
if leftDepth = rightDepth then
|
PREPEND_JOIN_PREPEND left
|
||||||
if getRootVecLength left + getRootVecLength right <= maxSize then
|
else
|
||||||
joinSameDepth (left, right)
|
(case left of
|
||||||
else
|
LEAF (leftItems, leftSizes) =>
|
||||||
let
|
let
|
||||||
val ls = getMaxSize left
|
val maxLeftSize = Vector.sub
|
||||||
val rs = getMaxSize right + ls
|
(leftSizes, Vector.length leftSizes - 1)
|
||||||
val sizes = #[ls, rs]
|
val len = Vector.length leftSizes + Vector.length rightItems
|
||||||
val nodes = #[left, right]
|
val sizes = Vector.tabulate (len, fn i =>
|
||||||
in
|
if i < Vector.length leftSizes then
|
||||||
BRANCH (nodes, sizes)
|
Vector.sub (leftSizes, i)
|
||||||
end
|
else
|
||||||
else if leftDepth > rightDepth then
|
Vector.sub (rightSizes, i - Vector.length leftSizes)
|
||||||
|
+ maxLeftSize)
|
||||||
|
val items = Vector.tabulate (len, fn i =>
|
||||||
|
if i < Vector.length leftItems then
|
||||||
|
Vector.sub (leftItems, i)
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val {start, finish} = Vector.sub
|
||||||
|
(rightItems, i - Vector.length leftItems)
|
||||||
|
in
|
||||||
|
{ start = start + maxLeftSize
|
||||||
|
, finish = finish + maxLeftSize
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
in
|
||||||
|
PREPEND_JOIN_UPDATE (LEAF (items, sizes))
|
||||||
|
end
|
||||||
|
| BRANCH _ =>
|
||||||
|
raise Fail
|
||||||
|
"persistent-vector.sml prependJoin: \
|
||||||
|
\expected left and right to be LEAF \
|
||||||
|
\but right is LEAF while left is BRANCH")
|
||||||
|
|
||||||
|
fun join (left, right) =
|
||||||
|
if isEmpty left then
|
||||||
|
right
|
||||||
|
else if isEmpty right then
|
||||||
|
left
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val leftDepth = getDepth (left, 0)
|
||||||
|
val rightDepth = getDepth (right, 0)
|
||||||
|
in
|
||||||
|
if leftDepth = rightDepth then
|
||||||
|
if getRootVecLength left + getRootVecLength right <= maxSize then
|
||||||
|
joinSameDepth (left, right)
|
||||||
|
else
|
||||||
let
|
let
|
||||||
val joinDepth = leftDepth - rightDepth
|
val ls = getMaxSize left
|
||||||
val rightLength = getRootVecLength right
|
val rs = getMaxSize right + ls
|
||||||
|
val sizes = #[ls, rs]
|
||||||
|
val nodes = #[left, right]
|
||||||
in
|
in
|
||||||
case appendJoin (left, right, joinDepth, rightLength) of
|
BRANCH (nodes, sizes)
|
||||||
APPEND_JOIN_UPDATE t => t
|
|
||||||
| APPEND_JOIN_APPEND newRight =>
|
|
||||||
let
|
|
||||||
val ls = getMaxSize left
|
|
||||||
val rs = getMaxSize right + ls
|
|
||||||
val sizes = #[ls, rs]
|
|
||||||
val nodes = #[left, newRight]
|
|
||||||
in
|
|
||||||
BRANCH (nodes, sizes)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
else if leftDepth > rightDepth then
|
||||||
|
let
|
||||||
|
val joinDepth = leftDepth - rightDepth
|
||||||
|
val rightLength = getRootVecLength right
|
||||||
|
in
|
||||||
|
case appendJoin (left, right, joinDepth, rightLength) of
|
||||||
|
APPEND_JOIN_UPDATE t => t
|
||||||
|
| APPEND_JOIN_APPEND newRight =>
|
||||||
|
let
|
||||||
|
val ls = getMaxSize left
|
||||||
|
val rs = getMaxSize right + ls
|
||||||
|
val sizes = #[ls, rs]
|
||||||
|
val nodes = #[left, newRight]
|
||||||
|
in
|
||||||
|
BRANCH (nodes, sizes)
|
||||||
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val joinDepth = rightDepth - leftDepth
|
val joinDepth = rightDepth - leftDepth
|
||||||
|
|||||||
Reference in New Issue
Block a user