mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 11:45:31 -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
|
||||
| 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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue