CCSet: implement find_last_map using find_last

find_last exists since ocaml 4.05, using it for find_map avoid the
linear time behavior on ocaml >= 4.05
This commit is contained in:
Fardale 2023-03-14 18:59:22 +01:00
parent 0b72812a55
commit b6d99645ea

View file

@ -151,19 +151,19 @@ module Make (O : Map.OrderedType) = struct
| None -> raise Not_found | None -> raise Not_found
| Some x -> x | Some x -> x
(* linear time, must traverse the whole set… *) include S
(* Use find_last which is linear time on OCaml < 4.05 *)
let find_last_map f m = let find_last_map f m =
let res = ref None in let res = ref None in
S.iter let _ = S.find_last_opt
(fun x -> (fun x ->
match f x with match f x with
| None -> () | None -> false
| Some y -> res := Some y) | Some y -> res := Some y; true)
m; m in
!res !res
include S
let add_seq seq set = let add_seq seq set =
let set = ref set in let set = ref set in
Seq.iter (fun x -> set := add x !set) seq; Seq.iter (fun x -> set := add x !set) seq;