progress rewriting rope.sml

This commit is contained in:
2024-02-15 12:30:48 +00:00
parent f0fc084171
commit d5ffb4f762
5 changed files with 263 additions and 379 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
.rope.sml.swp Normal file

Binary file not shown.

1
mlton Submodule

Submodule mlton added at fd6d870578

533
rope.sml
View File

@@ -1,398 +1,277 @@
datatype rope = signature ROPE = sig
N0 of string type t
| N1 of rope val empty : t
| N2 of rope * int * int * rope val of_string : string -> t
val size : t -> int
val insert : int * string * t -> t
end
structure Rope :> ROPE = struct
datatype t
= N0 of string
| N1 of t
| N2 of t * int * t
| L2 of string * string | L2 of string * string
| N3 of rope * rope * rope | N3 of t * t * t
datatype balance
= AddedNode
| DeletedNode
| NoAction
val target_length = 1024 val target_length = 1024
val empty = N0 "" val empty = N0 ""
fun of_string string = N0 string fun of_string string = N0 string
exception Size fun is_less_than_target(str1, str2) =
exception Ins String.size str1 + String.size str2 <= target_length
exception Substring
exception Delete
exception To_string
fun size rope = exception AuxConstructor
local
fun help_size(acc, rope) =
case rope of case rope of
N0 s => String.size s N0 s =>
| N1 t => size t acc + String.size s
| N2(_, lm, rm, _) => lm + rm | N1 t =>
| N3(t1, t2, t3) => help_size(acc, t)
let | N2(_, lm, r) =>
val t1_size = size t1 help_size(acc + lm, r)
val t2_size = size t2 | _ => raise AuxConstructor
val t3_size = size t3
in in
t1_size + t2_size + t3_size fun size rope = help_size(0, rope)
end end
| _ => raise Size
fun root rope = fun ins_root rope =
case rope of
L2(s1, s2) => N2(N0 s1, String.size s1, String.size s2, N0 s2)
| N3(t1, t2, t3) =>
let
val t1_size = size t1
val t2_size = size t2
val left = N2(t1, t1_size, t2_size, t2)
val left_size = t1_size + t2_size
val t3_size = size t3
in
N2(left, left_size, t3_size, N1 t3)
end
| t => t
fun n1 rope =
case rope of case rope of
L2(s1, s2) => L2(s1, s2) =>
N2(N0 s1, String.size s1, String.size s2, N0 s2) N2(N0 s1, String.size s1, N0 s2)
| N3(t1, t2, t3) => | N3(t1, t2, t3) =>
let let
val t1_size = size t1 val t1_size = size t1
val left = N2(t1, t1_size, t2)
val t2_size = size t2 val t2_size = size t2
val left = N2(t1, t1_size, t2_size, t2)
val left_size = t1_size + t2_size
val t3_size = size t3
in in
N2(left, left_size, t3_size, N1 t3) N2(left, t1_size + t2_size, N1 t3)
end end
| t => N1 t | t =>
N1 t
fun ins_n2_left left right = fun del_root rope =
case rope of
N1 t => t
| t => t
fun ins_n1 rope =
case rope of
L2 (s1, s2) =>
N2(N0 s1, String.size s1, N0 s2)
| N3(t1, t2, t3) =>
let
val left = N2(t1, size t1, t2)
in
N2(left, size left, t2)
end
| t =>
N1 t
fun ins_n2_left (left, right) =
case (left, right) of case (left, right) of
(L2(s1, s2), t3) => N3(N0 s1, N0 s2, t3) (L2(s1, s2), t3) =>
N3(N0 s1, N0 s2, t3)
| (N3(t1, t2, t3), N1 t4) => | (N3(t1, t2, t3), N1 t4) =>
let let
val t1_size = size t1 val t1_size = size t1
val t2_size = size t2 val left = N2(t1, t1_size, t2)
val left = N2(t1, t1_size, t2_size, t2)
val left_size = t1_size + t2_size
val t3_size = size t3 val t3_size = size t3
val t4_size = size t4 val right = N2(t3, t3_size, t4)
val right = N2(t3, t3_size, t4_size, t4) val t2_size = size t2
val right_size = t3_size + t4_size val left_size = t1_size + t2_size
in in
N2(left, left_size, right_size, right) N2(left, left_size, right)
end
| (N3(t1, t2, t3), t4 as N2 _) =>
let
val left = N2(t1, size t1, size t2, t2)
in
N3(left, N1 t3, t4)
end end
| (N3(t1, t2, t3), t4) => | (N3(t1, t2, t3), t4) =>
let let
val t1_size = size t1 val left = N2(t1, size t2, t2)
val t2_size = size t2
val left = N2(t1, t1_size, t2_size, t2)
val t3_size = size t3
val t4_size = size t4
val right = N2(t3, t3_size, t4_size, t4)
val left_size = t1_size + t2_size
val right_size = t3_size + t4_size
in in
N2(left, left_size, right_size, right) N3(left, N1 t3, t4)
end end
| (l, r) => | (l, r) =>
N2(l, size l, size r, r) N2(l, size l, r)
fun ins_n2_right left right = fun del_n2_left(left, right) =
case (left, right) of case (left, right) of
(t1, L2(s1, s2)) => N3(t1, N0 s1, N0 s2) (N1 t1, N1 t2) =>
N1(N2(t1, size t1, t2))
| (N1 (N1 t1), N2(N1 t2, _, (t3 as N2 _))) =>
let
val left = N2(t1, size t1, t2)
val inner = N2(left, size left, t3)
in
N1 (inner)
end
| (N1 (N1 t1), N2(N2(t2, _, t3), _, N1 t4)) =>
let
val left = N2(t1, size t1, t2)
val right = N2(t3, size t3, t4)
val inner = N2(left, size left, right)
in
N1 inner
end
| (N1 (t1 as N1 _), N2((t2 as N2 _), _, (t3 as N2 _))) =>
let
val left = N2(t1, size t1, t2)
val right = N1 t3
in
N2(left, size left, right)
end
| (l, r) =>
N2(l, size l, r)
fun ins_n2_right (left, right) =
case (left, right) of
(t1, L2(s1, s2)) =>
N3(t1, N0 s1, N0 s2)
| (N1 t1, N3(t2, t3, t4)) => | (N1 t1, N3(t2, t3, t4)) =>
let let
val t1_size = size t1 val left = N2(t1, size t1, t2)
val t2_size = size t2
val left = N2(t1, t1_size, t2_size, t2)
val t3_size = size t3 val t3_size = size t3
val t4_size = size t4 val right = N2(t3, t3_size, t4)
val right = N2(t3, t3_size, t4_size, t4)
val right_size = t3_size + t4_size
val left_size = t1_size + t2_size
in in
N2(left, left_size, right_size, right) N2(left, size left, right)
end
| (t1 as N2 _, N3(t2, t3, t4)) =>
let
val right = N2(t3, size t3, size t4, t4)
in
N3(t1, N1 t2, right)
end end
| (t1, N3(t2, t3, t4)) => | (t1, N3(t2, t3, t4)) =>
let let
val t1_size = size t1 val right = N2(t3, size t3, t4)
val t2_size = size t2
val left = N2(t1, t1_size, t2_size, t2)
val t3_size = size t3
val t4_size = size t4
val right = N2(t3, t3_size, t4_size, t4)
val right_size = t3_size + t4_size
val left_size = t1_size + t2_size
in in
N2(left, left_size, right_size, right) N3(t1, N1 t2, right)
end end
| (l, r) => | (l, r) =>
N2(l, size l, size r, r) N2(l, size l, r)
fun ins cur_index string rope = fun del_n2_right(left, right) =
case rope of case (left, right) of
N0 str => (N2(N1 t1, _, N2(t2, _, t3)), N1 t4) =>
let let
val old_str_size = String.size str val left = N2(t1, size t1, t2)
val ins_str_size = String.size string val right = N2(t3, size t3, t4)
val total_str_size = old_str_size + ins_str_size val inner = N2(left, size left, right)
val left_size = ins_str_size + cur_index
val prefer_left = left_size <= target_length
val right_size = ins_str_size - cur_index
val prefer_right = right_size <= target_length
in in
N1 inner
end
| (N2((t1 as N2 _), lm, N1 t2), N1 (N1 t3)) =>
let
val right = N2(t2, size t2, t3)
val inner = N2(t1, lm, right)
in
N1 inner
end
| (N2( (t1 as N2 _), _, (t2 as N2 _)), N1 (N1 t3)) =>
let
val left_size = size t1
val left = N1 t1
val right = N2(t2, size t2, t3)
in
N2(left, left_size, right)
end
| (l, r) =>
N2(l, size l, r)
local
fun ins_leaf(cur_index, new_str, rope, old_str) =
if cur_index <= 0 then if cur_index <= 0 then
if total_str_size <= target_length then if is_less_than_target(old_str, new_str) then
N0(string ^ str) (N0(new_str ^ old_str), NoAction)
else L2(string, str)
else if cur_index >= old_str_size then
if total_str_size <= target_length then
N0(str ^ string)
else L2(str, string)
else if total_str_size <= target_length then
let
val str1 = String.substring(str, 0, cur_index)
val str3 = String.substring(str, cur_index, old_str_size - cur_index)
in
N0(String.concat [str1, string, str3])
end
else else
let (L2(new_str, old_str), AddedNode)
val str1 = String.substring(str, 0, cur_index) else if cur_index >= String.size old_str then
val str3 = String.substring(str, cur_index, old_str_size - cur_index) if is_less_than_target(old_str, new_str) then
in (N0(old_str ^ new_str), NoAction)
if prefer_left then
L2(str1 ^ string, str3)
else if prefer_right then
L2(str1, string ^ str3)
else else
N3(N0 str1, N0 string, N0 str3) (L2(old_str, new_str), AddedNode)
end else
end (* Need to split in middle of string. *)
| N1 t =>
let let
val t = ins cur_index string t val sub1 = String.substring(old_str, 0, cur_index)
val sub2_len = String.size old_str - cur_index
val sub2 = String.substring(old_str, cur_index, sub2_len)
in in
n1 t if is_less_than_target(old_str, new_str) then
(N0(sub1 ^ new_str ^ sub2), NoAction)
else if cur_index + String.size new_str <= target_length then
(L2(sub1 ^ new_str, sub2), AddedNode)
else if (String.size old_str - cur_index) + String.size new_str <= target_length then
(L2(sub1, new_str ^ sub2), AddedNode)
else
(N3(N0 sub1, N0 new_str, N0 sub2), AddedNode)
end end
| N2(l, lm, _, r) => fun ins(cur_index, new_str, rope) =
case rope of
N2(l, lm, r) =>
if cur_index < lm then if cur_index < lm then
let let
val l = ins cur_index string l val (l, action) = ins(cur_index, new_str, l)
in in
ins_n2_left l r (case action of
NoAction =>
(case (l, r) of
(N0 s1, N0 s2) =>
if is_less_than_target(s1, s2) then
(N0 (s1 ^ s2), DeletedNode)
else
(N2(l, lm + String.size new_str, r), action)
| _ =>
(N2(l, lm + String.size new_str, r), action))
| AddedNode =>
(ins_n2_left(l, r), action)
| DeletedNode =>
(del_n2_left(l, r), action))
end end
else else
let let
val next_index = cur_index - lm val (r, action) = ins(cur_index - lm, new_str, r)
val r = ins next_index string r
in in
ins_n2_right l r (case action of
end NoAction =>
| _ => raise Ins (case (l, r) of
(N0 s1, N0 s2) =>
fun insert index string rope = if is_less_than_target(s1, s2) then
let (N0 (s1 ^ s2), DeletedNode)
val rope = ins index string rope
in
root rope
end
fun sub start_idx end_idx acc rope =
case rope of
N0 str =>
let
val str_size = String.size str
val before_start = start_idx <= 0
val after_start = start_idx >= 0
val after_end = end_idx >= str_size
val before_end = end_idx <= str_size
in
if before_start andalso after_end then
(str :: acc)
else if after_start andalso before_start then
let
val len = end_idx - start_idx
val str = String.substring(str, start_idx, len)
in
(str :: acc)
end
else if after_start andalso after_end then
let
val len = str_size - start_idx
val str = String.substring(str, start_idx, len)
in
(str :: acc)
end
else else
let (N2(l, lm, r), action)
val str = String.substring(str, 0, end_idx) | _ =>
in (N2(l, lm, r), action))
(str::acc) | AddedNode =>
end (ins_n2_right(l, r), action)
end | DeletedNode =>
| N1 t => (del_n2_right(l, r), action))
sub start_idx end_idx acc t
| N2(l, lm, _, r) =>
let
val starts_before = lm > start_idx
val ends_before = lm > end_idx
val starts_after = lm < start_idx
val ends_after = lm < end_idx
in
if starts_before andalso ends_before then
sub start_idx end_idx acc l
else if starts_after andalso ends_after then
let
val next_start = start_idx - lm
val next_end = end_idx - lm
in
sub next_start next_end acc r
end
else
let
val next_start = start_idx - lm
val next_end = end_idx - lm
val sub_acc = sub next_start next_end acc r
in
sub start_idx end_idx sub_acc l
end
end
| _ => raise Substring
fun substring start length rope =
let
val finish = start + length
val lst = sub start finish [] rope
in
String.concat lst
end
fun del start_idx end_idx rope =
case rope of
N0 str =>
let
val str_size = String.size str
val before_start = start_idx <= 0
val after_end = end_idx >= str_size
val after_start = start_idx >= 0
val before_end = end_idx <= str_size
in
if before_start andalso after_end then
(N0 "", false)
else if after_start andalso before_end then
let
val str1 = String.substring(str, 0, start_idx)
val del_len = str_size - end_idx
val str2 = String.substring(str, start_idx, del_len)
val new_len = str_size - del_len
in
if new_len <= target_length then
(N0(str1 ^ str2), false)
else
(L2(str1, str2), true)
end
else if after_start andalso after_end then
let
val str = String.substring(str, 0, start_idx)
in
(N0 str, false)
end
else
let
val len = str_size - end_idx
val str = String.substring(str, end_idx, len)
in
(N0 str, false)
end
end end
| N1 t => | N1 t =>
let let
val (t, did_ins) = del start_idx end_idx t val (t, action) = ins(cur_index, new_str, t)
val t = if did_ins then n1 t else t
in in
(t, did_ins) (case action of
AddedNode =>
(ins_n1 t, action)
| _ =>
(N1 t, action))
end end
| N2(l, lm, rm, r) => | N0 old_str =>
ins_leaf(cur_index, new_str, rope, old_str)
| _ =>
raise AuxConstructor
in
fun insert (index, str, rope) =
let let
val start_is_less = lm > start_idx val (rope, action) = ins(index, str, rope)
val end_is_less = lm > end_idx
val start_is_more = lm < start_idx
val end_is_more = lm < end_idx
in in
if start_is_less andalso end_is_less then (case action of
let NoAction =>
val (l, did_ins) = del start_idx end_idx l rope
in | AddedNode =>
case did_ins of ins_root rope
false => | DeletedNode =>
(N2(l, size l, rm, r), false) del_root rope)
| true =>
(ins_n2_left l r, true)
end
else if start_is_more andalso end_is_more then
let
val next_start = start_idx - lm
val next_end = end_idx - lm
val (r, did_ins) = del next_start next_end r
in
case did_ins of
false =>
(N2(l, lm, size r, r), false)
| true =>
(ins_n2_right l r, true)
end
else
let
val (l, did_ins_l) = del start_idx end_idx l
val r_start = start_idx - lm
val r_end = end_idx - lm
val (r, did_ins_r) = del r_start r_end r
in
if did_ins_l then
(ins_n2_left l r, true)
else if did_ins_r then
(ins_n2_right l r, true)
else
(N2(l, size l, size r, r), false)
end end
end end
| _ => raise Delete
fun delete start length rope =
let
val finish = start + length
val (t, did_ins) = del start finish rope
val t = if did_ins then root rope else rope
in
t
end
fun to_str acc rope =
case rope of
N0 str =>
(str::acc)
| N1 t =>
to_str acc t
| N2 (l, _, _, r) =>
let
val acc = to_str acc r
in
to_str acc l
end
| _ => raise To_string
fun to_string rope =
let
val lst = to_str [] rope
in
String.concat lst
end end

