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 automerge.sml
end end
rope.sml tiny_rope.sml
utils.sml utils.sml

View File

@@ -1,15 +1,16 @@
signature ROPE = signature TINY_ROPE =
sig 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 append: 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 = structure TinyRope :> TINY_ROPE =
struct struct
datatype t = datatype t =
N0 of string N0 of string
@@ -249,6 +250,49 @@ struct
| DeletedNode => delRoot rope) | DeletedNode => delRoot rope)
end 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) = 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)

View File

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