format rope.sml using smlfmt

This commit is contained in:
2024-03-13 06:33:51 +00:00
parent 8177c99ef7
commit 84d6dacdeb
4 changed files with 175 additions and 239 deletions

BIN
.DS_Store vendored

Binary file not shown.

Binary file not shown.

364
rope.sml
View File

@@ -1,16 +1,18 @@
signature ROPE = sig signature ROPE =
sig
type t type t
val empty : t val empty: t
val fromString : string -> t val fromString: string -> t
val size : t -> int val size: t -> int
val insert : int * string * t -> t val insert: int * string * t -> t
val delete : int * int * t -> t val delete: int * int * t -> t
val toString : t -> string val toString: t -> string
end end
structure Rope :> ROPE = struct structure Rope :> ROPE =
datatype t struct
= N0 of string datatype t =
N0 of string
| N1 of t | N1 of t
| N2 of t * int * t | N2 of t * int * t
| L2 of string * string | L2 of string * string
@@ -22,62 +24,45 @@ structure Rope :> ROPE = struct
fun foldr (f, state, rope) = fun foldr (f, state, rope) =
case rope of case rope of
N2(l, _, r) => N2 (l, _, r) =>
let let val state = foldr (f, state, r)
val state = foldr(f, state, r) in foldr (f, state, l)
in
foldr(f, state, l)
end end
| N1 t => | N1 t => foldr (f, state, t)
foldr(f, state, t) | N0 s => f (state, s)
| N0 s => | _ => raise AuxConstructor
f (state, s)
| _ =>
raise AuxConstructor
fun toString rope = fun toString rope =
let let val strList = foldr ((fn (acc, str) => str :: acc), [], rope)
val strList = foldr((fn (acc, str) => str::acc), [], rope) in String.concat strList
in
String.concat strList
end end
datatype balance datatype balance = AddedNode | DeletedNode | NoAction
= AddedNode
| DeletedNode
| NoAction
val targetLength = 1024 val targetLength = 1024
val empty = N0 "" val empty = N0 ""
fun fromString string = N0 string fun fromString string = N0 string
fun isLessThanTarget(str1, str2) = fun isLessThanTarget (str1, str2) =
String.size str1 + String.size str2 <= targetLength String.size str1 + String.size str2 <= targetLength
fun helpSize(acc, rope) = fun helpSize (acc, rope) =
case rope of case rope of
N0 s => N0 s => acc + String.size s
acc + String.size s | N1 t => helpSize (acc, t)
| N1 t => | N2 (_, lm, r) => helpSize (acc + lm, r)
helpSize(acc, t)
| N2(_, lm, r) =>
helpSize(acc + lm, r)
| _ => raise AuxConstructor | _ => raise AuxConstructor
fun size rope = helpSize(0, rope) fun size rope = helpSize (0, rope)
fun insRoot rope = fun insRoot rope =
case rope of case rope of
L2(s1, s2) => L2 (s1, s2) => N2 (N0 s1, String.size s1, N0 s2)
N2(N0 s1, String.size s1, N0 s2) | N3 (t1, t2, t3) =>
| N3(t1, t2, t3) => let val left = N2 (t1, size t1, t2)
let in N2 (left, size left, N1 t3)
val left = N2(t1, size t1, t2)
in
N2(left, size left, N1 t3)
end end
| t => | t => t
t
fun delRoot rope = fun delRoot rope =
case rope of case rope of
@@ -86,291 +71,242 @@ structure Rope :> ROPE = struct
fun insN1 rope = fun insN1 rope =
case rope of case rope of
L2 (s1, s2) => L2 (s1, s2) => N2 (N0 s1, String.size s1, N0 s2)
N2(N0 s1, String.size s1, N0 s2) | N3 (t1, t2, t3) =>
| N3(t1, t2, t3) => let val left = N2 (t1, size t1, t2)
let in N2 (left, size left, N1 t3)
val left = N2(t1, size t1, t2)
in
N2(left, size left, N1 t3)
end end
| t => | t => N1 t
N1 t
fun insN2Left (left, right) = fun insN2Left (left, right) =
case (left, right) of case (left, right) of
(L2(s1, s2), t3) => (L2 (s1, s2), t3) => N3 (N0 s1, N0 s2, t3)
N3(N0 s1, N0 s2, t3) | (N3 (t1, t2, t3), N1 t4) =>
| (N3(t1, t2, t3), N1 t4) =>
let let
val left = N2(t1, size t1, t2) val left = N2 (t1, size t1, t2)
val right = N2(t3, size t3, t4) val right = N2 (t3, size t3, t4)
in in
N2(left, size left, right) N2 (left, size left, right)
end end
| (N3(t1, t2, t3), t4) => | (N3 (t1, t2, t3), t4) =>
let let val left = N2 (t1, size t1, t2)
val left = N2(t1, size t1, t2) in N3 (left, N1 t3, t4)
in
N3(left, N1 t3, t4)
end end
| (l, r) => | (l, r) => N2 (l, size l, r)
N2(l, size l, r)
fun delN2Left(left, right) = fun delN2Left (left, right) =
case (left, right) of case (left, right) of
(N1 t1, N1 t2) => (N1 t1, N1 t2) => N1 (N2 (t1, size t1, t2))
N1(N2(t1, size t1, t2)) | (N1 (N1 t1), N2 (N1 t2, _, (t3 as N2 _))) =>
| (N1 (N1 t1), N2(N1 t2, _, (t3 as N2 _))) =>
let let
val left = N2(t1, size t1, t2) val left = N2 (t1, size t1, t2)
val inner = N2(left, size left, t3) val inner = N2 (left, size left, t3)
in in
N1 (inner) N1 (inner)
end end
| (N1 (N1 t1), N2(N2(t2, _, t3), _, N1 t4)) => | (N1 (N1 t1), N2 (N2 (t2, _, t3), _, N1 t4)) =>
let let
val left = N2(t1, size t1, t2) val left = N2 (t1, size t1, t2)
val right = N2(t3, size t3, t4) val right = N2 (t3, size t3, t4)
val inner = N2(left, size left, right) val inner = N2 (left, size left, right)
in in
N1 inner N1 inner
end end
| (N1 (t1 as N1 _), N2((t2 as N2 _), _, (t3 as N2 _))) => | (N1 (t1 as N1 _), N2 ((t2 as N2 _), _, (t3 as N2 _))) =>
let let
val left = N2(t1, size t1, t2) val left = N2 (t1, size t1, t2)
val right = N1 t3 val right = N1 t3
in in
N2(left, size left, right) N2 (left, size left, right)
end end
| (l, r) => | (l, r) => N2 (l, size l, r)
N2(l, size l, r)
fun insN2Right (left, right) = fun insN2Right (left, right) =
case (left, right) of case (left, right) of
(t1, L2(s1, s2)) => (t1, L2 (s1, s2)) => N3 (t1, N0 s1, N0 s2)
N3(t1, N0 s1, N0 s2) | (N1 t1, N3 (t2, t3, t4)) =>
| (N1 t1, N3(t2, t3, t4)) =>
let let
val left = N2(t1, size t1, t2) val left = N2 (t1, size t1, t2)
val right = N2(t3, size t3, t4) val right = N2 (t3, size t3, t4)
in in
N2(left, size left, right) N2 (left, size left, right)
end end
| (t1, N3(t2, t3, t4)) => | (t1, N3 (t2, t3, t4)) =>
let let val right = N2 (t3, size t3, t4)
val right = N2(t3, size t3, t4) in N3 (t1, N1 t2, right)
in
N3(t1, N1 t2, right)
end end
| (l, r) => | (l, r) => N2 (l, size l, r)
N2(l, size l, r)
fun delN2Right(left, right) = fun delN2Right (left, right) =
case (left, right) of case (left, right) of
(N2(N1 t1, _, N2(t2, _, t3)), N1 (N1 t4)) => (N2 (N1 t1, _, N2 (t2, _, t3)), N1 (N1 t4)) =>
let let
val left = N2(t1, size t1, t2) val left = N2 (t1, size t1, t2)
val right = N2(t3, size t3, t4) val right = N2 (t3, size t3, t4)
val inner = N2(left, size left, right) val inner = N2 (left, size left, right)
in in
N1 inner N1 inner
end end
| (N2((t1 as N2 _), lm, N1 t2), N1 (N1 t3)) => | (N2 ((t1 as N2 _), lm, N1 t2), N1 (N1 t3)) =>
let let
val right = N2(t2, size t2, t3) val right = N2 (t2, size t2, t3)
val inner = N2(t1, lm, right) val inner = N2 (t1, lm, right)
in in
N1 inner N1 inner
end end
| (N2( (t1 as N2 _), _, (t2 as N2 _)), N1 (t3 as N1 _)) => | (N2 ((t1 as N2 _), _, (t2 as N2 _)), N1 (t3 as N1 _)) =>
let let
val left = N1 t1 val left = N1 t1
val right = N2(t2, size t2, t3) val right = N2 (t2, size t2, t3)
in in
N2(left, size left, right) N2 (left, size left, right)
end end
| (l, r) => | (l, r) => N2 (l, size l, r)
N2(l, size l, r)
fun insLeaf(curIdx, newStr, rope, oldStr) = fun insLeaf (curIdx, newStr, rope, oldStr) =
if curIdx <= 0 then if curIdx <= 0 then
if isLessThanTarget(oldStr, newStr) then if isLessThanTarget (oldStr, newStr) then (N0 (newStr ^ oldStr), NoAction)
(N0(newStr ^ oldStr), NoAction) else (L2 (newStr, oldStr), AddedNode)
else
(L2(newStr, oldStr), AddedNode)
else if curIdx >= String.size oldStr then else if curIdx >= String.size oldStr then
if isLessThanTarget(oldStr, newStr) then if isLessThanTarget (oldStr, newStr) then (N0 (oldStr ^ newStr), NoAction)
(N0(oldStr ^ newStr), NoAction) else (L2 (oldStr, newStr), AddedNode)
else
(L2(oldStr, newStr), AddedNode)
else else
(* Need to split in middle of string. *) (* Need to split in middle of string. *)
let let
val sub1 = String.substring(oldStr, 0, curIdx) val sub1 = String.substring (oldStr, 0, curIdx)
val sub2Len = String.size oldStr - curIdx val sub2Len = String.size oldStr - curIdx
val sub2 = String.substring(oldStr, curIdx, sub2Len) val sub2 = String.substring (oldStr, curIdx, sub2Len)
in in
if isLessThanTarget(oldStr, newStr) then if
(N0(sub1 ^ newStr ^ sub2), NoAction) isLessThanTarget (oldStr, newStr)
else if curIdx + String.size newStr <= targetLength then then
(L2(sub1 ^ newStr, sub2), AddedNode) (N0 (sub1 ^ newStr ^ sub2), NoAction)
else if ((String.size oldStr) - curIdx) + String.size newStr <= targetLength then else if
(L2(sub1, newStr ^ sub2), AddedNode) curIdx + String.size newStr <= targetLength
then
(L2 (sub1 ^ newStr, sub2), AddedNode)
else if
((String.size oldStr) - curIdx) + String.size newStr <= targetLength
then
(L2 (sub1, newStr ^ sub2), AddedNode)
else else
(N3(N0 sub1, N0 newStr, N0 sub2), AddedNode) (N3 (N0 sub1, N0 newStr, N0 sub2), AddedNode)
end end
fun ins(curIdx, newStr, rope) = fun ins (curIdx, newStr, rope) =
case rope of case rope of
N2(l, lm, r) => N2 (l, lm, r) =>
if curIdx < lm then if curIdx < lm then
let let
val (l, action) = ins(curIdx, newStr, l) val (l, action) = ins (curIdx, newStr, l)
in in
(case action of (case action of
NoAction => NoAction =>
(case (l, r) of (case (l, r) of
(N0 s1, N0 s2) => (N0 s1, N0 s2) =>
if isLessThanTarget(s1, s2) then if isLessThanTarget (s1, s2) then
(N0 (s1 ^ s2), DeletedNode) (N0 (s1 ^ s2), DeletedNode)
else else
(N2(l, lm + String.size newStr, r), action) (N2 (l, lm + String.size newStr, r), action)
| _ => | _ => (N2 (l, lm + String.size newStr, r), action))
(N2(l, lm + String.size newStr, r), action)) | AddedNode => (insN2Left (l, r), action)
| AddedNode => | DeletedNode => (delN2Left (l, r), action))
(insN2Left(l, r), action)
| DeletedNode =>
(delN2Left(l, r), action))
end end
else else
let let
val (r, action) = ins(curIdx - lm, newStr, r) val (r, action) = ins (curIdx - lm, newStr, r)
in in
(case action of (case action of
NoAction => NoAction =>
(case (l, r) of (case (l, r) of
(N0 s1, N0 s2) => (N0 s1, N0 s2) =>
if isLessThanTarget(s1, s2) then if isLessThanTarget (s1, s2) then
(N0 (s1 ^ s2), DeletedNode) (N0 (s1 ^ s2), DeletedNode)
else else
(N2(l, lm, r), action) (N2 (l, lm, r), action)
| _ => | _ => (N2 (l, lm, r), action))
(N2(l, lm, r), action)) | AddedNode => (insN2Right (l, r), action)
| AddedNode => | DeletedNode => (delN2Right (l, r), action))
(insN2Right(l, r), action)
| DeletedNode =>
(delN2Right(l, r), action))
end end
| N1 t => | N1 t =>
let let
val (t, action) = ins(curIdx, newStr, t) val (t, action) = ins (curIdx, newStr, t)
in in
(case action of (case action of
AddedNode => AddedNode => (insN1 t, action)
(insN1 t, action) | _ => (N1 t, action))
| _ =>
(N1 t, action))
end end
| N0 oldStr => | N0 oldStr => insLeaf (curIdx, newStr, rope, oldStr)
insLeaf(curIdx, newStr, rope, oldStr) | _ => raise AuxConstructor
| _ =>
raise AuxConstructor
fun insert (index, str, rope) = fun insert (index, str, rope) =
let let
val (rope, action) = ins(index, str, rope) val (rope, action) = ins (index, str, rope)
in in
(case action of (case action of
NoAction => NoAction => rope
rope | AddedNode => insRoot rope
| AddedNode => | DeletedNode => delRoot rope)
insRoot rope
| DeletedNode =>
delRoot rope)
end end
fun delLeaf(startIdx, endIdx, str) = fun delLeaf (startIdx, endIdx, str) =
if startIdx <= 0 andalso endIdx >= String.size str then if startIdx <= 0 andalso endIdx >= String.size str then
(empty, false) (empty, false)
else if startIdx >= 0 andalso endIdx <= String.size str then else if startIdx >= 0 andalso endIdx <= String.size str then
let let
val sub1 = String.substring(str, 0, startIdx) val sub1 = String.substring (str, 0, startIdx)
val sub2 = String.substring(str, endIdx, (String.size str - endIdx)) val sub2 = String.substring (str, endIdx, (String.size str - endIdx))
in in
if isLessThanTarget(sub1, sub2) then if isLessThanTarget (sub1, sub2) then (N0 (sub1 ^ sub2), false)
(N0 (sub1 ^ sub2), false) else (L2 (sub1, sub2), true)
else
(L2(sub1, sub2), true)
end end
else if startIdx >= 0 andalso endIdx >= String.size str then else if startIdx >= 0 andalso endIdx >= String.size str then
let let
val start = Int.toString startIdx val start = Int.toString startIdx
val str = String.substring(str, 0, startIdx) val str = String.substring (str, 0, startIdx)
in in
(N0 str, false) (N0 str, false)
end end
else else
let let val str = String.substring (str, endIdx, String.size str - endIdx)
val str = String.substring(str, endIdx, String.size str - endIdx) in (N0 str, false)
in
(N0 str, false)
end end
fun del (startIdx, endIdx, rope) = fun del (startIdx, endIdx, rope) =
case rope of case rope of
N2(l, lm, r) => N2 (l, lm, r) =>
if lm > startIdx andalso lm > endIdx then if lm > startIdx andalso lm > endIdx then
let let
val (l, didAdd) = del(startIdx, endIdx, l) val (l, didAdd) = del (startIdx, endIdx, l)
in in
if didAdd then if didAdd then (insN2Left (l, r), didAdd)
(insN2Left(l, r), didAdd) else (N2 (l, size l, r), didAdd)
else
(N2(l, size l, r), didAdd)
end end
else if lm < startIdx andalso lm < endIdx then else if lm < startIdx andalso lm < endIdx then
let let
val (r, didAdd) = del(startIdx - lm, endIdx - lm, r) val (r, didAdd) = del (startIdx - lm, endIdx - lm, r)
in in
if didAdd then if didAdd then (insN2Right (l, r), didAdd)
(insN2Right(l, r), didAdd) else (N2 (l, lm, r), didAdd)
else
(N2(l, lm, r), didAdd)
end end
else else
let let
val (r, didAddR) = del(startIdx - lm, endIdx - lm, r) val (r, didAddR) = del (startIdx - lm, endIdx - lm, r)
val (l, didaddL) = del(startIdx, endIdx, l) val (l, didaddL) = del (startIdx, endIdx, l)
in in
if didaddL then if didaddL then (insN2Left (l, r), didaddL)
(insN2Left(l, r), didaddL) else if didAddR then (insN2Right (l, r), didAddR)
else if didAddR then else (N2 (l, size l, r), false)
(insN2Right(l, r), didAddR)
else
(N2(l, size l, r), false)
end end
| N1 t => | N1 t =>
let let val (t, didAdd) = del (startIdx, endIdx, t)
val (t, didAdd) = del(startIdx, endIdx, t) in if didAdd then (insN1 t, didAdd) else (N1 t, didAdd)
in
if didAdd then
(insN1 t, didAdd)
else
(N1 t, didAdd)
end end
| N0 str => | N0 str => delLeaf (startIdx, endIdx, str)
delLeaf(startIdx, endIdx, str) | _ => raise AuxConstructor
| _ =>
raise AuxConstructor
fun delete(start, length, rope) = fun delete (start, length, rope) =
let let val (rope, didAdd) = del (start, start + length, rope)
val (rope, didAdd) = del(start, start + length, rope) in if didAdd then insRoot rope else delRoot rope
in
if didAdd then
insRoot rope
else
delRoot rope
end end
end end

View File

@@ -41,7 +41,7 @@ fun run_txns_time title arr =
end end
fun run_to_string rope = fun run_to_string rope =
Rope.to_string rope Rope.toString rope
fun run_to_string_time title rope = fun run_to_string_time title rope =
let let