From b6d99645eaecd0c4aa22fe60d84467fafda4b85a Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 14 Mar 2023 18:59:22 +0100 Subject: [PATCH] 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 --- src/core/CCSet.ml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/core/CCSet.ml b/src/core/CCSet.ml index 6bf73716..215cb77f 100644 --- a/src/core/CCSet.ml +++ b/src/core/CCSet.ml @@ -151,19 +151,19 @@ module Make (O : Map.OrderedType) = struct | None -> raise Not_found | 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 res = ref None in - S.iter + let _ = S.find_last_opt (fun x -> match f x with - | None -> () - | Some y -> res := Some y) - m; + | None -> false + | Some y -> res := Some y; true) + m in !res - include S - let add_seq seq set = let set = ref set in Seq.iter (fun x -> set := add x !set) seq;