From 1c6bc16362758fd5c508bb63265e08954a212850 Mon Sep 17 00:00:00 2001 From: favonia Date: Mon, 24 May 2021 09:23:33 -0500 Subject: [PATCH] style(list): move sorted_mem up --- src/core/CCList.ml | 26 +++++++++++++------------- src/core/CCList.mli | 9 +++++---- src/core/CCListLabels.mli | 9 +++++---- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 538cab4d..85c2c2fc 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -722,6 +722,19 @@ let map_product_l f l = cmp_lii_unord (cartesian_product l) (map_product_l CCFun.id l)) *) +let rec sorted_mem ~cmp x l = match l with + | [] -> false + | y :: tail -> + match cmp x y with + | 0 -> true + | n when n<0 -> false + | _ -> (sorted_mem[@tailcall]) ~cmp x tail + +(*$Q + Q.(pair small_int (list small_int)) (fun (x,l) -> \ + sorted_mem ~cmp:CCInt.compare x (List.sort CCInt.compare l) = mem ~eq:CCInt.equal x l) +*) + let sorted_merge ~cmp l1 l2 = let rec recurse cmp acc l1 l2 = match l1,l2 with | [], _ -> List.rev_append acc l2 @@ -797,19 +810,6 @@ let is_sorted ~cmp l = is_sorted ~cmp:CCInt.compare (List.sort Stdlib.compare l)) *) -let rec sorted_mem ~cmp x l = match l with - | [] -> false - | y :: tail -> - match cmp x y with - | 0 -> true - | n when n<0 -> false - | _ -> (sorted_mem[@tailcall]) ~cmp x tail - -(*$Q - Q.(pair small_int (list small_int)) (fun (x,l) -> \ - sorted_mem ~cmp:CCInt.compare x (List.sort CCInt.compare l) = mem ~eq:CCInt.equal x l) -*) - let sorted_insert ~cmp ?(uniq=false) x l = let rec aux cmp uniq x left l = match l with | [] -> List.rev_append left [x] diff --git a/src/core/CCList.mli b/src/core/CCList.mli index fcf70f61..b24b5a78 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -497,6 +497,11 @@ val all_ok : ('a, 'err) result t -> ('a t, 'err) result @since 1.3, but only @since 2.2 with labels *) +val sorted_mem : cmp:('a -> 'a -> int) -> 'a -> 'a list -> bool +(** [sorted_mem ~cmp x l] and [mem x l] give the same result for any sorted list [l], + but potentially more efficiently. + @since NEXT_RELEASE *) + val sorted_merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list (** [sorted_merge ~cmp l1 l2] merges elements from both sorted list using the given comparison function [cmp]. *) @@ -535,10 +540,6 @@ val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool @param cmp the comparison function. @since 0.17 *) -val sorted_mem : cmp:('a -> 'a -> int) -> 'a -> 'a list -> bool -(** Suppose the list [l] is sorted. [sorted_mem x l] is [true] iff [x] is equal to an element of [l]. - @since NEXT_RELEASE *) - val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list (** [sorted_insert ~cmp ?uniq x l] inserts [x] into [l] such that, if [l] was sorted, then [sorted_insert x l] is sorted too. diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index c0b8016a..bca4d062 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -500,6 +500,11 @@ val all_ok : ('a, 'err) result t -> ('a t, 'err) result @since 1.3, but only @since 2.2 with labels *) +val sorted_mem : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a -> 'a list -> bool +(** [sorted_mem ~cmp x l] and [mem x l] give the same result for any sorted list [l], + but potentially more efficiently. + @since NEXT_RELEASE *) + val sorted_merge : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list -> 'a list (** [sorted_merge ~cmp l1 l2] merges elements from both sorted list using the given comparison function [cmp]. *) @@ -538,10 +543,6 @@ val is_sorted : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> bool @param cmp the comparison function. @since 0.17 *) -val sorted_mem : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a -> 'a list -> bool -(** Suppose the list [l] is sorted. [sorted_mem x l] is [true] iff [x] is equal to an element of [l]. - @since NEXT_RELEASE *) - val sorted_insert : cmp:(('a -> 'a -> int) [@keep_label]) -> ?uniq:bool -> 'a -> 'a list -> 'a list (** [sorted_insert ~cmp ?uniq x l] inserts [x] into [l] such that, if [l] was sorted, then [sorted_insert x l] is sorted too.