feat(list): add sorted_remove

This commit is contained in:
favonia 2021-05-22 20:28:45 -05:00
parent 3eb676c55c
commit 8c197da02c
3 changed files with 34 additions and 0 deletions

View file

@ -797,6 +797,30 @@ let sorted_insert ~cmp ?(uniq=false) x l =
List.mem x (sorted_insert ~cmp:CCInt.compare 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 uniq_succ ~eq l =
let rec f acc l = match l with let rec f acc l = match l with
| [] -> List.rev acc | [] -> List.rev acc

View file

@ -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). [x] is not duplicated. Default [false] ([x] will be inserted in any case).
@since 0.17 *) @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 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. (** [uniq_succ ~eq l] removes duplicate elements that occur one next to the other.
Examples: Examples:

View file

@ -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). [x] is not duplicated. Default [false] ([x] will be inserted in any case).
@since 0.17 *) @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 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. (** [uniq_succ ~eq l] removes duplicate elements that occur one next to the other.
Examples: Examples: