formatting for fcore/persistent-vector.sml
This commit is contained in:
@@ -32,10 +32,8 @@ struct
|
|||||||
else if searchIdx = 0 then
|
else if searchIdx = 0 then
|
||||||
isInRange (checkIdx, Vector.sub (nodes, searchIdx))
|
isInRange (checkIdx, Vector.sub (nodes, searchIdx))
|
||||||
else
|
else
|
||||||
let
|
let val nextCheckIdx = checkIdx - Vector.sub (sizes, searchIdx - 1)
|
||||||
val nextCheckIdx = checkIdx - Vector.sub (sizes, searchIdx - 1)
|
in isInRange (nextCheckIdx, Vector.sub (nodes, searchIdx))
|
||||||
in
|
|
||||||
isInRange (nextCheckIdx, Vector.sub (nodes, searchIdx))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
| LEAF (values, sizes) =>
|
| LEAF (values, sizes) =>
|
||||||
@@ -45,10 +43,8 @@ struct
|
|||||||
if searchIdx = ~1 then
|
if searchIdx = ~1 then
|
||||||
false
|
false
|
||||||
else
|
else
|
||||||
let
|
let val {start, finish} = Vector.sub (values, searchIdx)
|
||||||
val {start, finish} = Vector.sub (values, searchIdx)
|
in checkIdx >= start andalso checkIdx <= finish
|
||||||
in
|
|
||||||
checkIdx >= start andalso checkIdx <= finish
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -83,8 +79,10 @@ struct
|
|||||||
let
|
let
|
||||||
(* adjust "finish" so that it does not consider
|
(* adjust "finish" so that it does not consider
|
||||||
* offset for "lower" vector *)
|
* offset for "lower" vector *)
|
||||||
val finish = finish - Vector.sub (sizes, Vector.length sizes - 1)
|
val finish =
|
||||||
val newNode = BRANCH (#[newVec], #[finish])
|
finish - Vector.sub (sizes, Vector.length sizes - 1)
|
||||||
|
val newNode = BRANCH
|
||||||
|
(#[newVec], #[finish])
|
||||||
in
|
in
|
||||||
APPEND newNode
|
APPEND newNode
|
||||||
end
|
end
|
||||||
@@ -107,7 +105,10 @@ 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 (#[{start = start, finish = finish}], #[finish])
|
val newNode = LEAF
|
||||||
|
( #[{start = start, finish = finish}]
|
||||||
|
, #[finish]
|
||||||
|
)
|
||||||
in
|
in
|
||||||
APPEND newNode
|
APPEND newNode
|
||||||
end
|
end
|
||||||
@@ -128,7 +129,8 @@ struct
|
|||||||
let
|
let
|
||||||
val maxSize = getFinishIdx tree
|
val maxSize = getFinishIdx tree
|
||||||
in
|
in
|
||||||
BRANCH (#[tree, newNode], #[maxSize, finish])
|
BRANCH
|
||||||
|
(#[tree, newNode], #[maxSize, finish])
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getStart tree =
|
fun getStart tree =
|
||||||
@@ -142,26 +144,22 @@ struct
|
|||||||
let
|
let
|
||||||
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
||||||
in
|
in
|
||||||
if idx = ~1 then {start = ~1, finish = ~1}
|
if idx = ~1 then
|
||||||
|
{start = ~1, finish = ~1}
|
||||||
else
|
else
|
||||||
let
|
let val {start, finish} = Vector.sub (values, idx)
|
||||||
val {start, finish} = Vector.sub (values, idx)
|
in {start = start + acc, finish = finish + acc}
|
||||||
in
|
|
||||||
{start = start + acc, finish = finish + acc}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
| BRANCH (nodes, sizes) =>
|
| BRANCH (nodes, sizes) =>
|
||||||
let
|
let
|
||||||
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
||||||
in
|
in
|
||||||
if idx = ~1 then {start = ~1, finish = ~1}
|
if idx = ~1 then
|
||||||
|
{start = ~1, finish = ~1}
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val prevSize =
|
val prevSize = if idx = 0 then 0 else Vector.sub (sizes, idx - 1)
|
||||||
if idx = 0 then
|
|
||||||
0
|
|
||||||
else
|
|
||||||
Vector.sub (sizes, idx - 1)
|
|
||||||
val acc = acc + prevSize
|
val acc = acc + prevSize
|
||||||
val cursorIdx = cursorIdx - prevSize
|
val cursorIdx = cursorIdx - prevSize
|
||||||
in
|
in
|
||||||
@@ -185,14 +183,11 @@ struct
|
|||||||
let
|
let
|
||||||
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
||||||
in
|
in
|
||||||
if idx = ~1 then {start = ~1, finish = ~1}
|
if idx = ~1 then
|
||||||
|
{start = ~1, finish = ~1}
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val prevSize =
|
val prevSize = if idx = 0 then 0 else Vector.sub (sizes, idx - 1)
|
||||||
if idx = 0 then
|
|
||||||
0
|
|
||||||
else
|
|
||||||
Vector.sub (sizes, idx - 1)
|
|
||||||
val cursorIdx = cursorIdx - prevSize
|
val cursorIdx = cursorIdx - prevSize
|
||||||
in
|
in
|
||||||
helpNextMatch (cursorIdx, Vector.sub (nodes, idx), prevSize)
|
helpNextMatch (cursorIdx, Vector.sub (nodes, idx), prevSize)
|
||||||
@@ -226,8 +221,7 @@ struct
|
|||||||
in loopNextMatch (start, finish, tree, count - 1)
|
in loopNextMatch (start, finish, tree, count - 1)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
let
|
let in
|
||||||
in
|
|
||||||
if cursorIdx >= start andalso cursorIdx <= finish then
|
if cursorIdx >= start andalso cursorIdx <= finish then
|
||||||
loopNextMatch (start, finish, tree, count)
|
loopNextMatch (start, finish, tree, count)
|
||||||
else
|
else
|
||||||
@@ -238,10 +232,8 @@ struct
|
|||||||
fun getLast (tree, acc) =
|
fun getLast (tree, acc) =
|
||||||
case tree of
|
case tree of
|
||||||
LEAF (values, _) =>
|
LEAF (values, _) =>
|
||||||
let
|
let val {start, finish} = Vector.sub (values, Vector.length values - 1)
|
||||||
val {start, finish} = Vector.sub (values, Vector.length values - 1)
|
in {start = start + acc, finish = finish + acc}
|
||||||
in
|
|
||||||
{start = start + acc, finish = finish + acc}
|
|
||||||
end
|
end
|
||||||
| BRANCH (nodes, sizes) =>
|
| BRANCH (nodes, sizes) =>
|
||||||
let
|
let
|
||||||
@@ -283,7 +275,8 @@ struct
|
|||||||
in
|
in
|
||||||
if #start result < cursorIdx then
|
if #start result < cursorIdx then
|
||||||
{start = #start result + acc, finish = #finish result + acc}
|
{start = #start result + acc, finish = #finish result + acc}
|
||||||
else {start = ~1, finish = ~1}
|
else
|
||||||
|
{start = ~1, finish = ~1}
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
@@ -307,19 +300,18 @@ struct
|
|||||||
let
|
let
|
||||||
val node = Vector.sub (nodes, idx)
|
val node = Vector.sub (nodes, idx)
|
||||||
val prevSize = Vector.sub (sizes, idx - 1)
|
val prevSize = Vector.sub (sizes, idx - 1)
|
||||||
val result = helpPrevMatch (cursorIdx - prevSize, node, acc + prevSize)
|
val result =
|
||||||
|
helpPrevMatch (cursorIdx - prevSize, node, acc + prevSize)
|
||||||
in
|
in
|
||||||
if #start result = ~1 then
|
if #start result = ~1 then
|
||||||
let
|
let
|
||||||
val prevPrevSize =
|
val prevPrevSize =
|
||||||
if idx - 2 < 0 then
|
if idx - 2 < 0 then 0 else Vector.sub (sizes, idx - 2)
|
||||||
0
|
|
||||||
else
|
|
||||||
Vector.sub (sizes, idx - 2)
|
|
||||||
in
|
in
|
||||||
getLast (Vector.sub (nodes, idx - 1), acc + prevPrevSize)
|
getLast (Vector.sub (nodes, idx - 1), acc + prevPrevSize)
|
||||||
end
|
end
|
||||||
else result
|
else
|
||||||
|
result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -358,7 +350,9 @@ struct
|
|||||||
(* todo: modify below functions so that they also
|
(* todo: modify below functions so that they also
|
||||||
* use rope-like metadata *)
|
* use rope-like metadata *)
|
||||||
|
|
||||||
datatype insert_result = INSERT_UPDATE of t | INSERT_SPLIT of t * t
|
datatype insert_result =
|
||||||
|
INSERT_UPDATE of t
|
||||||
|
| INSERT_SPLIT of t * t
|
||||||
|
|
||||||
fun getMaxSize tree =
|
fun getMaxSize tree =
|
||||||
case tree of
|
case tree of
|
||||||
@@ -444,24 +438,29 @@ struct
|
|||||||
(* have to split this node too *)
|
(* have to split this node too *)
|
||||||
let
|
let
|
||||||
(* slice sizes *)
|
(* slice sizes *)
|
||||||
val leftSize = VectorSlice.full #[getMaxSize left]
|
val leftSize =
|
||||||
val rightSize = VectorSlice.full #[getMaxSize right]
|
VectorSlice.full #[getMaxSize left]
|
||||||
|
val rightSize =
|
||||||
|
VectorSlice.full #[getMaxSize right]
|
||||||
|
|
||||||
val leftLen = SOME idx
|
val leftLen = SOME idx
|
||||||
val rightLen = SOME (Vector.length nodes - idx - 1)
|
val rightLen = SOME (Vector.length nodes - idx - 1)
|
||||||
|
|
||||||
val leftSizeSlice = VectorSlice.slice (sizes, 0, leftLen)
|
val leftSizeSlice = VectorSlice.slice (sizes, 0, leftLen)
|
||||||
val rightSizeSlice = VectorSlice.slice (sizes, idx + 1, rightLen)
|
val rightSizeSlice =
|
||||||
|
VectorSlice.slice (sizes, idx + 1, rightLen)
|
||||||
|
|
||||||
val leftSizes = VectorSlice.concat [leftSizeSlice, leftSize]
|
val leftSizes = VectorSlice.concat [leftSizeSlice, leftSize]
|
||||||
val rightSizes = VectorSlice.concat [rightSizeSlice, rightSize]
|
val rightSizes =
|
||||||
|
VectorSlice.concat [rightSizeSlice, rightSize]
|
||||||
|
|
||||||
(* slice nodes *)
|
(* slice nodes *)
|
||||||
val left = VectorSlice.full #[left]
|
val left = VectorSlice.full #[left]
|
||||||
val right = VectorSlice.full #[right]
|
val right = VectorSlice.full #[right]
|
||||||
|
|
||||||
val leftNodesSlice = VectorSlice.slice (nodes, 0, leftLen)
|
val leftNodesSlice = VectorSlice.slice (nodes, 0, leftLen)
|
||||||
val rightNodesSlice = VectorSlice.slice (nodes, idx + 1, rightLen)
|
val rightNodesSlice =
|
||||||
|
VectorSlice.slice (nodes, idx + 1, rightLen)
|
||||||
|
|
||||||
val leftNodes = VectorSlice.concat [leftNodesSlice, left]
|
val leftNodes = VectorSlice.concat [leftNodesSlice, left]
|
||||||
val rightNodes = VectorSlice.concat [right, rightNodesSlice]
|
val rightNodes = VectorSlice.concat [right, rightNodesSlice]
|
||||||
@@ -475,7 +474,8 @@ struct
|
|||||||
else
|
else
|
||||||
(* can join children into parent *)
|
(* can join children into parent *)
|
||||||
let
|
let
|
||||||
val midSizes = #[getMaxSize left, getMaxSize right]
|
val midSizes =
|
||||||
|
#[getMaxSize left, getMaxSize right]
|
||||||
val midSizes = VectorSlice.full midSizes
|
val midSizes = VectorSlice.full midSizes
|
||||||
val midNodes = #[left, right]
|
val midNodes = #[left, right]
|
||||||
val midNodes = VectorSlice.full midNodes
|
val midNodes = VectorSlice.full midNodes
|
||||||
@@ -506,8 +506,7 @@ struct
|
|||||||
in
|
in
|
||||||
INSERT_UPDATE (LEAF (item, size))
|
INSERT_UPDATE (LEAF (item, size))
|
||||||
end
|
end
|
||||||
else
|
else if finish > Vector.sub (sizes, Vector.length sizes - 1) then
|
||||||
if finish > Vector.sub (sizes, Vector.length sizes - 1) then
|
|
||||||
if Vector.length sizes = maxSize then
|
if Vector.length sizes = maxSize then
|
||||||
(* have to split *)
|
(* have to split *)
|
||||||
let
|
let
|
||||||
@@ -521,7 +520,8 @@ struct
|
|||||||
val midItems = VectorSlice.slice (items, halfSize, midLen)
|
val midItems = VectorSlice.slice (items, halfSize, midLen)
|
||||||
|
|
||||||
val rightSizes = VectorSlice.full #[finish]
|
val rightSizes = VectorSlice.full #[finish]
|
||||||
val rightItems = VectorSlice.full #[{start = start, finish = finish}]
|
val rightItems =
|
||||||
|
VectorSlice.full #[{start = start, finish = finish}]
|
||||||
|
|
||||||
val rightItems = VectorSlice.concat [midItems, rightItems]
|
val rightItems = VectorSlice.concat [midItems, rightItems]
|
||||||
val leftItems = VectorSlice.vector leftItems
|
val leftItems = VectorSlice.vector leftItems
|
||||||
@@ -549,7 +549,8 @@ struct
|
|||||||
(* have to split *)
|
(* have to split *)
|
||||||
let
|
let
|
||||||
val leftSizes = VectorSlice.full #[finish]
|
val leftSizes = VectorSlice.full #[finish]
|
||||||
val leftItems = VectorSlice.full #[{start = start, finish = finish}]
|
val leftItems =
|
||||||
|
VectorSlice.full #[{start = start, finish = finish}]
|
||||||
|
|
||||||
val midLen = SOME halfSize
|
val midLen = SOME halfSize
|
||||||
val rightLen = SOME (Vector.length items - halfSize)
|
val rightLen = SOME (Vector.length items - halfSize)
|
||||||
@@ -593,7 +594,8 @@ struct
|
|||||||
val leftItems = VectorSlice.slice (items, 0, leftLen)
|
val leftItems = VectorSlice.slice (items, 0, leftLen)
|
||||||
val rightItems = VectorSlice.slice (items, idx, rightLen)
|
val rightItems = VectorSlice.slice (items, idx, rightLen)
|
||||||
val midSize = VectorSlice.full #[finish]
|
val midSize = VectorSlice.full #[finish]
|
||||||
val midItem = VectorSlice.full #[{start = start, finish = finish}]
|
val midItem =
|
||||||
|
VectorSlice.full #[{start = start, finish = finish}]
|
||||||
in
|
in
|
||||||
if Vector.length items = maxSize then
|
if Vector.length items = maxSize then
|
||||||
(* have to return split *)
|
(* have to return split *)
|
||||||
|
|||||||
Reference in New Issue
Block a user