style(list): move sorted_mem up

This commit is contained in:
favonia 2021-05-24 09:23:33 -05:00
parent 8d532f9a00
commit 1c6bc16362
3 changed files with 23 additions and 21 deletions

View file

@ -722,6 +722,19 @@ let map_product_l f l =
cmp_lii_unord (cartesian_product l) (map_product_l CCFun.id 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 sorted_merge ~cmp l1 l2 =
let rec recurse cmp acc l1 l2 = match l1,l2 with let rec recurse cmp acc l1 l2 = match l1,l2 with
| [], _ -> List.rev_append acc l2 | [], _ -> List.rev_append acc l2
@ -797,19 +810,6 @@ let is_sorted ~cmp l =
is_sorted ~cmp:CCInt.compare (List.sort Stdlib.compare 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 sorted_insert ~cmp ?(uniq=false) x l =
let rec aux cmp uniq x left l = match l with let rec aux cmp uniq x left l = match l with
| [] -> List.rev_append left [x] | [] -> List.rev_append left [x]

View file

@ -497,6 +497,11 @@ val all_ok : ('a, 'err) result t -> ('a t, 'err) result
@since 1.3, but only @since 1.3, but only
@since 2.2 with labels *) @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 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 (** [sorted_merge ~cmp l1 l2] merges elements from both sorted list using
the given comparison function [cmp]. *) the given comparison function [cmp]. *)
@ -535,10 +540,6 @@ val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool
@param cmp the comparison function. @param cmp the comparison function.
@since 0.17 *) @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 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, (** [sorted_insert ~cmp ?uniq x l] inserts [x] into [l] such that, if [l] was sorted,
then [sorted_insert x l] is sorted too. then [sorted_insert x l] is sorted too.

View file

@ -500,6 +500,11 @@ val all_ok : ('a, 'err) result t -> ('a t, 'err) result
@since 1.3, but only @since 1.3, but only
@since 2.2 with labels *) @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 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 (** [sorted_merge ~cmp l1 l2] merges elements from both sorted list using
the given comparison function [cmp]. *) 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. @param cmp the comparison function.
@since 0.17 *) @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 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, (** [sorted_insert ~cmp ?uniq x l] inserts [x] into [l] such that, if [l] was sorted,
then [sorted_insert x l] is sorted too. then [sorted_insert x l] is sorted too.