mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
style(list): move sorted_mem up
This commit is contained in:
parent
8d532f9a00
commit
1c6bc16362
3 changed files with 23 additions and 21 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue