mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-05 19:00:31 -05:00
feat(list): add sorted_mem
This commit is contained in:
parent
e58c5d8f3b
commit
80e403c969
3 changed files with 21 additions and 0 deletions
|
|
@ -797,6 +797,19 @@ 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]
|
||||
|
|
|
|||
|
|
@ -523,6 +523,10 @@ 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.
|
||||
|
|
|
|||
|
|
@ -526,6 +526,10 @@ 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