Skip to content

Commit

Permalink
Depth function (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshey1103 authored Oct 5, 2023
1 parent ee12c37 commit 7e00275
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/rtree.ml
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

0 comments on commit 7e00275

Please sign in to comment.