]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
fixed join operation and some broken unit tests. Also implemented crude ascii-only...
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 9 Nov 2017 14:03:01 +0000 (09:03 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 9 Nov 2017 14:03:01 +0000 (09:03 -0500)
docs/Rope.html
lib/rope.ml
lib/rope.mli
tests/rope_tests.ml

index c68a1a5ba7c923fa5a44d283d878148723950ece..56ebad42bdcdfd7d8fc1146e39d9261cf1520213 100644 (file)
@@ -76,7 +76,7 @@
 <pre><span id="VALsplit"><span class="keyword">val</span> split</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> int -> <a href="Rope.html#TYPEt">t</a> * <a href="Rope.html#TYPEt">t</a></code></pre>
 <pre><span id="VALdel"><span class="keyword">val</span> del</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> int -> int -> <a href="Rope.html#TYPEt">t</a></code></pre>
 <pre><span id="VALputs"><span class="keyword">val</span> puts</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> string -> int -> <a href="Rope.html#TYPEt">t</a></code></pre>
-<pre><span id="VALputc"><span class="keyword">val</span> putc</span> : <code class="type">'a -> 'b -> 'c -> 'a</code></pre>
+<pre><span id="VALputc"><span class="keyword">val</span> putc</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> int -> int -> <a href="Rope.html#TYPEt">t</a></code></pre>
 <pre><span id="VALnextc"><span class="keyword">val</span> nextc</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> int -> int</code></pre>
 <pre><span id="VALprevc"><span class="keyword">val</span> prevc</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> int -> int</code></pre>
 <pre><span id="VALis_bol"><span class="keyword">val</span> is_bol</span> : <code class="type"><a href="Rope.html#TYPEt">t</a> -> int -> bool</code></pre>
index 9b0653d04329d31fdd50001d7b3f00cbaa49b2de..d8211a2c73739849c5dfb4c7a76758a628fadeec 100644 (file)
@@ -106,28 +106,12 @@ let gets rope i j =
   let buf = Bytes.create (j - i) in
   iteri
     (fun n c ->
-      Bytes.set buf (n - i) (getb rope i);
+      Bytes.set buf (n - i) (Char.chr c);
       (n <= j))
     rope i;
   Bytes.unsafe_to_string buf
 
 (******************************************************************************)
-(*
-
-If both arguments are short leaves, we produce a flat rope (leaf) consisting of the concatenation.
-
-If the left argument is a concatenation node whose right son is a short leaf, and the right argument is also a short leaf, then we concatenate the two leaves, and then concatenate the result to the left son of the left argument.
-
-let join_special left right =
-  let rlen = (length right) in
-  match left with
-  | Leaf(s,o,l) ->
-      if (l + rlen) <= 256 then
-        flatten ()
-
-  | Node(ln,Leaf(s,o,l),_,_) ->
-      if (l + rlen) <= 256 then
-*)
 
 let flatten rope =
   let s = (gets rope 0 (length rope)) in
@@ -178,7 +162,8 @@ let rec puts rope s i =
   let middle = from_string s in
   (join (join left middle) right)
 
-let putc rope i c = rope (* TODO: convert and call puts *)
+let putc rope i c =
+  puts rope (String.make 1 (Char.chr c)) i
 
 let nextc rope pos =
   let next = limit_index rope (pos + 1) in
index 57be4b4a454b2a809106ea053dd52535686a8a8c..fd9e5456b8d7e5db1492086641b32244bf7835b4 100644 (file)
@@ -15,6 +15,7 @@ val height : rope -> int
 val limit_index : rope -> int -> int
 val last : rope -> int
 
+val flatten : rope -> rope
 val join : rope -> rope -> rope
 val split : rope -> int -> (rope * rope)
 val del : rope -> int -> int -> rope
index 11dddd5ac56909d30754a290cf470561c26a2917..156b8d5656b6e240a746f995cb5ea1e7e08562f6 100644 (file)
@@ -16,30 +16,38 @@ let  () =
     assert( length rope == 2 )
   );
 
+  (* flatten() tests *)
+  test "flatten : flatten a tree to a leaf" (fun () ->
+    let tree = Node (Leaf("a", 0, 1), Leaf("b", 0, 1), 2, 2) in
+    let leaf = (flatten tree) in
+    assert( match leaf with
+    | Leaf ("ab",0,2) -> true
+    | _ -> false)
+  );
+
   (* join() tests *)
   test "join : join two leaves into rope" (fun () ->
     let left  = Leaf("a", 0, 1) in
-    let right =  Leaf("a", 0, 1) in
+    let right =  Leaf("b", 0, 1) in
     let rope  = (join left right) in
     assert( match rope with
-    | Node (l,r,h,2) -> (l == left && r == right)
+    | Leaf ("ab",0,2) -> true
     | _ -> false)
   );
   test "join : join a rope with a leaf (l to r)" (fun () ->
-    let left  = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in
-    let right =  Leaf("a", 0, 1) in
+    let left  = join (Leaf("a", 0, 1)) (Leaf("b", 0, 1)) in
+    let right =  Leaf("c", 0, 1) in
     let rope  = (join left right) in
-    Printf.printf "length %d \n" (length rope);
     assert( match rope with
-    | Node (l,r,2,3) -> true
+    | Leaf ("abc",0,3) -> true
     | _ -> false)
   );
   test "join : join a rope with a leaf (r to l)" (fun () ->
     let left  =  Leaf("a", 0, 1) in
-    let right = join (Leaf("a", 0, 1)) (Leaf("a", 0, 1)) in
+    let right = Node (Leaf("b", 0, 1), Leaf("c", 0, 1), 2, 2) in
     let rope  = (join left right) in
     assert( match rope with
-    | Node (l,r,h,3) -> (l == left && r == right)
+    | Leaf ("abc",0,3) -> true
     | _ -> false)
   );
 
@@ -92,6 +100,7 @@ let  () =
     let rope = Leaf("bc", 0, 2) in
     let rope = (puts rope "a" 0) in
     assert( (length rope) == 3 );
+    assert( (gets rope 0 3) = "abc" );
     assert( (getc rope (0)) == Char.code 'a' );
     assert( (getc rope (1)) == Char.code 'b' );
     assert( (getc rope (2)) == Char.code 'c' );
@@ -100,6 +109,7 @@ let  () =
     let rope = Leaf("ac", 0, 2) in
     let rope = (puts rope "b" 1) in
     assert( (length rope) == 3 );
+    assert( (gets rope 0 3) = "abc" );
     assert( (getc rope (0)) == Char.code 'a' );
     assert( (getc rope (1)) == Char.code 'b' );
     assert( (getc rope (2)) == Char.code 'c' );
@@ -108,6 +118,7 @@ let  () =
     let rope = Leaf("ab", 0, 2) in
     let rope = (puts rope "c" 2) in
     assert( (length rope) == 3 );
+    assert( (gets rope 0 3) = "abc" );
     assert( (getc rope (0)) == Char.code 'a' );
     assert( (getc rope (1)) == Char.code 'b' );
     assert( (getc rope (2)) == Char.code 'c' );