From 1ad80ea9ecbe287b718d8d39238a3b0662d9c195 Mon Sep 17 00:00:00 2001 From: harshey1103 Date: Tue, 3 Oct 2023 20:08:20 +0530 Subject: [PATCH 1/6] created depth function --- src/rtree.ml | 10 ++++++++++ src/rtree_intf.ml | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/rtree.ml b/src/rtree.ml index 315e7ae..f3051c1 100644 --- a/src/rtree.ml +++ b/src/rtree.ml @@ -222,6 +222,16 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct let load ?(max_node_load = 8) entries = let tree = omt ~m:max_node_load entries in { max_node_load; tree } + + let rec depth' = function + | Node ns -> + let sub_sizes = List.map (fun (_, n) -> depth' n) ns in + List.fold_left max 0 sub_sizes + | Leaf _ -> 1 + | Empty -> 0 + + let depth t = depth' t.tree + end module Rectangle = Rectangle diff --git a/src/rtree_intf.ml b/src/rtree_intf.ml index 05cd679..b8f465e 100644 --- a/src/rtree_intf.ml +++ b/src/rtree_intf.ml @@ -122,6 +122,9 @@ module type S = sig a better tree and is preferred over folding with {! insert}. It uses the {{: https://ceur-ws.org/Vol-74/files/FORUM_18.pdf} OMT algorithm}. *) + + val depth : t -> int + (** [depth tree] returns the depth of the tree. *) end module type Maker = functor From ea2d06cec446b6c017f8f26841b5c73f70bec1b8 Mon Sep 17 00:00:00 2001 From: harshey1103 Date: Tue, 3 Oct 2023 20:20:00 +0530 Subject: [PATCH 2/6] changed list name to sub_depths --- src/rtree.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rtree.ml b/src/rtree.ml index f3051c1..3b6129c 100644 --- a/src/rtree.ml +++ b/src/rtree.ml @@ -225,8 +225,8 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct let rec depth' = function | Node ns -> - let sub_sizes = List.map (fun (_, n) -> depth' n) ns in - List.fold_left max 0 sub_sizes + let sub_depths = List.map (fun (_, n) -> depth' n) ns in + List.fold_left max 0 sub_depths | Leaf _ -> 1 | Empty -> 0 From 3a7367a408533d900d52d6d568481607063c77bb Mon Sep 17 00:00:00 2001 From: harshey1103 Date: Tue, 3 Oct 2023 20:27:15 +0530 Subject: [PATCH 3/6] minor correction in depth' function --- src/rtree.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rtree.ml b/src/rtree.ml index 3b6129c..393f394 100644 --- a/src/rtree.ml +++ b/src/rtree.ml @@ -226,7 +226,7 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct let rec depth' = function | Node ns -> let sub_depths = List.map (fun (_, n) -> depth' n) ns in - List.fold_left max 0 sub_depths + 1 + List.fold_left max 0 sub_depths | Leaf _ -> 1 | Empty -> 0 From 5b5711d2d9761b1ed364ebe419e0ce02ac1ffb96 Mon Sep 17 00:00:00 2001 From: harshey1103 Date: Wed, 4 Oct 2023 21:25:20 +0530 Subject: [PATCH 4/6] fixed formatting --- src/rtree.ml | 11 +++++------ src/rtree_intf.ml | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/rtree.ml b/src/rtree.ml index 393f394..ae719a1 100644 --- a/src/rtree.ml +++ b/src/rtree.ml @@ -224,14 +224,13 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct { max_node_load; tree } let rec depth' = function - | Node ns -> - let sub_depths = List.map (fun (_, n) -> depth' n) ns in - 1 + List.fold_left max 0 sub_depths - | Leaf _ -> 1 - | Empty -> 0 + | Node ns -> + let sub_depths = List.map (fun (_, n) -> depth' n) ns in + 1 + List.fold_left max 0 sub_depths + | Leaf _ -> 1 + | Empty -> 0 let depth t = depth' t.tree - end module Rectangle = Rectangle diff --git a/src/rtree_intf.ml b/src/rtree_intf.ml index b8f465e..b6b327e 100644 --- a/src/rtree_intf.ml +++ b/src/rtree_intf.ml @@ -122,7 +122,7 @@ module type S = sig a better tree and is preferred over folding with {! insert}. It uses the {{: https://ceur-ws.org/Vol-74/files/FORUM_18.pdf} OMT algorithm}. *) - + val depth : t -> int (** [depth tree] returns the depth of the tree. *) end From 0f6a2ac34f972a91f3a4f923aa51abb84bb5243d Mon Sep 17 00:00:00 2001 From: harshey1103 Date: Thu, 5 Oct 2023 14:00:02 +0530 Subject: [PATCH 5/6] tail recursive --- src/rtree.ml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/rtree.ml b/src/rtree.ml index ae719a1..6391c7f 100644 --- a/src/rtree.ml +++ b/src/rtree.ml @@ -223,14 +223,15 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct let tree = omt ~m:max_node_load entries in { max_node_load; tree } - let rec depth' = function + let rec depth' node depth = + match node with | Node ns -> - let sub_depths = List.map (fun (_, n) -> depth' n) ns in - 1 + List.fold_left max 0 sub_depths - | Leaf _ -> 1 - | Empty -> 0 + let sub_depths = List.map (fun (_, n) -> depth' n depth + 1) ns in + List.fold_left max 0 sub_depths + | Leaf _ -> 1 + depth + | Empty -> depth - let depth t = depth' t.tree + let depth t = depth' t.tree 0 end module Rectangle = Rectangle From 611d25bb6a303931c245ec69a463a871c118352b Mon Sep 17 00:00:00 2001 From: harshey1103 Date: Thu, 5 Oct 2023 14:43:18 +0530 Subject: [PATCH 6/6] unit test for depth --- test/basic.ml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/basic.ml b/test/basic.ml index 860e5b5..58f14c4 100644 --- a/test/basic.ml +++ b/test/basic.ml @@ -158,6 +158,37 @@ let rectangle () = let r = Rtree.Rectangle.merge_many [ r1; r2 ] in assert (r = r3) +let test_depth () = + let module R = + Rtree.Make + (Rtree.Rectangle) + (struct + type t = line + + let t = lint_t + + type envelope = Rtree.Rectangle.t + + let envelope { p1 = x1, y1; p2 = x2, y2 } = + let x0 = Float.min x1 x2 in + let x1 = Float.max x1 x2 in + let y0 = Float.min y1 y2 in + let y1 = Float.max y1 y2 in + Rtree.Rectangle.v ~x0 ~y0 ~x1 ~y1 + end) + in + let lines = + [ + { p1 = (0., 0.); p2 = (1., 1.) }; + { p1 = (1., 1.); p2 = (2., 2.) }; + { p1 = (2., 2.); p2 = (3., 3.) }; + { p1 = (3., 3.); p2 = (4., 4.) }; + ] + in + let t = R.load ~max_node_load:2 lines in + let calc_depth = R.depth t in + assert (calc_depth = 2) + let suite = "R" >::: [ @@ -166,6 +197,7 @@ let suite = "lines" >:: test_lines; "omt" >:: omt_loader; "rect" >:: rectangle; + "depth" >:: test_depth; ] let _ = run_test_tt_main suite