From 1c24102e455b186b15ce821343c31b6e253d35c2 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 5 Sep 2018 07:26:25 -0700 Subject: [PATCH] DifferentialFilters: simplify relative_complements Reviewed By: ngorogiannis Differential Revision: D9633532 fbshipit-source-id: 609f95211 --- infer/src/backend/DifferentialFilters.ml | 20 +++++++++----------- infer/src/backend/DifferentialFilters.mli | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/infer/src/backend/DifferentialFilters.ml b/infer/src/backend/DifferentialFilters.ml index a4c886bd5..3dad75718 100644 --- a/infer/src/backend/DifferentialFilters.ml +++ b/infer/src/backend/DifferentialFilters.ml @@ -88,7 +88,7 @@ end (** Returns a triple [(l1', dups, l2')] where [dups] is the set of elements of that are in the intersection of [l1] and [l2] according to [cmd] and additionally satisfy [pred], and [lN'] is [lN] minus [dups]. [dups] contains only one witness for each removed issue, taken from [l1]. *) -let relative_complements ~compare ?(pred = fun _ -> true) l1 l2 = +let relative_complements ~compare ~pred l1 l2 = let rec aux ((out_l1, dups, out_l2) as out) in_l1 in_l2 = let is_last_seen_dup v = match dups with ld :: _ -> Int.equal (compare ld v) 0 | [] -> false @@ -98,19 +98,17 @@ let relative_complements ~compare ?(pred = fun _ -> true) l1 l2 = (* i = f *) if pred i then aux (out_l1, i :: dups, out_l2) is fs else aux (i :: out_l1, dups, f :: out_l2) is fs + | i :: is, _ when is_last_seen_dup i -> + aux out is in_l2 + | _, f :: fs when is_last_seen_dup f -> + aux out in_l1 fs | i :: is, f :: _ when compare i f < 0 -> (* i < f *) - let out_l1' = if is_last_seen_dup i then out_l1 else i :: out_l1 in - aux (out_l1', dups, out_l2) is in_l2 + aux (i :: out_l1, dups, out_l2) is in_l2 | _ :: _, f :: fs -> (* i > f *) - let out_l2' = if is_last_seen_dup f then out_l2 else f :: out_l2 in - aux (out_l1, dups, out_l2') in_l1 fs - | i :: is, [] when is_last_seen_dup i -> - aux out is in_l2 - | [], f :: fs when is_last_seen_dup f -> - aux out in_l1 fs - | _, _ -> + aux (out_l1, dups, f :: out_l2) in_l1 fs + | [], _ | _, [] -> (List.rev_append in_l1 out_l1, dups, List.rev_append in_l2 out_l2) in let l1_sorted = List.sort ~compare l1 in @@ -134,7 +132,7 @@ let skip_duplicated_types_on_filenames renamings (diff : Differential.t) : Diffe in let fixed_normalized = List.map diff.fixed ~f:(fun f -> (f, f.Jsonbug_t.file)) in let introduced_normalized', preexisting', fixed_normalized' = - relative_complements ~compare introduced_normalized fixed_normalized + relative_complements ~compare ~pred:(fun _ -> true) introduced_normalized fixed_normalized in let list_map_fst = List.map ~f:fst in ( list_map_fst introduced_normalized' diff --git a/infer/src/backend/DifferentialFilters.mli b/infer/src/backend/DifferentialFilters.mli index f226fb515..d8ff40e89 100644 --- a/infer/src/backend/DifferentialFilters.mli +++ b/infer/src/backend/DifferentialFilters.mli @@ -39,7 +39,7 @@ val do_filter : module VISIBLE_FOR_TESTING_DO_NOT_USE_DIRECTLY : sig val relative_complements : compare:('a -> 'a -> int) - -> ?pred:('a -> bool) + -> pred:('a -> bool) -> 'a list -> 'a list -> 'a list * 'a list * 'a list