View File

@@ -50,9 +50,15 @@ fun run_to_string_time title rope =
time_func title f time_func title f
end end
fun run_txns_1000_times counter arr acc = fun run_txns_1000_times (counter, arr, total) =
if counter = 1000 then if counter = 1000 then
acc let
val divisor = Int.toLarge 1000
val total = total div divisor
val str = LargeInt.toString total
in
print str
end
else else
let let
val start_time = Time.now() val start_time = Time.now()
@@ -63,9 +69,10 @@ fun run_txns_1000_times counter arr acc =
val end_time = Time.now() val end_time = Time.now()
val end_time = Time.toMilliseconds end_time val end_time = Time.toMilliseconds end_time
val time_diff = end_time - start_time val time_diff = end_time - start_time
val time_diff = LargeInt.toString time_diff val counter = counter + 1
val total = time_diff + total
in in
run_txns_1000_times (counter + 1) arr (time_diff::acc) run_txns_1000_times (counter, arr, total)
end end
fun write_file filename acc = fun write_file filename acc =
@@ -80,17 +87,14 @@ fun write_file filename acc =
val _ = val _ =
let let
val svelte = run_txns_1000_times 0 svelte_arr [] val start_time = LargeInt.fromInt 0
val _ = write_file "svelte_edit_traces.csv" svelte val svelte = run_txns_1000_times (999 ,svelte_arr, start_time )
val rust = run_txns_1000_times 0 rust_arr [] val rust = run_txns_1000_times (999 ,rust_arr, start_time )
val _ = write_file "rust_edit_traces.csv" rust
val seph = run_txns_1000_times 0 seph_arr [] val seph = run_txns_1000_times (999, seph_arr, start_time)
val _ = write_file "seph_edit_traces.csv" seph
val automerge = run_txns_1000_times 0 automerge_arr [] val automerge = run_txns_1000_times (999, automerge_arr , start_time)
val _ = write_file "automerge_edit_traces.csv" automerge
in in
() ()
end end