2025-08-30 23:05:11 +01:00
|
|
|
structure PersistentVector =
|
|
|
|
|
struct
|
2025-09-29 14:02:07 +01:00
|
|
|
(* Clojure-style persistent vector, for building search list.
|
|
|
|
|
* There is an "int table" too, which stores the last index
|
|
|
|
|
* at the node with the same index.
|
|
|
|
|
* We can use the size table for binary search.
|
|
|
|
|
* *)
|
2025-08-30 23:05:11 +01:00
|
|
|
datatype t =
|
2025-09-29 14:02:07 +01:00
|
|
|
BRANCH of t vector * int vector
|
|
|
|
|
| LEAF of {start: int, finish: int} vector * int vector
|
2025-08-30 23:05:11 +01:00
|
|
|
|
|
|
|
|
val maxSize = 32
|
|
|
|
|
|
2025-09-29 14:49:50 +01:00
|
|
|
fun isEmpty t =
|
|
|
|
|
case t of
|
2025-09-29 14:55:20 +01:00
|
|
|
LEAF (_, sizes) => Vector.length sizes = 0
|
2025-09-29 14:49:50 +01:00
|
|
|
| _ => false
|
|
|
|
|
|
2025-09-29 14:02:07 +01:00
|
|
|
val empty = LEAF (#[], #[])
|
2025-08-30 23:05:11 +01:00
|
|
|
|
|
|
|
|
datatype append_result = APPEND of t | UPDATE of t
|
|
|
|
|
|
2025-09-29 14:29:43 +01:00
|
|
|
fun isInRange (checkIdx, t) =
|
|
|
|
|
case t of
|
|
|
|
|
BRANCH (nodes, sizes) =>
|
|
|
|
|
let
|
|
|
|
|
val searchIdx = BinSearch.equalOrMore (checkIdx, sizes)
|
|
|
|
|
in
|
|
|
|
|
if searchIdx = ~1 then
|
|
|
|
|
false
|
|
|
|
|
else
|
|
|
|
|
isInRange (checkIdx, Vector.sub (nodes, searchIdx))
|
|
|
|
|
end
|
|
|
|
|
| LEAF (values, sizes) =>
|
|
|
|
|
let
|
|
|
|
|
val searchIdx = BinSearch.equalOrMore (checkIdx, sizes)
|
|
|
|
|
in
|
|
|
|
|
if searchIdx = ~1 then
|
|
|
|
|
false
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {start, finish} = Vector.sub (values, searchIdx)
|
|
|
|
|
in
|
|
|
|
|
checkIdx >= start andalso checkIdx <= finish
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-29 14:02:07 +01:00
|
|
|
fun getFinishIdx t =
|
|
|
|
|
case t of
|
|
|
|
|
BRANCH (_, sizes) => Vector.sub (sizes, Vector.length sizes - 1)
|
|
|
|
|
| LEAF (_, sizes) => Vector.sub (sizes, Vector.length sizes - 1)
|
|
|
|
|
|
|
|
|
|
fun helpAppend (start, finish, tree) =
|
2025-08-30 23:05:11 +01:00
|
|
|
case tree of
|
2025-09-29 14:02:07 +01:00
|
|
|
BRANCH (nodes, sizes) =>
|
2025-08-30 23:05:11 +01:00
|
|
|
let
|
|
|
|
|
val lastNode = Vector.sub (nodes, Vector.length nodes - 1)
|
|
|
|
|
in
|
2025-09-29 14:02:07 +01:00
|
|
|
case helpAppend (start, finish, lastNode) of
|
2025-08-30 23:05:11 +01:00
|
|
|
UPDATE newLast =>
|
|
|
|
|
let
|
2025-09-29 14:02:07 +01:00
|
|
|
val lastPos = Vector.length nodes - 1
|
|
|
|
|
val newNode = Vector.update (nodes, lastPos, newLast)
|
|
|
|
|
val newSizes = Vector.update (sizes, lastPos, finish)
|
|
|
|
|
val newNode = BRANCH (newNode, newSizes)
|
2025-08-30 23:05:11 +01:00
|
|
|
in
|
|
|
|
|
UPDATE newNode
|
|
|
|
|
end
|
|
|
|
|
| APPEND newVec =>
|
|
|
|
|
if Vector.length nodes + 1 > maxSize then
|
2025-09-29 14:02:07 +01:00
|
|
|
let val newNode = BRANCH (#[newVec], #[finish])
|
2025-08-30 23:05:11 +01:00
|
|
|
in APPEND newNode
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val newNodes = Vector.concat [nodes, #[newVec]]
|
2025-09-29 14:02:07 +01:00
|
|
|
val newSizes = Vector.concat [sizes, #[finish]]
|
|
|
|
|
val newNodes = BRANCH (newNodes, newSizes)
|
2025-08-30 23:05:11 +01:00
|
|
|
in
|
|
|
|
|
UPDATE newNodes
|
|
|
|
|
end
|
|
|
|
|
end
|
2025-09-29 14:02:07 +01:00
|
|
|
| LEAF (values, sizes) =>
|
|
|
|
|
if Vector.length values + 1 > maxSize then
|
|
|
|
|
let val newNode = LEAF (#[{start = start, finish = finish}], #[finish])
|
2025-08-30 23:05:11 +01:00
|
|
|
in APPEND newNode
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-09-29 14:02:07 +01:00
|
|
|
val newNode = Vector.concat
|
|
|
|
|
[values, #[{start = start, finish = finish}]]
|
|
|
|
|
val newSizes = Vector.concat [sizes, #[finish]]
|
|
|
|
|
val newNode = LEAF (newNode, newSizes)
|
2025-08-30 23:05:11 +01:00
|
|
|
in
|
|
|
|
|
UPDATE newNode
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-29 14:02:07 +01:00
|
|
|
fun append (start, finish, tree) =
|
2025-10-08 08:10:51 +01:00
|
|
|
case helpAppend (start, finish, tree) of
|
|
|
|
|
UPDATE t => t
|
|
|
|
|
| APPEND newNode =>
|
|
|
|
|
let
|
|
|
|
|
val maxSize = getFinishIdx tree
|
|
|
|
|
in
|
|
|
|
|
BRANCH (#[tree, newNode], #[maxSize, finish])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun getStart tree =
|
|
|
|
|
case tree of
|
|
|
|
|
LEAF (values, _) => Vector.sub (values, 0)
|
|
|
|
|
| BRANCH (nodes, _) => getStart (Vector.sub (nodes, 0))
|
|
|
|
|
|
2025-10-08 10:27:19 +01:00
|
|
|
fun helpNextMatch (cursorIdx, tree) =
|
2025-10-08 08:10:51 +01:00
|
|
|
case tree of
|
|
|
|
|
LEAF (values, sizes) =>
|
|
|
|
|
let
|
2025-10-08 10:27:19 +01:00
|
|
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
2025-10-08 08:10:51 +01:00
|
|
|
in
|
|
|
|
|
if idx = ~1 then {start = ~1, finish = ~1}
|
|
|
|
|
else Vector.sub (values, idx)
|
|
|
|
|
end
|
|
|
|
|
| BRANCH (nodes, sizes) =>
|
|
|
|
|
let
|
2025-10-08 10:27:19 +01:00
|
|
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
2025-10-08 08:10:51 +01:00
|
|
|
in
|
|
|
|
|
if idx = ~1 then {start = ~1, finish = ~1}
|
2025-10-08 10:27:19 +01:00
|
|
|
else helpNextMatch (cursorIdx, Vector.sub (nodes, idx))
|
2025-10-08 08:10:51 +01:00
|
|
|
end
|
|
|
|
|
|
2025-10-08 10:27:19 +01:00
|
|
|
fun startNextMatch (cursorIdx, tree) =
|
2025-10-08 08:10:51 +01:00
|
|
|
case tree of
|
|
|
|
|
LEAF (values, sizes) =>
|
|
|
|
|
if Vector.length sizes = 0 then
|
|
|
|
|
{start = ~1, finish = ~1}
|
|
|
|
|
else
|
2025-09-29 14:29:43 +01:00
|
|
|
let
|
2025-10-08 10:27:19 +01:00
|
|
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
2025-10-08 08:10:51 +01:00
|
|
|
val idx = if idx = ~1 then 0 else idx
|
2025-09-29 14:29:43 +01:00
|
|
|
in
|
2025-10-08 08:10:51 +01:00
|
|
|
Vector.sub (values, idx)
|
|
|
|
|
end
|
|
|
|
|
| BRANCH (nodes, sizes) =>
|
|
|
|
|
let
|
2025-10-08 10:27:19 +01:00
|
|
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
2025-10-08 08:10:51 +01:00
|
|
|
in
|
|
|
|
|
if idx = ~1 then {start = ~1, finish = ~1}
|
2025-10-08 10:27:19 +01:00
|
|
|
else helpNextMatch (cursorIdx, Vector.sub (nodes, idx))
|
2025-10-08 08:10:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun loopNextMatch (prevStart, prevFinish, tree, count) =
|
|
|
|
|
if count = 0 then
|
|
|
|
|
prevStart
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {start, finish} = startNextMatch (prevFinish + 1, tree)
|
|
|
|
|
in
|
|
|
|
|
if start = ~1 then
|
|
|
|
|
let val {start, finish} = getStart tree
|
|
|
|
|
in loopNextMatch (start, finish, tree, count - 1)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
loopNextMatch (start, finish, tree, count - 1)
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-08 10:27:19 +01:00
|
|
|
fun nextMatch (cursorIdx, tree, count) =
|
2025-10-08 08:10:51 +01:00
|
|
|
if isEmpty tree then
|
|
|
|
|
~1
|
|
|
|
|
else
|
|
|
|
|
let
|
2025-10-08 10:27:19 +01:00
|
|
|
val {start, finish} = startNextMatch (cursorIdx, tree)
|
2025-10-08 08:10:51 +01:00
|
|
|
in
|
|
|
|
|
if start = ~1 then
|
|
|
|
|
let val {start, finish} = getStart tree
|
|
|
|
|
in loopNextMatch (start, finish, tree, count - 1)
|
2025-09-29 14:29:43 +01:00
|
|
|
end
|
2025-10-08 10:27:19 +01:00
|
|
|
else if cursorIdx >= start andalso cursorIdx <= finish then
|
2025-10-08 08:10:51 +01:00
|
|
|
loopNextMatch (start, finish, tree, count)
|
|
|
|
|
else
|
|
|
|
|
loopNextMatch (start, finish, tree, count - 1)
|
|
|
|
|
end
|
2025-10-08 08:16:20 +01:00
|
|
|
|
|
|
|
|
fun getLast tree =
|
|
|
|
|
case tree of
|
|
|
|
|
LEAF (values, _) =>
|
|
|
|
|
Vector.sub (values, Vector.length values - 1)
|
|
|
|
|
| BRANCH (nodes, _) =>
|
|
|
|
|
getLast (Vector.sub (nodes, Vector.length nodes - 1))
|
2025-10-08 10:27:19 +01:00
|
|
|
|
|
|
|
|
fun helpPrevMatch (cursorIdx, tree) =
|
|
|
|
|
case tree of
|
|
|
|
|
LEAF (values, sizes) =>
|
|
|
|
|
let
|
|
|
|
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
|
|
|
|
in
|
|
|
|
|
if idx < 0 then {start = ~1, finish = ~1}
|
2025-10-08 10:39:49 +01:00
|
|
|
else if idx = 0 then Vector.sub (values, 0)
|
2025-10-08 10:27:19 +01:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val current = Vector.sub (values, idx)
|
|
|
|
|
val prev = Vector.sub (values, idx - 1)
|
|
|
|
|
in
|
|
|
|
|
if cursorIdx > #start current then
|
|
|
|
|
current
|
|
|
|
|
else
|
|
|
|
|
prev
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
| BRANCH (nodes, sizes) =>
|
|
|
|
|
let
|
|
|
|
|
val idx = BinSearch.equalOrMore (cursorIdx, sizes)
|
|
|
|
|
in
|
|
|
|
|
if idx < 0 then {start = ~1, finish = ~1}
|
2025-10-08 10:39:49 +01:00
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val node = Vector.sub (nodes, idx)
|
|
|
|
|
val result = helpPrevMatch (cursorIdx, node)
|
|
|
|
|
in
|
|
|
|
|
result
|
|
|
|
|
end
|
2025-10-08 10:27:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun loopPrevMatch (prevStart, tree, count) =
|
|
|
|
|
if count = 0 then
|
|
|
|
|
prevStart
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {start, finish} = helpPrevMatch (prevStart - 1, tree)
|
|
|
|
|
in
|
|
|
|
|
if start = ~1 then
|
|
|
|
|
let val {start, finish} = getLast tree
|
|
|
|
|
in loopPrevMatch (start, tree, count - 1)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
loopPrevMatch (start, tree, count - 1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun prevMatch (cursorIdx, tree, count) =
|
|
|
|
|
if isEmpty tree then
|
|
|
|
|
~1
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val {start, finish} = helpPrevMatch (cursorIdx, tree)
|
|
|
|
|
in
|
|
|
|
|
if start = ~1 then
|
|
|
|
|
let val {start, finish} = getLast tree
|
|
|
|
|
in loopPrevMatch (start, tree, count - 1)
|
|
|
|
|
end
|
|
|
|
|
else if cursorIdx >= start andalso cursorIdx <= finish then
|
|
|
|
|
loopPrevMatch (start, tree, count)
|
|
|
|
|
else
|
|
|
|
|
loopPrevMatch (start, tree, count - 1)
|
|
|
|
|
end
|
2025-08-30 23:05:11 +01:00
|
|
|
end
|