mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
Merge pull request #304 from FardaleM/mem
harmonize CCList.mem and CCArray.mem
This commit is contained in:
commit
47b631d3f4
6 changed files with 38 additions and 9 deletions
|
|
@ -16,6 +16,7 @@ type 'a printer = Format.formatter -> 'a -> unit
|
|||
|
||||
(** {2 Arrays} *)
|
||||
|
||||
include CCShims_
|
||||
include CCShimsArray_
|
||||
|
||||
let empty = [| |]
|
||||
|
|
@ -197,8 +198,21 @@ let rev a =
|
|||
rev [| |] = [| |]
|
||||
*)
|
||||
|
||||
exception Found
|
||||
|
||||
let mem ?(eq = Stdlib.(=)) elt a =
|
||||
try
|
||||
Array.iter (fun e -> if eq e elt then raise_notrace 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)
|
||||
|
|
|
|||
|
|
@ -132,6 +132,11 @@ 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.
|
||||
@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
|
||||
that [f x = Some y]. Otherwise returns [None].
|
||||
|
|
|
|||
|
|
@ -131,6 +131,11 @@ 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.
|
||||
@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
|
||||
that [~f x = Some y]. Otherwise returns [None].
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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 *)
|
||||
|
||||
|
|
|
|||
|
|
@ -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 *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue