diff --git a/proj.mlb b/proj.mlb index 786a52b..b082aae 100644 --- a/proj.mlb +++ b/proj.mlb @@ -9,5 +9,5 @@ in automerge.sml end -rope.sml +tiny_rope.sml utils.sml diff --git a/rope.sml b/tiny_rope.sml similarity index 87% rename from rope.sml rename to tiny_rope.sml index f31cfda..d62ffbc 100644 --- a/rope.sml +++ b/tiny_rope.sml @@ -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) diff --git a/utils.sml b/utils.sml index 78dd097..986381a 100644 --- a/utils.sml +++ b/utils.sml @@ -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