From 2dc830735ecb42fb5840534afe2453e026238411 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 28 Jul 2020 03:51:36 -0700 Subject: [PATCH] [istd] refactor of ImperativeUnionFind to expose [find_opt] later Summary: I think this does the same thing as before. Reviewed By: skcho Differential Revision: D22576002 fbshipit-source-id: 8317dd82e --- infer/src/istd/ImperativeUnionFind.ml | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/infer/src/istd/ImperativeUnionFind.ml b/infer/src/istd/ImperativeUnionFind.ml index 24d475472..7a478e921 100644 --- a/infer/src/istd/ImperativeUnionFind.ml +++ b/infer/src/istd/ImperativeUnionFind.ml @@ -57,15 +57,18 @@ module Make (Set : Set) = struct let is_a_repr (t : t) e = not (H.mem t e) - let rec find (t : t) e : Repr.t = - match H.find_opt t e with - | None -> - Repr.of_elt e - | Some r -> - let r' = find t (r :> Set.elt) in - if not (phys_equal r r') then H.replace t e r' ; - r' - + let rec find_opt (t : t) e : Repr.t option = + H.find_opt t e + |> Option.map ~f:(fun (r : Repr.t) -> + match find_opt t (r :> Set.elt) with + | None -> + r + | Some r' -> + if not (phys_equal r r') then H.replace t e r' ; + r' ) + + + let find (t : t) e : Repr.t = match find_opt t e with Some r -> r | None -> Repr.of_elt e let merge (t : t) ~(from : Repr.t) ~(to_ : Repr.t) = H.replace t (from :> Set.elt) to_ end