mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
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:
parent
0b72812a55
commit
b6d99645ea
1 changed files with 7 additions and 7 deletions
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue