From e58c5d8f3bcf0b8740270cd8bf1d45405d125b98 Mon Sep 17 00:00:00 2001 From: favonia Date: Sat, 22 May 2021 21:41:36 -0500 Subject: [PATCH] fix(list): add the test sorted_diff (sorted_merge l1 l2) l2 = l1 Also fixed a typo. --- src/core/CCList.ml | 4 ++++ src/core/CCList.mli | 4 +++- src/core/CCListLabels.mli | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 52102681..434d240a 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -771,6 +771,10 @@ let sorted_diff ~cmp l1 l2 = Q.(triple small_nat small_nat int) (fun (n1,n2,x) -> \ let l = sorted_diff ~cmp:CCInt.compare (CCList.init n1 (fun _ -> x)) (CCList.init n2 (fun _ -> x)) in \ count (CCInt.equal x) l = CCInt.max (n1 - n2) 0) + Q.(pair (list small_int) (list small_int)) (fun (l1,l2) -> \ + let l1 = List.sort CCInt.compare l1 in \ + let l2 = List.sort CCInt.compare l2 in \ + l1 = sorted_diff ~cmp:CCInt.compare (sorted_merge ~cmp:CCInt.compare l1 l2) l2) *) let sort_uniq ~cmp l = List.sort_uniq cmp l diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 921a7e22..91148e24 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -504,7 +504,9 @@ val sorted_merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list val sorted_diff : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list (** [sorted_diff ~cmp l1 l2] returns the elements in [l1] that are not in [l2]. Both lists are assumed to be sorted with respect to [cmp] and - duplicate elements are treated individually. + duplicate elements are treated individually. It is the left inverse of + [sorted_merge]; that is, [sorted_diff ~cmp (sorted_merge ~cmp l1 l2) l2] + is always equal to [l1] for sorted lists [l1] and [l2]. @since NEXT_RELEASE *) val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index d55f1ecc..bca4cc6a 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -505,9 +505,11 @@ val sorted_merge : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list - the given comparison function [cmp]. *) val sorted_diff : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list -> 'a list -(** [sorted_merge ~cmp l1 l2] returns the elements in [l1] that are not in [l2]. +(** [sorted_diff ~cmp l1 l2] returns the elements in [l1] that are not in [l2]. Both lists are assumed to be sorted with respect to [cmp] and - duplicate elements are treated individually. + duplicate elements are treated individually. It is the left inverse of + [sorted_merge]; that is, [sorted_diff ~cmp (sorted_merge ~cmp l1 l2) l2] + is always equal to [l1] for sorted lists [l1] and [l2]. @since NEXT_RELEASE *) val sort_uniq : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list