Skip to content

Commit

Permalink
Merge pull request #14 from geocaml/stackoverflow
Browse files Browse the repository at this point in the history
Fix stackoverflows
  • Loading branch information
patricoferris authored Aug 10, 2023
2 parents d0a789b + 0e222a4 commit ba7ee18
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/import.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(*
* Copyright (c) 2019-2021 Craig Ferguson <craig@tarides.com>
* Copyright (c) 2018-2022 Tarides <contact@tarides.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

module List = struct
include Stdlib.List

let map f l =
let rec aux acc = function
| [] -> acc []
| h :: t -> (aux [@tailcall]) (fun t' -> acc (f h :: t')) t
in
aux (fun x -> x) l
end
9 changes: 6 additions & 3 deletions src/rectangle.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open! Import

type t = floatarray
(* lower left x, upper right x, lower left y, upper right y*)

Expand Down Expand Up @@ -56,10 +58,11 @@ let merge arr arr' =
Array.Floatarray.unsafe_set arr 3 (max y1 y1');
arr

let rec merge_many = function
| e :: [] -> e
| e :: es -> merge e (merge_many es)
let merge_many t =
let rec loop acc = function [] -> acc | e :: es -> loop (merge e acc) es in
match t with
| [] -> raise (Invalid_argument "can't zero envelopes")
| e :: es -> loop e es

let area arr =
let x0, x1, y0, y1 = (x0 arr, x1 arr, y0 arr, y1 arr) in
Expand Down
1 change: 1 addition & 0 deletions src/rtree.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open! Import
include Rtree_intf

module Make (E : Envelope) (V : Value with type envelope = E.t) = struct
Expand Down
10 changes: 10 additions & 0 deletions test/basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,23 @@ let omt_loader () =
let idx = R.load ~max_node_load:2 lines in
print_endline (Repr.to_string R.t idx)

let rectangle () =
let r1 = Rtree.Rectangle.v ~x0:(-1.) ~y0:(-1.) ~x1:1. ~y1:1. in
assert (r1 = Rtree.Rectangle.(merge r1 empty));
assert (r1 = Rtree.Rectangle.(merge_many [ r1 ]));
let r2 = Rtree.Rectangle.v ~x0:(-2.) ~y0:(-2.) ~x1:0. ~y1:0. in
let r3 = Rtree.Rectangle.v ~x0:(-2.) ~y0:(-2.) ~x1:1. ~y1:1. in
let r = Rtree.Rectangle.merge_many [ r1; r2 ] in
assert (r = r3)

let suite =
"R"
>::: [
"init" >:: test_init;
"functor" >:: test_functor;
"lines" >:: test_lines;
"omt" >:: omt_loader;
"rect" >:: rectangle;
]

let _ = run_test_tt_main suite

0 comments on commit ba7ee18

Please sign in to comment.