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.

222
rope.sml
View File

@@ -1,4 +1,5 @@
signature ROPE = sig signature ROPE =
sig
type t type t
val empty: t val empty: t
val fromString: string -> t val fromString: string -> t
@@ -8,9 +9,10 @@ signature ROPE = sig
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
@@ -23,29 +25,19 @@ 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 ""
@@ -56,28 +48,21 @@ structure Rope :> ROPE = struct
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 let val left = N2 (t1, size t1, t2)
val left = N2(t1, size t1, t2) in N2 (left, size left, N1 t3)
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,21 +71,16 @@ 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 let val left = N2 (t1, size t1, t2)
val left = N2(t1, size t1, t2) in N2 (left, size left, N1 t3)
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)
@@ -109,18 +89,14 @@ structure Rope :> ROPE = struct
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)
@@ -143,13 +119,11 @@ structure Rope :> ROPE = struct
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)
@@ -158,13 +132,10 @@ structure Rope :> ROPE = struct
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
@@ -190,20 +161,15 @@ structure Rope :> ROPE = struct
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
@@ -211,11 +177,17 @@ structure Rope :> ROPE = struct
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
isLessThanTarget (oldStr, newStr)
then
(N0 (sub1 ^ newStr ^ sub2), NoAction) (N0 (sub1 ^ newStr ^ sub2), NoAction)
else if curIdx + String.size newStr <= targetLength then else if
curIdx + String.size newStr <= targetLength
then
(L2 (sub1 ^ newStr, sub2), AddedNode) (L2 (sub1 ^ newStr, sub2), AddedNode)
else if ((String.size oldStr) - curIdx) + String.size newStr <= targetLength then else if
((String.size oldStr) - curIdx) + String.size newStr <= targetLength
then
(L2 (sub1, newStr ^ sub2), AddedNode) (L2 (sub1, newStr ^ sub2), AddedNode)
else else
(N3 (N0 sub1, N0 newStr, N0 sub2), AddedNode) (N3 (N0 sub1, N0 newStr, N0 sub2), AddedNode)
@@ -236,12 +208,9 @@ structure Rope :> ROPE = struct
(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
@@ -255,39 +224,29 @@ structure Rope :> ROPE = struct
(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) =
@@ -298,10 +257,8 @@ structure Rope :> ROPE = struct
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
@@ -311,10 +268,8 @@ structure Rope :> ROPE = struct
(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) =
@@ -324,53 +279,34 @@ structure Rope :> ROPE = struct
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