From 2025a625362a675aa32f3bd43fe93d421be303dc Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 28 Apr 2020 15:39:47 +0200 Subject: [PATCH 1/5] feat(CCList): make mem compatible with the Stdlib The eq argument is now optional. --- src/core/CCList.ml | 9 +++++++-- src/core/CCList.mli | 6 +++--- src/core/CCListLabels.mli | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 9d2672dc..70ab4f5b 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1215,12 +1215,17 @@ let group_join_by (type a) ?(eq=Stdlib.(=)) ?(hash=Hashtbl.hash) f c1 c2 = (Error "e2") (all_ok [Ok 1; Error "e2"; Error "e3"; Ok 4]) *) -let mem ~eq x l = +let mem ?(eq=Stdlib.(=)) x l = let rec search eq x l = match l with | [] -> false | y::l' -> eq x y || search eq x l' in search eq x l +(*$Q mem + Q.(small_list small_int) (fun l -> \ + mem 1 l = (List.mem 1 l)) +*) + let add_nodup ~eq x l = if mem ~eq x l then l else x::l @@ -1545,7 +1550,7 @@ module Assoc = struct = [1, "1"; 2, "2"; 3, "3"] *) - let mem ~eq x l = + let mem ?(eq=Stdlib.(=)) x l = try ignore (search_exn eq l x); true with Not_found -> false diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 6f18433d..c1cd6eb4 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -544,7 +544,7 @@ val remove_one : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t (** [remove_one x set] removes one occurrence of [x] from [set]. Linear time. @since 0.11 *) -val mem : eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool (** Membership to the list. Linear time. *) val subset : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool @@ -607,7 +607,7 @@ module Assoc : sig val set : eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t (** Add the binding into the list (erase it if already present). *) - val mem : eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool + val mem : ?eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool (** [mem x l] returns [true] iff [x] is a key in [l]. @since 0.16 *) @@ -637,7 +637,7 @@ val assq_opt : 'a -> ('a * 'b) t -> 'b option @since 1.5, but only @since 2.0 with labels *) -val mem_assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool +val mem_assoc : ?eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool (** Like [Assoc.mem]. @since 2.0 *) diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index b9371d7d..714a3acf 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -548,7 +548,7 @@ val remove_one : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> 'a t -> 'a t (** [remove_one x set] removes one occurrence of [x] from [set]. Linear time. @since 0.11 *) -val mem : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> 'a t -> bool +val mem : ?eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> 'a t -> bool (** Membership to the list. Linear time. *) val subset : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a t -> 'a t -> bool @@ -611,7 +611,7 @@ module Assoc : sig val set : eq:(('a->'a->bool) [@keep_label]) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t (** Add the binding into the list (erase it if already present). *) - val mem : eq:(('a->'a->bool) [@keep_label]) -> 'a -> ('a,_) t -> bool + val mem : ?eq:(('a->'a->bool) [@keep_label]) -> 'a -> ('a,_) t -> bool (** [mem x l] returns [true] iff [x] is a key in [l]. @since 0.16 *) @@ -641,7 +641,7 @@ val assq_opt : 'a -> ('a * 'b) t -> 'b option @since 1.5, but only @since 2.0 with labels *) -val mem_assoc : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> ('a * _) t -> bool +val mem_assoc : ?eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> ('a * _) t -> bool (** Like [Assoc.mem]. @since 2.0 *) From c50672ff7ab078ab7cdb80e501873dcb233f11a1 Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 28 Apr 2020 15:41:01 +0200 Subject: [PATCH 2/5] feat(CCArray): add optional argument eq to mem --- src/core/CCArray.ml | 15 ++++++++++++++- src/core/CCArray.mli | 3 +++ src/core/CCArrayLabels.mli | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 2d593617..ac237245 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -197,8 +197,21 @@ let rev a = rev [| |] = [| |] *) +exception Found + +let mem ?(eq = Stdlib.(=)) elt a = + try + Array.iter (fun e -> if eq e elt then raise Found) a; + false + with Found -> true + +(*$Q mem + Q.(array small_int) (fun a -> \ + mem 1 a = (Array.mem 1 a)) +*) + let rec find_aux f a i = - if i = Array.length a then None + if i >= Array.length a then None else match f i a.(i) with | Some _ as res -> res | None -> find_aux f a (i+1) diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index cb6d4bb0..f254a8e6 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -132,6 +132,9 @@ val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array [lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]. @since 1.0 *) +val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +(** [mem ~eq x a] return true if x is present in [a]. Linear time. *) + val find_map : ('a -> 'b option) -> 'a t -> 'b option (** [find_map f a] returns [Some y] if there is an element [x] such that [f x = Some y]. Otherwise returns [None]. diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index af596800..bd23893b 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -131,6 +131,9 @@ val sort_ranking : f:('a -> 'a -> int) -> 'a t -> int array [lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]. @since 1.0 *) +val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +(** [mem ~eq x a] return true if x is present in [a]. Linear time. *) + val find_map : f:('a -> 'b option) -> 'a t -> 'b option (** [find_map ~f a] returns [Some y] if there is an element [x] such that [~f x = Some y]. Otherwise returns [None]. From 4267210da9a03bdb33c022bb6302b3f969f505a4 Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 28 Apr 2020 22:26:20 +0200 Subject: [PATCH 3/5] CCArray: use raise_notrace instead of raise --- src/core/CCArray.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index ac237245..b0c1b780 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -201,7 +201,7 @@ exception Found let mem ?(eq = Stdlib.(=)) elt a = try - Array.iter (fun e -> if eq e elt then raise Found) a; + Array.iter (fun e -> if eq e elt then raise_notrace Found) a; false with Found -> true From 789eee9d5365b07852abe335ddba8ad220e528ca Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 28 Apr 2020 22:26:42 +0200 Subject: [PATCH 4/5] CCArray: add @since --- src/core/CCArray.mli | 4 +++- src/core/CCArrayLabels.mli | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index f254a8e6..29e1cbfa 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -133,7 +133,9 @@ val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array @since 1.0 *) val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool -(** [mem ~eq x a] return true if x is present in [a]. Linear time. *) +(** [mem ~eq x a] return true if x is present in [a]. Linear time. + @since NEXT_RELEASE +*) val find_map : ('a -> 'b option) -> 'a t -> 'b option (** [find_map f a] returns [Some y] if there is an element [x] such diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index bd23893b..c74b7a11 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -132,7 +132,9 @@ val sort_ranking : f:('a -> 'a -> int) -> 'a t -> int array @since 1.0 *) val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool -(** [mem ~eq x a] return true if x is present in [a]. Linear time. *) +(** [mem ~eq x a] return true if x is present in [a]. Linear time. + @since NEXT_RELEASE +*) val find_map : f:('a -> 'b option) -> 'a t -> 'b option (** [find_map ~f a] returns [Some y] if there is an element [x] such From 7dd45a0fa8618afdb8418783ed671e045011ac28 Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 28 Apr 2020 22:31:33 +0200 Subject: [PATCH 5/5] fix(CCArray) fix use of Stdlib for ocaml < 4.07 --- src/core/CCArray.ml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index b0c1b780..5ac6090a 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -16,6 +16,7 @@ type 'a printer = Format.formatter -> 'a -> unit (** {2 Arrays} *) +include CCShims_ include CCShimsArray_ let empty = [| |]