feat(list): add the optional argument all to sorted_remove

Also added another missing "since".
This commit is contained in:
favonia 2021-05-22 21:27:35 -05:00
parent f6829d1219
commit 6d2dc4ccf4
3 changed files with 45 additions and 17 deletions

View file

@ -825,28 +825,51 @@ 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 sorted_remove ~cmp ?(all=false) x l =
let rec aux cmp key left l = match l with let rec aux cmp all x left l = match l with
| [] -> List.rev left | [] -> List.rev left
| y :: tail -> | y :: tail ->
match cmp key y with match cmp x y with
| 0 -> aux cmp key left tail | 0 ->
if all then aux cmp all x left tail else List.rev_append left tail
| n when n<0 -> List.rev_append left l | n when n<0 -> List.rev_append left l
| _ -> aux cmp key (y::left) tail | _ -> aux cmp all x (y::left) tail
in in
aux cmp key [] l aux cmp all x [] l
(*$Q (*$Q
Q.(pair small_int (list small_int)) (fun (key,l) -> \ Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \ let l = List.sort Stdlib.compare l in \
is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~key l)) is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare x l))
Q.(pair small_int (list small_int)) (fun (key,l) -> \ Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \ let l = List.sort Stdlib.compare l in \
let l' = sorted_remove ~cmp:CCInt.compare ~key l in \ is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~all:false x l))
List.length l' = List.length l - count (CCInt.equal key) l) Q.(pair small_int (list small_int)) (fun (x,l) -> \
Q.(pair small_int (list small_int)) (fun (key,l) -> \
let l = List.sort Stdlib.compare l in \ let l = List.sort Stdlib.compare l in \
not (List.mem key (sorted_remove ~cmp:CCInt.compare ~key l))) is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~all:true x l))
Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \
let l' = sorted_remove ~cmp:CCInt.compare x l in \
List.length l' = List.length l - (if List.mem x l then 1 else 0))
Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \
let l' = sorted_remove ~cmp:CCInt.compare ~all:true x l in \
List.length l' = List.length l - count (CCInt.equal x) l)
Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \
let l' = sorted_remove ~cmp:CCInt.compare ~all:false x l in \
List.length l' = List.length l - (if List.mem x l then 1 else 0))
Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \
let l' = sorted_remove ~cmp:CCInt.compare x l in \
count (CCInt.equal x) l' = count (CCInt.equal x) l - (if List.mem x l then 1 else 0))
Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \
let l' = sorted_remove ~cmp:CCInt.compare ~all:false x l in \
count (CCInt.equal x) l' = count (CCInt.equal x) l - (if List.mem x l then 1 else 0))
Q.(pair small_int (list small_int)) (fun (x,l) -> \
let l = List.sort Stdlib.compare l in \
not (List.mem x (sorted_remove ~cmp:CCInt.compare ~all:true x l)))
*) *)
let uniq_succ ~eq l = let uniq_succ ~eq l =

View file

@ -528,9 +528,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 val sorted_remove : cmp:('a -> 'a -> int) -> ?all:bool -> 'a -> 'a list -> 'a list
(** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that (** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that
the return value is sorted too. the return value is sorted too.
@param all if true then all occurrences of [x] will be removed. Otherwise, only the first
[x] will be removed (if any). Default [false] (only the first will be removed).
@since NEXT_RELEASE *) @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

View file

@ -507,7 +507,8 @@ val sorted_merge : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list -
val sorted_diff : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list -> 'a list val sorted_diff : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list -> 'a list
(** [sorted_merge ~cmp l1 l2] returns the elements in [l1] that are not in [l2]. (** [sorted_merge ~cmp l1 l2] returns the elements in [l1] that are not in [l2].
Both lists are assumed to be sorted with respect to [cmp] and Both lists are assumed to be sorted with respect to [cmp] and
duplicate elements are treated individually. *) duplicate elements are treated individually.
@since NEXT_RELEASE *)
val sort_uniq : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list val sort_uniq : cmp:(('a -> 'a -> int) [@keep_label]) -> 'a list -> 'a list
(** [sort_uniq ~cmp l] sorts the list [l] using the given comparison function [cmp] (** [sort_uniq ~cmp l] sorts the list [l] using the given comparison function [cmp]
@ -530,9 +531,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 val sorted_remove : cmp:(('a -> 'a -> int) [@keep_label]) -> ?all:bool -> 'a -> 'a list -> 'a list
(** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that (** [sorted_insert ~cmp x l] removes [x] from a sorted list [l] such that
the return value is sorted too. the return value is sorted too.
@param all if true then all occurrences of [x] will be removed. Otherwise, only the first
[x] will be removed (if any). Default [false] (only the first will be removed).
@since NEXT_RELEASE *) @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