add append function to tiny_rope.sml

This commit is contained in:
2024-03-13 06:54:19 +00:00
parent 7bd4fad8e8
commit 21fd98df8b
3 changed files with 51 additions and 7 deletions

View File

@@ -9,5 +9,5 @@ in
automerge.sml
end
rope.sml
tiny_rope.sml
utils.sml

View File

@@ -1,15 +1,16 @@
signature ROPE =
signature TINY_ROPE =
sig
type t
val empty: t
val fromString: string -> t
val size: t -> int
val insert: int * string * t -> t
val append: string * t -> t
val delete: int * int * t -> t
val toString: t -> string
end
structure Rope :> ROPE =
structure TinyRope :> TINY_ROPE =
struct
datatype t =
N0 of string
@@ -249,6 +250,49 @@ struct
| DeletedNode => delRoot rope)
end
fun app (newStr, rope) =
case rope of
N2 (l, lm, r) =>
let
val (r, action) = app (newStr, r)
in
(case action of
NoAction =>
(case (l, r) of
(N0 s1, N0 s2) =>
if isLessThanTarget (s1, s2) then
(N0 (s1 ^ s2), DeletedNode)
else
(N2 (l, lm, r), action)
| _ => (N2 (l, lm, r), action))
| AddedNode => (insN2Right (l, r), action)
| DeletedNode => (delN2Right (l, r), action))
end
| N1 t =>
let
val (t, action) = app (newStr, t)
in
(case action of
AddedNode => (insN1 t, action)
| _ => (N1 t, action))
end
| N0 oldStr =>
if isLessThanTarget (oldStr, newStr) then
(N0 (oldStr ^ newStr), NoAction)
else
(L2 (oldStr, newStr), AddedNode)
| _ => raise AuxConstructor
fun append (str, rope) =
let
val (rope, action) = app (str, rope)
in
(case action of
NoAction => rope
| AddedNode => insRoot rope
| DeletedNode => delRoot rope)
end
fun delLeaf (startIdx, endIdx, str) =
if startIdx <= 0 andalso endIdx >= String.size str then
(empty, false)

View File

@@ -21,17 +21,17 @@ fun run_txns arr =
let
val rope =
if del_num > 0
then Rope.delete(pos, del_num, rope)
then TinyRope.delete(pos, del_num, rope)
else rope
val str_size = String.size ins_str
val rope =
if str_size > 0
then Rope.insert(pos, ins_str, rope)
then TinyRope.insert(pos, ins_str, rope)
else rope
in
rope
end)
Rope.empty arr
TinyRope.empty arr
fun run_txns_time title arr =
let
@@ -41,7 +41,7 @@ fun run_txns_time title arr =
end
fun run_to_string rope =
Rope.toString rope
TinyRope.toString rope
fun run_to_string_time title rope =
let