Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depth function #22

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/rtree.ml
harshey1103 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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' node depth =
match node with
| Node ns ->
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 0
end

module Rectangle = Rectangle
3 changes: 3 additions & 0 deletions src/rtree_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>::: [
Expand All @@ -166,6 +197,7 @@ let suite =
"lines" >:: test_lines;
"omt" >:: omt_loader;
"rect" >:: rectangle;
"depth" >:: test_depth;
]

let _ = run_test_tt_main suite