mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
feat(list): add sorted_remove
This commit is contained in:
parent
3eb676c55c
commit
8c197da02c
3 changed files with 34 additions and 0 deletions
|
|
@ -797,6 +797,30 @@ let sorted_insert ~cmp ?(uniq=false) x l =
|
|||
List.mem x (sorted_insert ~cmp:CCInt.compare x l))
|
||||
*)
|
||||
|
||||
let sorted_remove ~cmp ~key l =
|
||||
let rec aux cmp key left l = match l with
|
||||
| [] -> List.rev left
|
||||
| y :: tail ->
|
||||
match cmp key y with
|
||||
| 0 -> aux cmp key left tail
|
||||
| n when n<0 -> List.rev_append left l
|
||||
| _ -> aux cmp key (y::left) tail
|
||||
in
|
||||
aux cmp key [] l
|
||||
|
||||
(*$Q
|
||||
Q.(pair small_int (list small_int)) (fun (key,l) -> \
|
||||
let l = List.sort Stdlib.compare l in \
|
||||
is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~key l))
|
||||
Q.(pair small_int (list small_int)) (fun (key,l) -> \
|
||||
let l = List.sort Stdlib.compare l in \
|
||||
let l' = sorted_remove ~cmp:CCInt.compare ~key l in \
|
||||
List.length l' = List.length l - count (Int.equal key) l)
|
||||
Q.(pair small_int (list small_int)) (fun (key,l) -> \
|
||||
let l = List.sort Stdlib.compare l in \
|
||||
not (List.mem key (sorted_remove ~cmp:CCInt.compare ~key l)))
|
||||
*)
|
||||
|
||||
let uniq_succ ~eq l =
|
||||
let rec f acc l = match l with
|
||||
| [] -> List.rev acc
|
||||
|
|
|
|||
|
|
@ -522,6 +522,11 @@ val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a l
|
|||
[x] is not duplicated. Default [false] ([x] will be inserted in any case).
|
||||
@since 0.17 *)
|
||||
|
||||
val sorted_remove : cmp:('a -> 'a -> int) -> key:'a -> 'a list -> 'a list
|
||||
(** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that
|
||||
the return value is sorted too.
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val uniq_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list
|
||||
(** [uniq_succ ~eq l] removes duplicate elements that occur one next to the other.
|
||||
Examples:
|
||||
|
|
|
|||
|
|
@ -525,6 +525,11 @@ val sorted_insert : cmp:(('a -> 'a -> int) [@keep_label]) -> ?uniq:bool -> 'a ->
|
|||
[x] is not duplicated. Default [false] ([x] will be inserted in any case).
|
||||
@since 0.17 *)
|
||||
|
||||
val sorted_remove : cmp:(('a -> 'a -> int) [@keep_label]) -> key:('a [@keep_label]) -> 'a list -> 'a list
|
||||
(** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that
|
||||
the return value is sorted too.
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val uniq_succ : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a list -> 'a list
|
||||
(** [uniq_succ ~eq l] removes duplicate elements that occur one next to the other.
|
||||
Examples:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue